<?php
include("DB.php");
$bdd = new DB();

/*if($_POST['type']=="getBillNo"){
	$response = array();
	$values=array();
	$row = $bdd->getone("SELECT bill_no FROM truckdetails WHERE ChassisNo='{$_POST['ChassisNo']}' LIMIT 1",$values);
	$response['bill_no']=$row['bill_no'];
	$response['response'] = "success";
	echo json_encode($response);	
}*/

if($_POST['type']=="getChassisNo"){
	//$query = "SELECT ChassisNo FROM truckdetails WHERE TruckNo=(SELECT TruckId FROM truckno WHERE show_status='ok') AND RIGHT(ChassisNo, 5) LIKE '".$_POST['ChassisNo']."%'";
	//$query = "SELECT ChassisNo FROM truckdetails WHERE TruckNo=(SELECT TruckId FROM truckno WHERE show_status='ok')";
	//$result = mysqli_query($conn, $query);
	$values=array();
	//$rows = $bdd->getAll("SELECT ChassisNo FROM truckdetails WHERE select_status IS NULL AND FIND_IN_SET(TruckNo,(SELECT GROUP_CONCAT(TruckId) FROM truckno WHERE show_status='ok')) AND RIGHT(ChassisNo, 5) LIKE '".$_POST['ChassisNo']."%'",$values);
	
	$rows = $bdd->getAll("SELECT ChassisNo,TruckNo FROM truckdetails WHERE select_status ='approved' AND RIGHT(ChassisNo, 5) LIKE '".$_POST['ChassisNo']."%'",$values);
	
	/*$TruckId_rows = $bdd->getAll("SELECT TruckId FROM truckno WHERE show_status='ok'",$values);
	$TruckId_array = array_column($TruckId_rows, 'TruckId');*/
	
	$response = array();
	$chassis_num = array();

	/*if (mysqli_num_rows($result) > 0) {
		while ($row = mysqli_fetch_assoc($result)) {
			array_push($chassis_num,$row['ChassisNo']);
		}
		$response['result']=$chassis_num;
		$response['response'] = "success";
		$response['message'] = "Found.";
	}else{
		$response['response'] = "fail.";
		$response['message'] = "No Chassis No found";
	}*/
	if(!empty($rows)){
		foreach($rows AS $row){
			array_push($chassis_num,$row['ChassisNo']);
			/*if(in_array($row['TruckNo'],$TruckId_array))
				array_push($chassis_num,$row['ChassisNo']);*/
		}
		$response['result']=$chassis_num;
		$response['response'] = "success";
		$response['message'] = "Found.";
	}else{
		$response['response'] = "fail.";
		$response['message'] = "No Chassis No found";
	}
	echo json_encode($response);
}

if($_POST['type']=="setSelectStatus"){
	$values=array();
	$bdd->execute("UPDATE truckdetails SET select_status='selected' WHERE ChassisNo='".$_POST['ChassisNo']."'",$values);
}

if($_POST['type']=="revertSelectStatus"){
	$values=array();
	$bdd->execute("UPDATE truckdetails SET select_status='approved' WHERE ChassisNo='".$_POST['ChassisNo']."'",$values);
}

$bdd->disconnect();