<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{

$date = mysqli_real_escape_string($con, trim($request->value));

if(isset($_GET['sid']) && isset($_GET['token']))
{
    $shop_id = $_GET['sid'];
    $shop_code = $_GET['token'];

    include 'token_check.php';
    
    if($token_check)
    {
        // $date = '2021-08-23';
        
        $sql1 = "SELECT * FROM orders where order_date = '$date' and status = 'pending' and o_status= '0' and shop_id='$shop_id' and shop_code='$shop_code' or o_status= '1' and order_date = '$date' and status = 'pending' and shop_id='$shop_id' and shop_code='$shop_code' order by id DESC";
        $result1 = mysqli_query($con,$sql1);
        while($rows1 = mysqli_fetch_assoc($result1))
        {
            $uid = $rows1['uid'];
            $cat =  $con->query("select name,mobile from user where id='$uid'");
        	if($row1 = $cat->fetch_assoc())
        	$p['name'] = $row1['name'];
        	$p['mobile'] = $row1['mobile'];
        	$p['date'] = $rows1['order_date'];
        	$p['time'] = $rows1['timesloat'];
        	$p['order_id'] = $rows1['order_id'];
        	$p['pay_mode'] = $rows1['p_method'];
        	$p['product_name'] = $rows1['pname'];
        	$p['product_price'] = $rows1['pprice'];
        	$p['product_type'] = $rows1['ptype'];
        	$p['product_qty'] = $rows1['qty'];
        	$p['total_amount'] = $rows1['total'];
        	$p['tax_amount'] = $rows1['tax_amount'];
        	$p['discount'] = $rows1['discount_amount'];
        	$p['item_total'] = $rows1['item_total'];
        	$p['cooking'] = $rows1['cooking_ins'];
        	$p['address'] = $rows1['address'];
        // 	$ad =  $con->query("select * from address where id=".$rows['address_id']."");
        // 	if($row1 = $ad->fetch_assoc())
        // 	$p['address'] = $row1['hno'].','.$row1['society'].','.$row1['landmark'].','.$row1['area'].','.$row1['pincode'];
            $pending_orders[] = $p;
        }
        
        $sql2 = "SELECT * FROM orders where o_status= '1' and order_date = '$date' and status = 'preparing' and shop_id='$shop_id' and shop_code='$shop_code' or o_status= '2' and order_date = '$date' and status = 'preparing' 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))
        {
            $uid = $rows2['uid'];
            $cat =  $con->query("select name,mobile from user where id='$uid'");
        	if($row2 = $cat->fetch_assoc())
        	$p['name'] = $row2['name'];
        	$p['mobile'] = $row2['mobile'];
        	$p['date'] = $rows2['order_date'];
        	$p['time'] = $rows2['timesloat'];
        	$p['order_id'] = $rows2['order_id'];
        	$p['otp'] = $rows2['r_otp'];
        	$p['o_status'] = $rows2['o_status'];
        	$p['pay_mode'] = $rows2['p_method'];
        	$p['product_name'] = $rows2['pname'];
        	$p['product_price'] = $rows2['pprice'];
        	$p['product_type'] = $rows2['ptype'];
        	$p['product_qty'] = $rows2['qty'];
        	$p['total_amount'] = $rows2['total'];
        	$p['tax_amount'] = $rows2['tax_amount'];
        	$p['discount'] = $rows2['discount_amount'];
        	$p['item_total'] = $rows2['item_total'];
        	$p['cooking'] = $rows2['cooking_ins'];
        	$p['address'] = $rows2['address'];
        // 	$ad =  $con->query("select * from address where id=".$rows['address_id']."");
        // 	if($row1 = $ad->fetch_assoc())
        // 	$p['address'] = $row1['hno'].','.$row1['society'].','.$row1['landmark'].','.$row1['area'].','.$row1['pincode'];
            $preparing_orders[] = $p;
        }
        
        $sql3 = "SELECT * FROM orders where order_date = '$date' and status = 'processing' and o_status= '3' and shop_id='$shop_id' and shop_code='$shop_code' order by id DESC ";
        $result3 = mysqli_query($con,$sql3);
        while($rows3 = mysqli_fetch_assoc($result3))
        {
            $uid = $rows3['uid'];
            $cat =  $con->query("select name,mobile from user where id='$uid'");
        	if($row3 = $cat->fetch_assoc())
        	$p['name'] = $row3['name'];
        	$p['mobile'] = $row3['mobile'];
        	$p['date'] = $rows3['order_date'];
        	$p['time'] = $rows3['timesloat'];
        	$p['otp'] = $rows3['r_otp'];
        	$p['o_status'] = $rows3['o_status'];
        	$p['order_id'] = $rows3['order_id'];
        	$p['pay_mode'] = $rows3['p_method'];
        	$p['product_name'] = $rows3['pname'];
        	$p['product_price'] = $rows3['pprice'];
        	$p['product_type'] = $rows3['ptype'];
        	$p['product_qty'] = $rows3['qty'];
        	$p['total_amount'] = $rows3['total'];
        	$p['tax_amount'] = $rows3['tax_amount'];
        	$p['discount'] = $rows3['discount_amount'];
        	$p['item_total'] = $rows3['item_total'];
        	$p['cooking'] = $rows3['cooking_ins'];
        	$p['rider_status'] = $rows3['r_status'];
            $rid = $rows3['rid'];
        	$rt =  $con->query("select name,mobile_no from assist_details where deli_code='$rid'");
        	if($row23 = $rt->fetch_assoc())
        	$p['rider_name'] = $row23['name']." (".$row23['mobile_no'].")";
        	$p['address'] = $rows3['address'];
        // 	$ad =  $con->query("select * from address where id=".$rows['address_id']."");
        // 	if($row1 = $ad->fetch_assoc())
        // 	$p['address'] = $row1['hno'].','.$row1['society'].','.$row1['landmark'].','.$row1['area'].','.$row1['pincode'];
            $processing_orders[] = $p;
        }
        
        $sql4 = "SELECT * FROM orders where order_date = '$date' and status = 'completed' and o_status= '4' and shop_id='$shop_id' and shop_code='$shop_code' order by id DESC";
        $result4 = mysqli_query($con,$sql4);
        while($rows4 = mysqli_fetch_assoc($result4))
        {
            $uid = $rows4['uid'];
            $cat =  $con->query("select name,mobile from user where id='$uid'");
        	if($row4 = $cat->fetch_assoc())
        	$p['name'] = $row4['name'];
        	$p['mobile'] = $row4['mobile'];
        	$p['date'] = $rows4['order_date'];
        	$p['time'] = $rows4['timesloat'];
        	$p['order_id'] = $rows4['order_id'];
        	$p['otp'] = $rows4['r_otp'];
        	$p['pay_mode'] = $rows4['p_method'];
        	$p['product_name'] = $rows4['pname'];
        	$p['product_price'] = $rows4['pprice'];
        	$p['product_type'] = $rows4['ptype'];
        	$p['product_qty'] = $rows4['qty'];
        	$p['total_amount'] = $rows4['total'];
        	$p['tax_amount'] = $rows4['tax_amount'];
        	$p['discount'] = $rows4['discount_amount'];
        	$p['item_total'] = $rows4['item_total'];
        	$p['cooking'] = $rows4['cooking_ins'];
        	$p['d_date'] = $rows4['d_date'];
        	$p['d_time'] = $rows4['d_time'];
        	$p['rider_status'] = $rows4['r_status'];
            $rid = $rows4['rid'];
        	$rt =  $con->query("select name,mobile_no from assist_details where deli_code='$rid'");
        	if($row24 = $rt->fetch_assoc())
        	$p['rider_name'] = $row24['name']." (".$row24['mobile_no'].")";
        	$p['address'] = $rows4['address'];
        // 	$ad =  $con->query("select * from address where id=".$rows['address_id']."");
        // 	if($row1 = $ad->fetch_assoc())
        // 	$p['address'] = $row1['hno'].','.$row1['society'].','.$row1['landmark'].','.$row1['area'].','.$row1['pincode'];
            $completed_orders[] = $p;
        }
        
        $sels = $con->query("SELECT 
                shop_details.expiry_date,
                shop_settings.shop_status
                FROM shop_settings
                INNER JOIN shop_details ON
                shop_settings.shop_id = shop_details.shop_id and
                shop_settings.shop_code = shop_details.shop_code
                WHERE shop_settings.shop_id = '$shop_id' and shop_settings.shop_code = '$shop_code'");
        while($row = $sels->fetch_assoc())
        {
            $status = $row['shop_status'];
            $expiredate = $row['expiry_date'];
        }
        
        $rowss = array('pending orders'=>$pending_orders,'preparing orders'=>$preparing_orders,'processing orders'=>$processing_orders,'completed orders'=>$completed_orders,'shopstatus'=>$status,'expiry_date'=>$expiredate);
        echo json_encode($rowss);
    }
    else
    {
        $rowss = array('status'=>"",'message' => 'Data Error');
        echo json_encode($rowss);
    }
}

}


?>