<?php
require_once '../../Server/connection.php';

## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Custom Field value
$searchByVehPlateNo = $_POST['searchByVehPlateNo'];
$searchByVehType = $_POST['searchByVehType'];
$searchByvehModel = $_POST['searchByvehModel'];
$searchByOwner = $_POST['searchByOwner'];

//$searchByStaffCurrent = 1;
## Search 

$searchQuery = " ";
if($searchByVehPlateNo != ''){
    $searchQuery .= " and (vehicle_plate_no like '%".$searchByVehPlateNo."%') ";
}

if($searchByVehType != ''){
    $searchQuery .= " and (vehicle_type_id='".$searchByVehType."') ";
}

if($searchByvehModel != ''){
    $searchQuery .= " and (vehicle_model_id='".$searchByvehModel."') ";
}

if($searchByOwner != ''){
    $searchQuery .= " and (veh_owner_id='".$searchByOwner."') ";
}



if($searchValue != ''){ 
    $searchQuery .= " and (vehicle_plate_no like '%".$searchValue."%')";
}

//if($searchByStaffCurrent != ''){
    //$searchQuery .= " and (status_id='".$searchByStaffCurrent."') ";
//}



## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from vehicles");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
$sel = mysqli_query($conn,"select count(*) as allcount from vehicles WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

## Fetch records
$empQuery = "select * from vehicles WHERE 1 ".$searchQuery." ";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$x = 1;
while ($row = mysqli_fetch_assoc($empRecords)){
	
	$productId = $row['vehicle_id'];
	$vmodel_id = $row['vehicle_model_id'];
	$vplate = $row['vehicle_plate_no'];
	$vchassis = $row['vehicle_chassis_no'];
	$veh_owner_id = $row['veh_owner_id'];
	$veh_type_id = $row['vehicle_type_id'];
	
	$vQuery15b = "select * from insurance_management WHERE vehicle_id='$productId' order by insurance_id desc limit 1";
    $vRecords15b = mysqli_query($conn, $vQuery15b);
    while ($row15b = mysqli_fetch_assoc($vRecords15b)){
		$insurance_number = $row15b['insurance_number'];
		$issued_date = $row15b['issued_date'];
		$expire_date = $row15b['expire_date'];
		$insurance_cost = number_format($row15b['insurance_cost'],2);
		$vinsuranceIssueDate = date('d-m-Y', strtotime($issued_date));
        $vinsuranceExpireDate = date('d-m-Y', strtotime($expire_date));
	         }
	
	$vQuery15 = "select * from vehicle_models WHERE vehicle_model_id='$vmodel_id'";
    $vRecords15 = mysqli_query($conn, $vQuery15);
    while ($row15 = mysqli_fetch_assoc($vRecords15)){
		$vmodel_name = $row15['vehicle_model_name'];
	         }
			 
			 if($veh_owner_id==1){
			$vownerName = 'Company';	 
			 }else{
			
			 $vQuery151 = "select * from veh_owners WHERE veh_owner_id='$veh_owner_id' and vehicle_id='$productId'";
    $vRecords151 = mysqli_query($conn, $vQuery151);
    while ($row151 = mysqli_fetch_assoc($vRecords151)){
		$vownerName = $row151['firstname'].' '.$row151['lastname'];
	         }

				 
			 }
			 
			 
	$empQuery15w = "select * from veh_type WHERE vehicle_type_id='$veh_type_id'";
    $empRecords15w = mysqli_query($conn, $empQuery15w);
    while ($row15w = mysqli_fetch_assoc($empRecords15w)){
		$vehicle_type = $row15w['vehicle_type'];
	         }		

   

	
	$button3 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="vehDocs.php?i='.$productId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	$button4 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="staffCont.php?i='.$productId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
		
		$button = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" data-toggle="modal"  data-target="#MyModal" data-id='.$productId.' id="getUser11"> <i class="glyphicon glyphicon-eye-open"></i></a>
	</div>';
	
	$button1 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="editVehicle.php?i='.$productId.'" id="editOrderModalBtn"><center><i class="glyphicon glyphicon-edit"></i></center></a>
	</div>';
	
	
	
	//$button1 = '<!-- Single button -->
	//<div class="btn-group">
	    //<a type="button" class="btn btn-primary" data-toggle="modal" id="editCategoriesModalBtn" data-target="#editCategoriesModal" onclick="editCategories('.$productId.')"> <i class="glyphicon glyphicon-edit"></i></a>
	//</div>';
	
	//$button2 = '<!-- Single button -->
	//<div class="btn-group">
	//<a type="button" class="btn btn-primary" data-toggle="modal" data-target="#removeProductModal" id="removeProductModalBtn" onclick="removeProduct('.$productId.')"> <i class="glyphicon glyphicon-trash"></i></a>
	
	//</div>';
	
	
	$button2 = '<!-- Single button -->
	<div class="btn-group">
	    <a type="button" class="btn btn-primary" href="pdf_Vehfile.php?i='.$productId.'" id="editOrderModalBtn"> <center><i class="glyphicon glyphicon-eye-open"></i></center></a>
	</div>';
	
	
    $data[] = array(
    		"vehicle_id"=>$x,
			"vmodel"=>$vmodel_name,
			"vplate"=>$vplate,
			"vchassis"=>$vchassis,
			"vtype"=>$vehicle_type,
			"vowner"=>$vownerName,
			"insNo"=>$insurance_number,
			"insIssDate"=>$vinsuranceIssueDate,
			"insExpDate"=>$vinsuranceExpireDate,
			"insCost"=>$insurance_cost,
    		"edit"=>$button1
			//"staffdocs"=>$button3,
			//"staffCont"=>$button4,
			//"staffFile"=>$button2
    	);
		$x++;
		
		
}

## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
