<?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));
    
    $counter = $con->query("SELECT * FROM orders where order_id = '$orderid'")->num_rows;
    if($counter > 0)
    {
        $sql = "SELECT * FROM orders where order_id = '$orderid'";
        $result = mysqli_query($con,$sql);
        if($rows = mysqli_fetch_assoc($result))
        {
        	$status = $rows['status'];
        	$o_status = $rows['o_status'];
        	
        	if($o_status == '0' && $status == 'cancelled')
        	{
        	    $status = 'pending';
        	    $o_status = '1';
        	    $sqlu = "UPDATE orders set o_status = '$o_status',status = '$status' where order_id = '$orderid'";
                $resultu = mysqli_query($con,$sqlu);
                if($resultu)
                {
                    $rows = array('status'=>true,'message' => 'Order Created Success');
                    echo json_encode($rows);
                }
                else
                {
                    $rows = array('status'=>false,'message' => 'Order Missing');
                    echo json_encode($rows);
                }
        	}
        	else
        	{
        	    $rows = array('status'=>false,'message' => 'Order Missing');
                echo json_encode($rows);
        	}
        }
    }
    else
    {
        $rows = array('status'=>false,'message' => 'Order Missing');
        echo json_encode($rows);
    }
}

?>