<?php
 ini_set('display_errors', 1);
error_reporting(E_ALL);
include('config.php');
//$role=$_SESSION['role'];
//$created_by=$_SESSION['mbs_name'];
//if($role==""||$role==null ){
//    die("session expired");
//}
$search=$_POST['search']['value'];
$length=$_POST['length'];
$from_date="";
$to_date="";

$o=$_POST['order'][0]['column'];
$dir=$_POST['order'][0]['dir'];
//echo $_POST['columns'][$o]['data'];die();
$order_col=$_POST['columns'][$o]['data'];
if($order_col=="id"){
    $dir="desc";
}
if($search==""){
 if($from_date==""){
     $sql = "SELECT count(*)as count from quotation_details_all";
 
    }else{
         $sql = "SELECT count(*)as count from quotation_details_all where created_on>= '".$from_date."' and created_on<= '".$to_date."'  order by created_on desc";
       
    }$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   //print_r($res);
while ( $row = mysqli_fetch_array($res,MYSQLI_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
   $sql = "SELECT * from quotation_details_all  order by $order_col $dir  limit $start,$length";
 }else{
      $sql = "SELECT * from quotation_details_all where   created_on>= '".$from_date."' and created_on<= '".$to_date."'  order by $order_col $dir 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,MYSQLI_ASSOC) )
{ 
    
array_push($data,$row);
}

echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count,
            "sql"=>$sql
        )
        
        );
}else{
    
    if($from_date==""){
       $sql = "SELECT count(*) as count from quotation_details_all where  (product_name like '%".$search."%'  ) order by id desc";

        
        }else{
             
             $sql = "SELECT count(*) as count from quotation_details_all where  (product_name like '%".$search."%'  ) order by id desc";
        
    }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQLI_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
   $sql = "SELECT * from quotation_details_all  where (product_name like '%".$search."%') order by id desc limit $start, $length";

    
    }else{
          $sql = "SELECT * from quotation_details_all  where  (product_name like '%".$search."%' ) order by id desc limit $start, $length";
  

         
         }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
   $data=array();
while ( $row = mysqli_fetch_array($res,MYSQLI_ASSOC) )
{ 
array_push($data,$row);
}
echo json_encode(
        array(
            "data"=>$data,
        "recordsTotal"=>$count ,
            "recordsFiltered"=>$count
        )
        
        );
}
?>