<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['deli_code'] == '')
{ 
 $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    $rid =  strip_tags(mysqli_real_escape_string($con,$data['deli_code']));
    $sel = $con->query("select * from orders where rid='$rid' and status ='processing' and o_status='3' and r_status <> 'undelivered' order by id desc");
    if($sel->num_rows != 0)
    {
        $result = array();
        $pp = array();
        while($row = $sel->fetch_assoc())
        {
            $a = explode('$;',$row['pname']);    
            $b =  explode('$;',$row['pprice']);
            $c = explode('$;',$row['ptype']);
            $d = explode('$;',$row['qty']);
            $e = explode('$;',$row['pid']);
            $k=array();
            for($i=0;$i<count($a);$i++)
            {
                $getimage = $con->query("select * from product where id=".$e[$i]."");
                $getimage = $getimage->fetch_assoc();
                $k[$i] = array("product_name"=>$a[$i],"product_price"=>number_format((float)$b[$i], 2, '.', ''),"product_weight"=>$c[$i],"product_qty"=>$d[$i],"product_image"=>$getimage['pimg'],"discount"=>$getimage['discount']);
            }
            $result['productinfo'] = $k;
            
            if($row['p_method'] == 'Pickup myself' and $row['status'] != 'completed' and $row['status'] != 'cancelled')
            {
                $status = $row['p_method'];
            }
            else 
            {
                $status = $row['status'];
            }
            $result['status'] = $status;
            $shop_id = $row['shop_id'];
        	$user_id = $row['uid'];
            $c = $con->query("select * from shop_details where shop_id='$shop_id'");
            $c = $c->fetch_assoc();
            $cc = $con->query("select * from user where id='$user_id'");
            $cc = $cc->fetch_assoc();
            		
            $result['p_method'] = $row['p_method'];
            $result['otp'] = $row['r_otp'] ;
            $result['total'] =$row['total'] ;
            $result['odate'] = $row['order_date'];
            $result['orderid'] = $row['order_id'];
            $result['timesloat'] = $row['timesloat'];
	        $result['delivery'] = $row['address']; 
            $result['astatus'] = $row['r_status'];
            $result['reach'] = $row['r_reach'];
            $result['mobile'] = $cc['mobile'];
            $result['name'] = $cc['name'];
            $result['shop_name'] = $c['shop_name'];
            $result['shop_latitude'] = $c['latitude'];
            $result['shop_longitude'] = $c['longitude'];
            $result['user_latitude'] = $row['latitude'];
            $result['user_longitude'] = $row['longitude'];
            $pp[] = $result;
        }
        $returnArr = array("order_data"=>$pp,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Get successfully!");
    }
    else 
    {
        $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"No Pending Order Found!");   
    }
}
echo json_encode($returnArr);