<?php
include("../db.php");
$requestData= $_REQUEST;
$columns = array( 
    0 =>  'name_pro',
    1 => 'q',
    2 => 'p_b',
    3 => 'tax',
    4 => 'p_f',
    5 => 'c',
    6 => 'name_emp',
    7 => 'date'
);

$sql = "
SELECT * FROM `sales` left join `pro` on `sales`.`id_pro` = `pro`.`id_pro`
left join `emp` on `emp`.`id_emp` = `sales`.`id_emp`
where 1
";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "
SELECT * FROM `sales` left join `pro` on `sales`.`id_pro` = `pro`.`id_pro` 
left join `emp` on `emp`.`id_emp` = `sales`.`id_emp`
where 1
";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( name_pro LIKE '".$requestData['search']['value']."%'";       
	$sql.=" AND   q LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" AND   p_b LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" AND   tax LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" AND   p_f LIKE '".$requestData['search']['value']."%'  ";       
	$sql.=" AND   c LIKE '".$requestData['search']['value']."%'  ";       
	$sql.=" AND   name_emp LIKE '".$requestData['search']['value']."%' ";       
	$sql.=" AND   `date` 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();
while( $row=mysqli_fetch_array($query)) {
    $nestedData=array(); 
	$nestedData[] = $row["name_pro"];
	$nestedData[] = $row["q"];
	$nestedData[] = $row["p_b"];
	$nestedData[] = $row["tax"];
	$nestedData[] = $row["p_f"];
	$nestedData[] = $row["c"];
	$nestedData[] = $row["name_emp"];
	$nestedData[] = $row["date"];
	$nestedData[] = "<button class='btn btn-danger' onclick='del_table(".$row['id_sales'].",4)'>حذف</button>";
	$data[] = $nestedData;
}
$json_data = array(
			"draw"            => intval( $requestData['draw']),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ),
			"data"            => $data
			);

echo json_encode($json_data);

?>



