<?php

$response = array();
require_once __DIR__ . '/db_config.php';

if (isset($_POST["companyname"]) && isset($_POST["submittedby"]) && isset($_POST["dateandtime"]) ) {
    
    $companyname = $_POST['companyname'];
    $submittedby = $_POST['submittedby'];
    $dateandtime= $_POST['dateandtime'];
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

    $query="SELECT ticketnumber,expensetype,description,distanceinkms,fromlocation,tolocation,location,claimamount,imagefilepath,dateandtime FROM expense WHERE  companyname = '$companyname' AND submittedby = '$submittedby' ";
    if($dateandtime != "0000-00-00"){
            $query=$query." AND DATE(dateandtime) = DATE('$dateandtime')";
    }
    
    $query=$query." order by dateandtime DESC";
    

    $result=mysqli_query($con,$query);
    $response["expense"]=array();
    if(!empty($result) && mysqli_num_rows($result) > 0){
        while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
            array_push($response["expense"], $row);
        }
        $response["s"] = 1;
    }else{
        $response["s"] = 2;
    }
    
    mysqli_close($con);
}else {
    $response["s"] = 0;
}

echo json_encode($response);


?>