<?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));
$name = 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 == "processing" && $name != null)
{
    $sql = "SELECT id FROM rider where name = '$name'";
    $result = mysqli_query($con,$sql);
    if($rows = mysqli_fetch_assoc($result))
    {
    	$r_id = $rows['id'];
    }
    
    if($r_id != null && $orderid != null)
    {
        $sql = "UPDATE orders set status = 'processing',rid='$r_id',r_status = 'pending',a_status = '0' where id = '$orderid'";
        $result = mysqli_query($con,$sql);
        if($result)
        {
            $rows = array('status'=>true,'message' => 'Partner Re-Assigned');
            echo json_encode($rows);
        }
        else
        {
            $rows = array('status'=>false,'message' => 'Order Missing');
            echo json_encode($rows);
        }
    }
    else
    {
        $rows = array('status'=>false,'message' => 'Partner Missing');
        echo json_encode($rows);  
    }
}
else
{
    $rows = array('status'=>false,'message' => 'Order Status Error');
    echo json_encode($rows); 
}
}

?>