<?php
require_once("config/configDB.php"); 

// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$columns = array( 
// datatable column index  => database column name
	
	0 => 'vehicleno',
	1 => 'brandtype',
	2 => 'brandname',
	3 => 'purchasedate',
	4 => 'vehicletype',
    5 => 'vownership',
    6 => 'ins_policynumber'
);

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

$sql = "SELECT vehicleno, brandtype, brandname, model, purchasedate, openingkm, vehicletype, vownership, ins_policynumber, ins_policyfrom,id ";
$sql.=" FROM vehicle_master WHERE 1=1 and isdelete=1";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( vehicleno LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR brandtype LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR brandname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR purchasedate LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR vehicletype  LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR vownership LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR model LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR id LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("vehiclegriddata.php: get vehicle 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("vehiclegriddata.php: get vehicle Data step 3");
	
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	//$nestedData[] = $row["id"];
	$nestedData[] = $row["vehicleno"];
	$nestedData[] = $row["brandtype"];
	//$nestedData[] = $row[""];
	$date = new DateTime($row["purchasedate"]); 
	$nestedData[] = $date->format('d-m-Y'); //only fetching date
	
	$nestedData[] = $row["vownership"];
	$nestedData[] = $row["model"];
	//$nestedData[] = "";
    
	
	//$nestedData[] = $row["open_balance"];
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='vehicleMasterEdit.php?id=".$row['id']."' id='vehicleMaster_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}

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

?>
