<?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 ='completed' and o_status = '4' order by id desc");
  
  if($sel->num_rows != 0)
  {
  $result = array();
  $pp = array();
  while($row = $sel->fetch_assoc())
    {
    $oid = $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();
    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();
	
	$distance = $row['distance'];
	$total_earn = 0.00;

    if($distance < 3)
    {
        $total_earn = $total_earn + 15;
    }
    else if($distance >= 3 && $distance < 4)
    {
        $total_earn = $total_earn + 16.50;
    }
    else if($distance >= 4 && $distance < 5)
    {
        $total_earn = $total_earn + 24;
    }
    else if($distance >= 5 && $distance < 6)
    {
        $total_earn = $total_earn + 30;
    }
    else if($distance >= 6 && $distance < 7)
    {
        $total_earn = $total_earn + 37.80;
    }
    else if($distance >= 7 && $distance < 8)
    {
        $total_earn = $total_earn + 46.90;
    }
    else if($distance >= 8 && $distance < 9)
    {
        $total_earn = $total_earn + 56;
    }
    else if($distance >= 9 && $distance < 10)
    {
        $total_earn = $total_earn + 65.70;
    }
    else if($distance >= 10 && $distance < 11)
    {
        $total_earn = $total_earn + 75;
    }
    
    $result['distance'] = $distance;
    $result['total_earn'] = number_format((float)$total_earn, 2, '.', '');
    
    $result['shop_name'] = $c['shop_name'];
			
	$result['d_charge'] = $row['d_charge'];
	$result['p_method'] = $row['p_method'];
    $result['total'] =$row['total'] ;
    $result['odate'] = $row['order_date'];
	$result['orderid'] = $row['order_id'];
    $result['timesloat'] = $row['timesloat'];
    $result['d_time'] = $row['d_time'];
	$result['delivery'] = $row['address']; 
	$result['mobile'] = $cc['mobile'];
	$result['name'] = $cc['name'];

	$pp[] = $result;
    }
            
    $returnArr = array("complete_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);