<?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'";
$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['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'];
	$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'";
$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['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'];
	$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 = 'processing'";
$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['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['rider_status'] = $rows['r_status'];
	$rt =  $con->query("select name from rider where id=".$rows['rid']."");
	if($row = $rt->fetch_assoc())
	$p['rider_name'] = $row['name'];
	$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'";
$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['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'];
	$rt =  $con->query("select name from rider where id=".$rows['rid']."");
	if($row = $rt->fetch_assoc())
	$p['rider_name'] = $row['name'];
	$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;
}

$rows = array('pending orders'=>$pending_orders,'preparing orders'=>$preparing_orders,'processing orders'=>$processing_orders,'completed orders'=>$completed_orders);
echo json_encode($rows);
}

?>