<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    if(isset($_GET['sid']) && isset($_GET['token']))
    {
        $shop_id = $_GET['sid'];
        $shop_code = $_GET['token'];
    
        include 'token_check.php';
        
        if($token_check)
        {
            $orderid = mysqli_real_escape_string($con, trim($request->value));
            
            $sql = "SELECT * FROM orders where order_id = '$orderid' and shop_id = '$shop_id' and shop_code = '$shop_code'";
            $result = mysqli_query($con,$sql);
            if($rows = mysqli_fetch_assoc($result))
            {
            	$status = $rows['status'];
            	$ostatus = $rows['o_status'];
            }
            
            if($status == "preparing" && $ostatus == "1")
            {
                $sql = "UPDATE orders set o_status = '2' where order_id = '$orderid' and shop_id = '$shop_id' and shop_code = '$shop_code'";
                $result = mysqli_query($con,$sql);
                if($result)
                {
                    $rows = array('status'=>true,'message' => 'Food Ready for Delivered');
                    echo json_encode($rows);
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'Order Missing');
                    echo json_encode($rows);
                }
            }
        }
        else
        {
            $rows = array('status'=>"",'message' => 'Data Error');
            echo json_encode($rows);
        }
    }
}

?>