<?php
include("../db.php");
$requestData= $_REQUEST;
$s = $_POST['s'];
$e = $_POST['e'];
$columns = array( 
	0 =>  'name_cus',
	1 =>  'name_emp',
    2 =>  'value_discount',
    3 =>  'comment_discount',
    4 =>  'date_discount'
);

$sql = "
SELECT * FROM `dscount_cus` 

left join `emp` on `emp`.`id_emp` = `dscount_cus`.`id_user`  
left join `cus` on `cus`.`id_cus` = `dscount_cus`.`id_cus`

where `dscount_cus`.`date_discount` BETWEEN '$s' and '$e'
";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "
SELECT * FROM `dscount_cus` 

left join `emp` on `emp`.`id_emp` = `dscount_cus`.`id_user`  
left join `cus` on `cus`.`id_cus` = `dscount_cus`.`id_cus`

where `dscount_cus`.`date_discount` BETWEEN '$s' and '$e'
";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( name_emp LIKE '".$requestData['search']['value']."%'  ";
	$sql.=" OR name_cus LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR value_discount LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR date_discount LIKE '".$requestData['search']['value']."%' )";    
}
$query=mysqli_query($db, $sql);
$totalFiltered = mysqli_num_rows($query); 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$query=mysqli_query($db, $sql);
$data = array();
$i = 0;
while( $row=mysqli_fetch_array($query)) {
	$nestedData=array(); 
    $i = $i + 1;
	$nestedData[] =  $i;
	$nestedData[] = $row["name_cus"];
	$nestedData[] = $row["id_scanner"];
	$nestedData[] = $row["value_discount"];
	$nestedData[] = $row["comment_discount"];
	$nestedData[] = $row["name_emp"];
	$nestedData[] = $row["date_discount"];

	
	$data[] = $nestedData;
}



$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);

?>



