<?php

require_once __DIR__ . '/db_config.php';

$response = array();
$response["travels"] = 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 company,tr_id,tr_emailid,tr_place_to_visit,tr_start, tr_end,tr_duration,tr_purpose_visit,tr_customer_name,tr_billable,tr_travel_type,tr_status,t_datetime,updated_datetime,createdby,createdtype,t_deleted FROM hcm_travel WHERE company='$cname'";

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


$isFirst=true;

if(count($list)!=0){
    $query=$query." AND  ( tr_status='$status' OR ("; 

foreach ($list as $value) {
    $id=$value["id"];
    $dt=$value["dt"];
    if(!$isFirst){
       $query=$query." OR "; 
    }else{
        $isFirst=false;
    }
    $query=$query." (tr_id='$id' and updated_datetime!='$dt') ";
}
 $query=$query."))";
}else{
    $query=$query." AND  tr_status='$status' "; 
}

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

}

echo json_encode($response);


?>