<?php
require_once("config/configDB.php"); 
// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;
$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
$columns = array( 
// datatable column index  => database column name
	0 => 'id', 
	1 => 'ish.invoicedate',
	2 => 'vm.vehicleno',
	3 => 'ish.givenby',
	4 => 'ish.receivedby',
	5 => 'ish.remarks'
);

$sql = "SELECT id FROM internalpro_spare_hdr where isdelete=1";
$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT ish.invoicedate, ish.vehicleno, ish.givenby, ish.receivedby,ish.remarks,ish.id,vm.vehicleno ";
$sql.=" FROM internalpro_spare_hdr as ish  
LEFT JOIN  vehicle_master as vm ON vm.id=ish.vehicleno  WHERE 1=1 and ish.isdelete=1";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( ish.invoicedate LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR vm.vehicleno LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ish.givenby LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ish.receivedby LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ish.remarks LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR ish.id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("brandgriddata.php: get brand Data step 2");
$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($conn, $sql) or die("brandgriddata.php: get brand Data step 3");
	

$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["id"];
	$invoicedate = new DateTime($row["invoicedate"]); 
	$nestedData[] = $invoicedate->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["vehicleno"];
	$nestedData[] = $row["givenby"];
	$nestedData[] = $row["receivedby"];
	$nestedData[] = $row["remarks"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='inSpareProEdit.php?id=".$row['id']."' id='inSparePro_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		$Delete .="<a id='inSpareProDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirminSpareProDelete(".$row['id'].")' >Delete</a>";
	}
	$nestedData[] = $edit."&nbsp".$Delete;
	
	$data[] = $nestedData;
}

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

echo json_encode($json_data);  

?>
