<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);

if($data['orderid'] == '')
{
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    // $orderid = "95523324";
	$orderid = $data['orderid'];
	$pol= array();
	
	$olist = $con->query("select * from orders where order_id='$orderid'")->fetch_assoc();
	
	$shop_id = $olist['shop_id'];
	$shop_code = $olist['shop_code'];
	$r_id = $olist['rid'];
	
	$restdata = $con->query("select * from shop_details where shop_id ='$shop_id' and shop_code='$shop_code'")->fetch_assoc();
	$pol['orderid'] = 'ORDER #'.$orderid;
	$pol['rest_name'] = $restdata['shop_name'];
	$pol['rest_img'] = $restdata['shop_logo'];
	$pol['rest_lats'] = $restdata['latitude'];
	$pol['rest_mobile'] = $restdata['manager_mobile'];
	$pol['rest_longs'] = $restdata['longitude'];
	$timestamp = date("Y-m-d H:i:s");
	$diff = strtotime($timestamp) - strtotime($olist['order_date']);
	$pol['order_arrive_seconds'] = $diff;
	$counter = $con->query("select * from orders where order_id=".$orderid."")->num_rows;
	$date_time = $olist['datesloat']." ".$olist['timesloat'];
	if($counter == 1)
	{
	    $pol['arrive_time'] = $date_time.', ₹'.$olist['total'];
	}
	else 
	{
		$pol['arrive_time'] = $date_time.', ₹'.$olist['total'];
	}

	
	if(strlen($r_id) != 1)
	{
		$riderdata = $con->query("select * from assist_details where deli_code='$r_id'")->fetch_assoc();
	
    	$pol['rider_name'] = $riderdata['name'].' '.$riderdata['last_name'];
    	$pol['rider_img'] = $riderdata['selfie_photo'];
		$pol['rider_lats'] = $riderdata['latitude'];
		$pol['rider_longs'] = $riderdata['longitude'];
		$pol['rider_mobile'] = $riderdata['mobile_no'];
	}
	else 
	{
		$pol['rider_name'] = '';
		$pol['rider_img'] = '';
		$pol['rider_lats'] = 0.0;
		$pol['rider_longs'] = 0.0;
		$pol['rider_mobile'] = '';
	}
	
	if($olist['status'] == 'pending' and $olist['o_status'] == '1')
	{
		$pol['order_step'] = 1; 
		$pol['rest_msg'] = 'Waiting For Restaurant Decision.';
		$pol['rider_msg'] = '';
	}
	else if($olist['status'] == 'pending' and $olist['o_status'] == '0')
	{
		$pol['order_step'] = 1; 
		$pol['rest_msg'] = 'Waiting For Restaurant Decision.';
		$pol['rider_msg'] = '';
	}
	else if($olist['status'] == 'preparing' and $olist['o_status'] == '1')
	{
		$pol['rest_msg'] = 'Is Preparing Your Order.';
		$pol['rider_msg'] = '';
		$pol['order_step'] = 2; 
	}
	else if($olist['status'] == 'preparing' and $olist['o_status'] == '2')
	{
		$pol['rest_msg'] = 'Is Preparing Your Order.';
		$pol['rider_msg'] = '';
		$pol['order_step'] = 2; 
	}
	else if($olist['status'] == 'processing' and $olist['o_status'] == '3' and $olist['r_reach'] == '0')
	{
		$pol['order_step'] = 3; 
		$pol['rider_msg'] = 'Is Assigned As Your Delivery Assist.';
		$pol['rest_msg'] = 'Is Preparing Your Order.';
	}
	else if($olist['status'] ==  'processing' and $olist['o_status'] == '3'  and $olist['r_reach'] == '1')
	{
		$pol['order_step'] = 4; 
		$pol['rider_msg'] = 'is on the way to deliver your order';
		$pol['rest_msg'] = '';
	}
	else if($olist['status'] == 'completed' and $olist['o_status'] == '4')
	{
		$pol['order_step'] = 5; 
		$pol['rider_msg'] = 'Your Order Delivered Successfully.';
		$pol['rest_msg'] = '';
	}
	else if($olist['o_status'] == 'cancelled' and $olist['o_status'] == '0')
	{
		$pol['order_step'] = 6; 
		$pol['rest_msg'] = 'Your Order Was Cancelled.';
		$pol['rider_msg'] = '';
	}
	
	
	$pol['cust_address_type'] = "Home";
	$pol['cust_address_lat'] = $olist['latitude'];
	$pol['cust_address_long'] = $olist['longitude'];
	$returnArr = array("Mapinfo"=>$pol,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Information  Get Successfully!!!");
}
echo json_encode($returnArr);
?>