<?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));
// $date = '2021-08-23';

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'pending' and o_status = '0' or order_date = '$date' and status = 'pending' and o_status = '1'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$p['address'] = $rows['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;
}

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'preparing' and o_status = '1'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$p['address'] = $rows['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;
}

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'preparing' and o_status = '2'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$p['address'] = $rows['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'];
    $ready_orders[] = $p;
}

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'processing' and o_status = '3'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$p['rider_status'] = $rows['r_status'];
	$rid = $rows['rid'];
	$rt =  $con->query("select name from assist_details where deli_code='$rid'");
	if($row2 = $rt->fetch_assoc())
	$p['rider_name'] = $row2['name'];
	$p['address'] = $rows['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;
}

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'completed' and o_status = '4'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$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,mobile_no from assist_details where deli_code='$rid'");
	if($row2 = $rt->fetch_assoc())
	$p['rider_name'] = $row2['name']." (".$row2['mobile_no'].")";
	$p['address'] = $rows['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;
}

$sql = "SELECT * FROM orders where order_date = '$date' and status = 'cancelled' and o_status = '0'";
$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'];
	}
	
	$cat =  $con->query("select shop_name,owner_mobile,manager_mobile from shop_details where shop_id=".$rows['shop_id']."");
	if($ro = $cat->fetch_assoc())
	{
	    $p['shop_name'] = $ro['shop_name'];
	    $p['owner_mobile'] = $ro['owner_mobile'];
	    $p['manager_mobile'] = $ro['manager_mobile'];
	}
	$p['shop_id'] = $rows['shop_id'];
	$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['tax_amount'] = $rows['tax_amount'];
	$p['discount'] = $rows['discount_amount'];
	$p['cooking'] = $rows['cooking_ins'];
	$p['item_total'] = $rows['item_total'];
	$p['d_charge'] = $rows['d_charge'];
	$p['coins_use'] = $rows['coins_use'];
	$p['deli_tip'] = $rows['deli_tip'];
	$p['comment'] = $rows['comment'];
	$p['address'] = $rows['address'];
	$p['seen'] = $rows['seen'];
// 	$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'];
    $cancelled_orders[] = $p;
}

$sels = $con->query("select count(*) as total from assist_settings where status = '1'");
while($rows = $sels->fetch_assoc())
{
    $total = $rows['total'];
}

$rows = array('pending orders'=>$pending_orders,'preparing orders'=>$preparing_orders,'ready orders'=>$ready_orders,'cancelled orders'=>$cancelled_orders,'processing orders'=>$processing_orders,'completed orders'=>$completed_orders,'total_assist'=>$total);
echo json_encode($rows);
}

?>