<?php 
require 'db.php';
$data = json_decode(file_get_contents('php://input'), true);
if($data['uid'] == '')
{ 
    $returnArr = array("ResponseCode"=>"401","Result"=>"false","ResponseMsg"=>"Something Went Wrong!");    
}
else
{
    $id = strip_tags(mysqli_real_escape_string($con,$data['id']));
    $uid =  strip_tags(mysqli_real_escape_string($con,$data['uid']));
    $shop_id = strip_tags(mysqli_real_escape_string($con,$data['shop_id']));
    $shop_code = strip_tags(mysqli_real_escape_string($con,$data['shop_code']));
    
    $sel = $con->query("select * from orders where uid='$uid' and order_id='$id' and shop_id = '$shop_id' and shop_code = '$shop_code'");
    
    $result = array();
    $pp = array();
    while($row = $sel->fetch_assoc())
    {
        $id = $row['order_id']; 
        $a = explode('$;',$row['pname']);    
        $b =  explode('$;',$row['pprice']);
        $c = explode('$;',$row['ptype']);
        $d = explode('$;',$row['qty']);
        $e = explode('$;',$row['pid']);
        $k=array();
        $subtotal = 0;
        $ksub = array();
        for($i=0;$i<count($a);$i++)
        {
            $getimage = $con->query("select * from product where id=".$e[$i]." and shop_id = '$shop_id' and shop_code = '$shop_code'");
            $getimage = $getimage->fetch_assoc();
            $discount = $b[$i] * $getimage['discount']*$d[$i] /100;
            $ksub [] = $subtotal  + ($d[$i] * $b[$i]) - $discount;
            $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']);
        }
        
        if($row['p_method'] == 'Pickup myself' and $row['status'] != 'completed' and $row['status'] != 'cancelled')
        {
            $status = $row['p_method'];
        }
        else 
        {
            $status =$row['status'];
        }
        
        $p_method = $row['p_method'];
        $cooking_ins = $row['cooking_ins'];
        $reason = $row['comment'];
        $total =$row['total'] ;
        $odate = $row['order_date'];
        $timesloat = $row['timesloat'];
        $d_time = $row['d_time'];
        $tax = $row['tax'];
        $itemtotal = $row['item_total'];
        $tax_amount = $row['tax_amount'];
        $discountamount = $row['discount_amount'];
        $couponcode = $row['coupon_code'];
        $address_id = $row['address_id'];
        $addresss = $row['address'];
        if($p_method == 'Pickup Myself')
        {
            $px = 0;
        }
        else 
        {
            $px = $row['d_charge'];
        }
        
        $ratee = $row['rate'];
        if($ratee != 0)
        {
        $rate = 'Yes';
        }
        else 
        {
        $rate = 'No'; 
        }
        
        $delicode = $row['rid'];
        $rdata = $con->query("select * from assist_details where deli_code='$delicode'")->fetch_assoc();
        $rider_name = $rdata['name'];
        $rider_mobile = $rdata['mobile_no'];
    }
    
    $returnArr = array("productinfo"=>$k,"Sub_total"=>array_sum($ksub),"orderid"=>$id,"address"=>$addresss,"address_type"=>$atype,"item_total"=>$itemtotal,"tax_amount"=>$tax_amount,"discount_amount"=>$discountamount,"coupon_code"=>$couponcode,"customer_name"=>$cname,"total_amt"=>$total,"rider_mobile"=>$rider_mobile,"rider_name"=>$rider_name,"p_method"=>$p_method,"status"=>$status,"order_date"=>$odate,"timesloat"=>$timesloat,"delivery_time"=>$d_time,"Israted"=>$rate,"d_charge"=>$px,"cooking_ins"=>$cooking_ins,"reason"=>$reason,"tax"=>$tax,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Product Get successfully!");
}
echo json_encode($returnArr);