<?php

require 'db.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{
    
$startdate = mysqli_real_escape_string($con, trim($request->value->start_date));
$enddate = mysqli_real_escape_string($con, trim($request->value->end_date));
$mode = mysqli_real_escape_string($con, trim($request->value->mode));

$date = str_replace('/', '-', $startdate);
$new_start_date = date('Y-m-d', strtotime($date));
$date1 = str_replace('/', '-', $enddate);
$new_end_date = date('Y-m-d', strtotime($date1));

if($mode == 'daily')
{
    $i=0;
    $sels = $con->query("select uid,p_method,total,datesloat from orders where status = 'completed' and order_date ='".$new_start_date."'");
    while($rows = $sels->fetch_assoc())
    {
        $cat =  $con->query("select name from user where id=".$rows['uid']);
    	if($row = $cat->fetch_assoc())
    	{
    	    $p['name'] = $row['name'];
    	}
    	$p['s_no'] = $i+1;
    	$p['order_date'] = $rows['datesloat'];
    	$p['pay_mode'] = $rows['p_method'];
    	$p['amount'] = $rows['total'];
    	$cp[] = $p;
		$i++;
    }
    $rows = array('data'=>$cp,'status'=>true);
    echo json_encode($rows);
}
else if($mode == 'weekly')
{
    $i=0;
    $sels = $con->query("select uid,p_method,total,datesloat from orders where status = 'completed' and order_date BETWEEN '".$new_start_date."' AND '".$new_end_date."'");
    while($rows = $sels->fetch_assoc())
    {
        $cat =  $con->query("select name from user where id=".$rows['uid']);
    	if($row = $cat->fetch_assoc())
    	{
    	    $p['name'] = $row['name'];
    	}
    	$p['s_no'] = $i+1;
    	$p['order_date'] = $rows['datesloat'];
    	$p['pay_mode'] = $rows['p_method'];
    	$p['amount'] = $rows['total'];
    	$cp[] = $p;
		$i++;
    }
    $rows = array('data'=>$cp,'status'=>true);
    echo json_encode($rows);
}
else if($mode == 'monthly')
{
    $i=0;
    $sels = $con->query("select uid,p_method,total,datesloat from orders where status = 'completed' and order_date BETWEEN '".$new_start_date."' AND '".$new_end_date."'");
    while($rows = $sels->fetch_assoc())
    {
        $cat =  $con->query("select name from user where id=".$rows['uid']);
    	if($row = $cat->fetch_assoc())
    	{
    	    $p['name'] = $row['name'];
    	}
    	$p['s_no'] = $i+1;
    	$p['order_date'] = $rows['datesloat'];
    	$p['pay_mode'] = $rows['p_method'];
    	$p['amount'] = $rows['total'];
    	$cp[] = $p;
    	$i++;
    }
    $rows = array('data'=>$cp,'status'=>true);
    echo json_encode($rows);
}
else if($mode == 'custom')
{
    $i=0;
    $sels = $con->query("select uid,p_method,total,datesloat from orders where status = 'completed' and order_date BETWEEN '".$new_start_date."' AND '".$new_end_date."'");
    while($rows = $sels->fetch_assoc())
    {
        $cat =  $con->query("select name from user where id=".$rows['uid']);
    	if($row = $cat->fetch_assoc())
    	{
    	    $p['name'] = $row['name'];
    	}
    	$p['s_no'] = $i+1;
    	$p['order_date'] = $rows['datesloat'];
    	$p['pay_mode'] = $rows['p_method'];
    	$p['amount'] = $rows['total'];
    	$cp[] = $p;
		$i++;
    }
    $rows = array('data'=>$cp,'status'=>true);
    echo json_encode($rows);
}
else
{
    $rows = array('data'=>"Error",'status'=>false);
    echo json_encode($rows);
}
}
else
{
    $rows = array('data'=>"",'status'=>false);
    echo json_encode($rows);
}

?>