<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["list"] = array();

if (isset($_POST["cname"]) ) {
 
$cname=$_POST["cname"];
$email=$_POST["mail"];
$id=$_POST["id"];
$list=json_decode($_POST["list"],true);
$status=$_POST["status"];

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());


$query="SELECT hcm_expense.company,hcm_expense.tr_id,tr_emailid as exp_emailid,hcm_expense.exp_id,exp_from_location,exp_to_location,exp_amount,exp_type,img_path,exp_datetime,exp_created_by,exp_created_type,exp_deleted FROM hcm_expense INNER JOIN hcm_travel ON hcm_travel.tr_id=hcm_expense.tr_id AND hcm_travel.company=hcm_expense.company WHERE hcm_travel.company='$cname' ";

if(!($id)){
    $query=$query." AND tr_emailid like '%$email%'";
}else{
    $query=$query." AND exp_id='$id'";
}


if(count($list)!=0){

$isFirst=true;

foreach ($list as $value) {
    $id=$value["id"];
    $dt=$value["dt"];
    if(!$isFirst){
       $query=$query." OR "; 
    }else{
        $isFirst=false;
    }
    $query=$query." (exp_id='$id' and exp_datetime!='$dt') ";
}
 $query=$query.")";
}

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

}

echo json_encode($response,JSON_UNESCAPED_SLASHES);


?>