<?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);
$user_id=$json['user_id'];
$sql="SELECT save_order.*,order_item.cat_id,order_item.subcat_id FROM save_order INNER JOIN order_item ON save_order.id=order_item.order_id where user_id='$user_id' group by order_item.order_id order by id desc";
$result = $conn->query($sql);

$arr=array();
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
    $order_id=$row['id'];
    $sqlcat="SELECT * FROM rating WHERE order_id='$order_id'";
    $resultcat = $conn->query($sqlcat);
    $rate=0;
    if(mysqli_num_rows($resultcat)>0){
        $rate=1;
    }
    $status=$row['job_status'];
    if($status==0){
     $stacom="Pending"; 
    }else if($status==1){
    $stacom="Approved";  
    }else{
    $stacom="Completed";  
    }
    $arr[]=array("name"=>$row['name'],"order_id"=>$row['id'],"job_date"=>$row['job_date'],"job_time"=>$row['job_time'],"street"=>$row['street'],"address"=>$row['address'],"status"=>$stacom,"cat_id"=>$row['cat_id'],"subcat_id"=>$row['subcat_id'],"vendor_id"=>$row['vendor_id'],"user_id"=>$row['user_id'],"rate"=>$rate);
}
$data = array("data" => $arr);
echo json_encode($data);    
$conn->close();
?>