<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{

$orderid = mysqli_real_escape_string($con, trim($request->value));
$s_status = mysqli_real_escape_string($con, trim($request->status));

$sql = "SELECT * FROM orders where id = '$orderid'";
$result = mysqli_query($con,$sql);
if($rows = mysqli_fetch_assoc($result))
{
	$status = $rows['status'];
}

if($status == "pending" && $s_status == "accept")
{
    $sql = "UPDATE orders set status = 'preparing' where id = '$orderid'";
    $result = mysqli_query($con,$sql);
    if($result)
    {
        $rows = array('status'=>true,'message' => 'Order Accepted');
        echo json_encode($rows);
    }
    else
    {
        $rows = array('status'=>false,'message' => 'Order Missing');
        echo json_encode($rows);
    }
}

else if($status == "pending" && $s_status != "accept")
{
    $sql = "UPDATE orders set status = 'cancelled', comment= '$s_status' where id = '$orderid'";
    $result = mysqli_query($con,$sql);
    if($result)
    {
        $rows = array('status'=>true,'message' => 'Order Rejected');
        echo json_encode($rows);
    }
    else
    {
        $rows = array('status'=>false,'message' => 'Order Missing');
        echo json_encode($rows);
    }
}
}

?>