<?php

global $db;
ini_set("display_errors", "0");
error_reporting(E_ALL);
$requestData= $_REQUEST;

if($_SESSION['group_id'] == 2){
	$columns = array( 
		0 => 'warehouse_id',
		1 => 'name',
		2 => 'address',
		3 => 'contact_person',
		4 => 'contact_person_no',	
		5 => 'branch_id',		
		6 => 'notice',
		7 => 'status'		
	);
}else{
	$columns = array( 
		0 => 'warehouse_id',
		1 => 'name',
		2 => 'address',
		3 => 'contact_person',
		4 => 'contact_person_no',	
		5 => 'notice',
		6 => 'status'		
	);	
}
$data = $db->query("SELECT COUNT(company_id) as jumlah FROM warehouses WHERE company_id = '" . getCompany($_SESSION['user_id']) . "'");
$count = $data[0]["jumlah"];
$totalData = $count;
$totalFiltered = $totalData;

$sql = "SELECT * FROM warehouses WHERE company_id = '" . getCompany($_SESSION['user_id']) . "'";
// if( !empty($requestData['columns'][0]['search']['value']) ){   //name
	// $sql.=" AND user_id.user_id LIKE '".$requestData['columns'][0]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][1]['search']['value']) ){   //name
	// $sql.=" AND user_id.uname LIKE '%".$requestData['columns'][1]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][2]['search']['value']) && !empty($requestData['columns'][3]['search']['value'])){   //name
	// $minRange = $requestData['columns'][2]['search']['value'];
	// $maxRange = $requestData['columns'][3]['search']['value'];
	// $sql.=" AND ( user_id.register_date >= '".$minRange."' AND  user_id.register_date <= '".$maxRange."' ) ";
// }
// if( !empty($requestData['columns'][4]['search']['value']) ){   //name
	// $sql.=" AND parent_detail.uname LIKE '%".$requestData['columns'][4]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][5]['search']['value']) ){   //name
	// $sql.=" AND spons_detail.uname LIKE '%".$requestData['columns'][5]['search']['value']."%' ";
// }
// if( !empty($requestData['columns'][6]['search']['value']) ){   //name
	// $sql.=" AND user_id.product = '".$requestData['columns'][6]['search']['value']."' ";
// }
// if( !empty($requestData['columns'][7]['search']['value']) ){   //name
	// $sql.=" AND user_id.banned = '".$requestData['columns'][7]['search']['value']."' ";
// }

$data = $db->query($sql);
$totalFiltered = count($data);
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
$data = $db->query($sql);
$qweqwe = array();
foreach ($data as $key => $value) {
    $nestedData = array(); 
	
	$nestedData[] = $value["warehouse_id"];
	$nestedData[] = $value["name"];
	$nestedData[] = $value["address"]. ", " . $value["zip"]. " ". $value["city"] . ", ". $value["state"] . ", " . $value["country"];
	$nestedData[] = $value["contact_person"] . "<br/>" . $value["contact_person_no"];
	
	if($_SESSION['group_id'] == 2){
	$nestedData[] = get_branch($value["branch_id"], "branch_name");
	}
	$nestedData[] = $value["notice"];
	$nestedData[] = "<input type='checkbox'". ($value["status"] != "1" ?  : "checked") ." id='mySwitch' class='make-switch' data-id='". $value["warehouse_id"] ."'data-size='small' >";
	$nestedData[] = "<a class='btn green btn-sm btn-circle edit' data-id ='". $value["warehouse_id"] ."'> Edit </a><br/><a class='btn red btn-sm btn-circle delete' data-id ='". $value["warehouse_id"] ."'> Delete </a>";
	
	$qweqwe[] = $nestedData;
}
$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw. 
			"recordsTotal"    => intval( $totalData ),  // total number of records
			"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
			"data"            => $qweqwe   // total data array
			);
echo json_encode($json_data);
