<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include 'dbconfig.php';

if(isset($_SERVER['HTTP_ORIGIN'])) {
        header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Max-Age: 86400');    // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
            header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

        exit(0);
}

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$json = json_decode(file_get_contents('php://input'), true);
$vendor_id=$json['vendor_id'];
//$vendor_id=1001;
$today = date("Y-m-d"); 
$sql="SELECT * FROM save_order where vendor_id='$vendor_id' AND job_date >= '$today' AND job_status !='2' order by id desc";
$result = $conn->query($sql);
$arr=array();
while($row = mysqli_fetch_assoc($result)) {
$order_id=$row['id']; 
$job_time1=$row['job_time'];
$status=$row['job_status'];
   if($status==0){
     $stacom="Pending"; 
    }else if($status==1){
    $stacom="Approved";  
    }else if($status==2){
    $stacom="Completed";  
    }else{
    $stacom="Canceled";  
    }
$job_time = date('h:i A', strtotime($job_time1));
$sqlorder="SELECT * FROM order_item WHERE order_id='$order_id'";
$resultorder = $conn->query($sqlorder); 
$total=0;
$service_name= array();
while($fetchrow = mysqli_fetch_assoc($resultorder)) {    
$total+=$fetchrow['service_total'];
$service=$fetchrow['service_name'].' At '.$row['address_type'];
array_push($service_name,$service);
}
$service_name1=implode(",",$service_name);
    $arr[]=array("name"=>$row['name'],"order_id"=>$row['id'],"job_date"=>$row['job_date'],"job_time"=>$job_time,"street"=>$row['street'],"address"=>$row['address'],"address_type"=>$row['address_type'],"vendor_id"=>$row['vendor_id'],"prefix"=>$row['prefix'],"user_id"=>$row['user_id'],"total"=>$total,"service_name"=>$service_name1,"phone"=>$row['phone'],"job_status"=>$stacom);
}
$data = array("data" => $arr);
echo json_encode($data);    
$conn->close();
?>