<?php
include("../db.php");
$i = $_POST['i'];
$o = $_POST['o'];
$u = $_POST['u'];
global $i,$o,$u;
if($i == 0)
{
	$s = "none";
}else{
	$s = "";
}
if($o == 0)
{
	$s1 = "none";
}else{
	$s1 = "";
}
if($u == 0)
{
	$s2 = "none";
}else{
	$s2 = "";
}
$requestData= $_REQUEST;
$columns = array( 
	0 =>  'id_pro',
	1 => 'name_pro',
	2 => 'price_pro',
	3 => 'stock_pro',
	4 => 'name_cat',
	5 => 'name_sup'
);

$sql = "SELECT * FROM pro left join `cat` on `cat`.`id_cat` = `pro`.`id_cat` 
left join `sup` on `sup`.`id_sup` = `pro`.`id_sup`
where `pro`.`active_pro` = '0'";
$query=mysqli_query($db, $sql);
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;
$sql = "SELECT * FROM pro left join `cat` on `cat`.`id_cat` = `pro`.`id_cat` 
left join `sup` on `sup`.`id_sup` = `pro`.`id_sup`
where `pro`.`active_pro` = '0'";
if( !empty($requestData['search']['value']) ) {  
	$sql.=" AND ( id_pro LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR name_cat LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR name_sup LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR price_pro LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR stock_pro LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR name_pro 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(); 
		$m1 = "<button type='button' style='display:".$s."'  class='btn btn-warning c_6' onclick='i_pro(".$row['id_pro'].")'>ادخال الى المخزن</button>";

		$m = "<button type='button'  style='display:".$s1."' class='btn btn-success c_7' onclick='o_pro(".$row['id_pro'].")'>اخراج من المخزن</button>";

		$m2 = "<button type='button' style='display:".$s2."'  class='btn btn-success c_5' onclick='get_pro(".$row['id_pro'].")'>تعديل</button>";

		$m3 = "<button type='button' style='display:".$s2."' class='btn btn-danger' onclick=del_table_1('".$row['id_pro']."',5)>حذف</button>";

	$nestedData[] = $row["id_pro"];
	$nestedData[] = $row["name_pro"];
	$nestedData[] = $row["name_cat"];
	$nestedData[] = $row["name_sup"];
	$nestedData[] = $row["price_pro"];
	$nestedData[] = $row["stock_pro"];
	$nestedData[] = $m3.$m2.$m1.$m;
	
	$data[] = $nestedData;
}



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

echo json_encode($json_data);

?>



