<?php
 ini_set('display_errors', 1);
error_reporting(E_ALL);
include('config.php');
$role="admin";
$search=$_POST['search']['value'];
$length=$_POST['length'];
$from_date=$_POST['from_date'];
$to_date=$_POST['to_date'];
$status=$_POST['status'];
$type=$_POST['type'];
$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";
}
header('Content-Type: application/json');
if($search==""){
 if($from_date==""){
 
         $sql = "SELECT count(*)as count from quotation_details where type='".$type."'";
  
 
    }else{
      
        $sql = "SELECT count(*)as count from quotation_details where type='".$type."' and  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==""){
    if($role=="admin"){
$sql_data = "SELECT * from quotation_details  where type='".$type."'  order by $order_col $dir  limit $start,$length";
    }else{
        $sql_data = "SELECT * from quotation_details  where created_by='".$created_by."' and type='".$type."'  order by $order_col $dir  limit $start,$length";
    }
 }else{
      if($role=="admin"){
     $sql_data = "SELECT * from quotation_details where type='".$type."' and  created_on>= '".$from_date."' and created_on<= '".$to_date."'  order by $order_col $dir limit $start,$length";
      }else{
           $sql_data = "SELECT * from quotation_details where created_by='".$created_by."' and type='".$type."' and  created_on>= '".$from_date."' and created_on<= '".$to_date."'  order by $order_col $dir limit $start,$length";
   
      }
 }
 
 $res_data = mysqli_query($con, $sql_data);
if (!$res_data) {
    die("Error: " . mysqli_error($con));
}


$rows = [];
while ($row = mysqli_fetch_assoc($res_data)) {
  
    foreach ($row as $key => $value) {
      
        if (!mb_check_encoding($value, 'UTF-8')) {
          
            $source_encoding = mb_detect_encoding($value, ['UTF-8', 'ISO-8859-1', 'Windows-1252']);
            if ($source_encoding) {
                $row[$key] = mb_convert_encoding($value, 'UTF-8', $source_encoding);
            }
        }
    }
    $rows[] = $row;
}

$json_string = json_encode($rows);
if (json_last_error() !== JSON_ERROR_NONE) {
    echo "JSON Error: " . json_last_error_msg();  
} else {
    // echo "JSON encoding was successful.";
}

if (count($rows) > 0) {
    echo json_encode([
        "data" => $rows, 
        "recordsTotal" => $count,
        "recordsFiltered" => $count, 
        "sql" => $sql_data 
    ]);
} else {
    echo json_encode([
        "data" => [],
        "recordsTotal" => 0, 
        "recordsFiltered" => 0,
        "sql" => $sql_data 
    ]);
}

}else{
    
    if($from_date==""){
        if($role=="admin"){
$sql = "SELECT count(*) as count from quotation_details where type='".$type."' and   (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc";
   
        }else{
            $sql = "SELECT count(*) as count from quotation_details where created_by='".$created_by."' and type='".$type."' and   (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc";

        }
        
        }else{
             if($role=="admin"){
        $sql = "SELECT count(*) as count from quotation_details where  type='".$type."' and  created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc";
             }else{
                 $sql = "SELECT count(*) as count from quotation_details where created_by='".$created_by."' and type='".$type."' and  created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on 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==""){
    if($role=="admin"){
$sql = "SELECT * from quotation_details  where type='".$type."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%') order by created_on desc limit $start, $length";
    }else{
        $sql = "SELECT * from quotation_details  where created_by='".$created_by."' and type='".$type."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%') order by created_on desc limit $start, $length";

    }

    
    }else{
         if($role=="admin"){
  $sql = "SELECT * from quotation_details  where  type='".$type."' and created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc limit $start, $length";
         }else{
             $sql = "SELECT * from quotation_details  where created_by='".$created_by."' and type='".$type."' and created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc limit $start, $length";
  
         }

         
         }
$res = mysqli_query($con,$sql);
    if (!$res)
    {
        die("Error".mysqli_error($con));
    }
 $rows = [];
while ($row = mysqli_fetch_assoc($res)) {
   
    foreach ($row as $key => $value) {
        if (!mb_check_encoding($value, 'UTF-8')) {
            $source_encoding = mb_detect_encoding($value, ['UTF-8', 'ISO-8859-1', 'Windows-1252']);
            if ($source_encoding) {
                $row[$key] = mb_convert_encoding($value, 'UTF-8', $source_encoding);
            }
        }
    }
    $rows[] = $row;
}
$length11 = count($rows);

        
if (count($rows) > 0) {
    echo json_encode([
        "data" => $rows,
        "recordsTotal" => $count,
        "recordsFiltered" => $count,
       
    ]);
} else {
    echo json_encode([
        "data" => [], 
        "recordsTotal" => 0,
        "recordsFiltered" => 0,
     
    ]);
}

}



/*include('config.php');
$search=$_POST['search']['value'];
$length=$_POST['length'];
$from_date=$_POST['from_date'];
$to_date=$_POST['to_date'];
$status=$_POST['status'];
$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 ";
 
    }else{
        $sql = "SELECT count(*)as count from quotation_details 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));
    }
   
while ( $row = mysqli_fetch_array($res,MYSQLI_ASSOC) )
{ 
$count=$row['count'];
}

$start=$_POST['start'];
if($from_date==""){
$sql = "SELECT * from quotation_details    order by $order_col $dir  limit $start,$length";
 }else{
     $sql = "SELECT * from quotation_details 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 where    (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on desc";
    }else{
        $sql = "SELECT count(*) as count from quotation_details where    created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on 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  where  (customer_name like '%".$search."%' or quotation_no like '%".$search."%') order by created_on desc limit $start, $length";
}else{
  $sql = "SELECT * from quotation_details  where   created_on>= '".$from_date."' and created_on<= '".$to_date."' and (customer_name like '%".$search."%' or quotation_no like '%".$search."%' ) order by created_on 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
        )
        
        );
}
  */
 
?>