<?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['order_id']));
    $uid =  strip_tags(mysqli_real_escape_string($con,$data['uid']));
    
    $shops = $con->query("select * from user where id='$uid'");
	if($roww = $shops->fetch_assoc())
	{
	    $shop_id = $roww['shop_id'];
        $shop_code = $roww['shop_code'];
	}
    
    $sel = $con->query("select * from orders where uid='$uid' and order_id='$id'");
    
    $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++)
        {
            $pro_id = $e[$i];
            $getimage = $con->query("select * from product where id='$pro_id' and shop_id = '$shop_id' and shop_code = '$shop_code'")->fetch_assoc();
            $p_img = $getimage['pimg'];
            $p_type = $getimage['ptype'];
            $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_type"=>$p_type,"product_image"=>$p_img,"discount"=>$getimage['discount']);
        }
        
        // if($row['status'] != 'completed' and $row['status'] != 'cancelled')
        // {
        //     $status = $row['p_method'];
        // }
        // else 
        // {
            $status =$row['status'];
        // }
        
        $p_method = $row['p_method'];
        $total =$row['total'] ;
        $odate = $row['order_date'];
        $d_time = $row['d_time'];
        $datesloat = $row['datesloat'];
        $timesloat = $row['timesloat'];        
        $tax = $row['tax'];
        $itemtotal = $row['item_total'];
        $px = $row['d_charge'];
        $tax_amount = $row['tax_amount'];
        $deli_tip = $row['deli_tip'];
        $discountamount = $row['discount_amount'];
        $couponcode = $row['coupon_code'];
        $total = $row['total'];
        $coins_use = $row['coins_use'];
        $address_id = $row['address_id'];
        $addresss = $row['address'];
        // $address_type = "Home";
        
        $add_type = $con->query("select * from address where id='$address_id'")->fetch_assoc();
        $address_type = $add_type['type'];

        $ratee = $row['rate'];
        if($ratee != 0)
        {
            $rate = 'Yes';
        }
        else 
        {
            $rate = 'No'; 
        }
        
        $shop_id = $row['shop_id'];
        $shop_data = $con->query("select * from shop_details where shop_id='$shop_id'")->fetch_assoc();
        $shop_address = $shop_data['address1'].", ".$shop_data['address2'].", ".$shop_data['area'].", ".$shop_data['city'];
        $shop_name = $shop_data['shop_name'];
        
        $delicode = $row['rid'];
        $rdata = $con->query("select * from assist_details where deli_code='$delicode'")->fetch_assoc();
        $rider_name = $rdata['name']." ".$rdata['last_name'];
        $rider_mobile = $rdata['mobile_no'];
    }
    
    $returnArr = array("productinfo"=>$k,"Sub_total"=>array_sum($ksub),"orderid"=>$id,"address"=>$addresss,"address_type"=>$address_type,
    "item_total"=>$itemtotal,"tax_amount"=>$tax_amount,"discount_amount"=>$discountamount,"coupon_code"=>$couponcode,"shop_name"=>$shop_name,"shop_address"=>$shop_address,"deli_tip"=>$deli_tip,"coins_use"=>$coins_use,
    "total_amt"=>$total,"rider_mobile"=>$rider_mobile,"rider_name"=>$rider_name,"p_method"=>$p_method,"status"=>$status,"order_date"=>$odate,"datesloat"=>$datesloat,"timesloat"=>$timesloat,
    "delivery_time"=>$d_time,"Israted"=>$rate,"d_charge"=>$px,"tax"=>$tax,"ResponseCode"=>"200","Result"=>"true","ResponseMsg"=>"Order Product Get successfully!");
}
echo json_encode($returnArr);