<?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 => 'dieseldate',
	2 => 'vehicleno',
	3 => 'odometer',
	4 => 'litres',
	5 => 'sname'
);

$sql = "SELECT id FROM externalpro_diesel 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 inpd.dieseldate,inpd.intakekm,inpd.intakehrs,inpd.sname,inpd.vehicleno,inpd.litres,inpd.id FROM externalpro_diesel as inpd WHERE inpd.isdelete=1";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( inpd.dieseldate LIKE '".$requestData['search']['value']."%' ";	
	$sql.=" OR sname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR vehicleno LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR inpd.intakekm LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR inpd.litres LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR inpd.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"];
	$tripedate = new DateTime($row["dieseldate"]); 
	$nestedData[] = $tripedate->format('d-m-Y'); //only fetching date
	$nestedData[] = $row["sname"];
	$nestedData[] = $row["vehicleno"];
	$nestedData[] = $row["intakekm"];
	$nestedData[] = $row["litres"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='exDieselProEdit.php?id=".$row['id']."' id='inDieselPro_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		$Delete .="<a id='exDieselProDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirminDieselProDelete(".$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);  

?>
