<?php
include('config.php');
$search=$_POST['search']['value'];
$length=$_POST['length'];
$from_date=$_POST['from_date'];
$to_date=$_POST['to_date'];
$status="honoured";
if($search==""){
 if($from_date==""){
       $sql = "SELECT count(*)as count from pdc_details where status='".$status."'   order by issued_date desc";
 
    }else{
        $sql = "SELECT count(*)as count from pdc_details where status='".$status."' and issued_date>= '".$from_date."' and issued_date<= '".$to_date."'  order by issued_date desc";

    }$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
$sql = "SELECT * from pdc_details where status='".$status."'  order by issued_date desc limit $start,$length";
 }else{
     $sql = "SELECT * from pdc_details where status='".$status."' and issued_date>= '".$from_date."' and issued_date<= '".$to_date."'  order by issued_date desc limit $start,$length";

 }//echo $sql;
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}else{
    
    if($from_date==""){ 
$sql = "SELECT count(*) as count from pdc_details where  status='".$status."' and (customer_name like '%".$search."%' or check_no like '%".$search."%') order by issued_date desc";
    }else{
        $sql = "SELECT count(*) as count from pdc_details where  status='".$status."'  and (customer_name like '%".$search."%' or check_no like '%".$search."%') order by issued_date desc";

    }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
   if($from_date==""){ 
$sql = "SELECT * from pdc_details  where status='".$status."' and  (customer_name like '%".$search."%' or check_no like '%".$search."%') order by issued_date desc limit $start, $length";
   }else{
    $sql = "SELECT * from pdc_details  where status='".$status."' and  issued_date>= '".$from_date."' and issued_date<= '".$to_date."' and (customer_name like '%".$search."%' or check_no like '%".$search."%') order by issued_date desc limit $start, $length";
   
   }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQL_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}
?>