<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    $shop_id = $_GET['sid'];
    $shop_code = $_GET['token'];

    include 'token_check.php';
    
    if($token_check)
    {
        $sql = "SELECT * FROM orders where status = 'completed' and shop_id='$shop_id' and shop_code='$shop_code' order by order_date DESC,id DESC";
        $result = mysqli_query($con,$sql);
        while($rows = mysqli_fetch_assoc($result))
        {
            $cat =  $con->query("select name,mobile from user where id=".$rows['uid']."");
        	if($row = $cat->fetch_assoc())
        	$p['name'] = $row['name'];
        	$p['mobile'] = $row['mobile'];
        	$p['date'] = $rows['order_date'];
        	$p['time'] = $rows['timesloat'];
        	$p['order_id'] = $rows['order_id'];
        	$p['pay_mode'] = $rows['p_method'];
        	$p['product_name'] = $rows['pname'];
        	$p['product_price'] = $rows['pprice'];
        	$p['product_type'] = $rows['ptype'];
        	$p['product_qty'] = $rows['qty'];
        	$p['total_amount'] = $rows['total'];
        	$p['cooking'] = $rows['cooking_ins'];
        	$p['d_date'] = $rows['d_date'];
        	$p['d_time'] = $rows['d_time'];
        	$p['rider_status'] = $rows['r_status'];
        	$rid = $rows['rid'];
        	$rt =  $con->query("select name from assist_details where deli_code='$rid'");
        	if($row = $rt->fetch_assoc())
        	$p['rider_name'] = $row['name'];
        	$p['address'] = $rows['address'];
            $completed_orders[] = $p;
        }
        
        $sql = "SELECT * FROM orders where status = 'cancelled' and shop_id='$shop_id' and shop_code='$shop_code' order by id DESC";
        $result = mysqli_query($con,$sql);
        while($rows = mysqli_fetch_assoc($result))
        {
            $cat =  $con->query("select name,mobile from user where id=".$rows['uid']."");
        	if($row = $cat->fetch_assoc())
        	$p1['name'] = $row['name'];
        	$p1['mobile'] = $row['mobile'];
        	$p1['date'] = $rows['order_date'];
        	$p1['time'] = $rows['timesloat'];
        	$p1['order_id'] = $rows['order_id'];
        	$p1['pay_mode'] = $rows['p_method'];
        	$p1['product_name'] = $rows['pname'];
        	$p1['product_price'] = $rows['pprice'];
        	$p1['product_type'] = $rows['ptype'];
        	$p1['product_qty'] = $rows['qty'];
        	$p1['total_amount'] = $rows['total'];
        	$p1['cooking'] = $rows['cooking_ins'];
        	$p1['d_date'] = $rows['d_date'];
        	$p1['d_time'] = $rows['d_time'];
        	$p1['rider_status'] = $rows['r_status'];
            $rid = $rows['rid'];
        	$rt =  $con->query("select name from assist_details where deli_code='$rid'");
        	if($row = $rt->fetch_assoc())
        	$p1['rider_name'] = $row['name'];
        	$p1['address'] = $rows['address'];
            $cancelled_orders[] = $p1;
        }
        
        $sql2 = "SELECT * FROM orders where status NOT IN ('cancelled','completed') and shop_id='$shop_id' and shop_code='$shop_code' order by id DESC";
        $result2 = mysqli_query($con,$sql2);
        while($rows2 = mysqli_fetch_assoc($result2))
        {
            $cat2 =  $con->query("select name,mobile from user where id=".$rows2['uid']."");
        	if($row2 = $cat2->fetch_assoc())
        	$p12['name'] = $row2['name'];
        	$p12['mobile'] = $row2['mobile'];
        	$p12['date'] = $rows2['order_date'];
        	$p12['time'] = $rows2['timesloat'];
        	$p12['order_id'] = $rows2['order_id'];
        	$p12['pay_mode'] = $rows2['p_method'];
        	$p12['product_name'] = $rows2['pname'];
        	$p12['product_price'] = $rows2['pprice'];
        	$p12['product_type'] = $rows2['ptype'];
        	$p12['product_qty'] = $rows2['qty'];
        	$p12['total_amount'] = $rows2['total'];
        	$p12['cooking'] = $rows2['cooking_ins'];
        	$p12['d_date'] = $rows2['d_date'];
        	$p12['d_time'] = $rows2['d_time'];
        	$p12['rider_status'] = $rows2['r_status'];
            $rid = $rows2['rid'];
        	$rt =  $con->query("select name from assist_details where deli_code='$rid'");
        	if($row2 = $rt->fetch_assoc())
        	$p12['rider_name'] = $row2['name'];
        	$p12['address'] = $rows2['address'];
            $pending_orders[] = $p12;
        }
        
        
        $rows = array('completed orders'=>$completed_orders,'cancelled orders'=>$cancelled_orders,'pending orders'=>$pending_orders);
        echo json_encode($rows);
    }
    else
    {
        $rows = array('status'=>"",'message' => 'Data Error');
        echo json_encode($rows);
    }
}

?>