<?php
date_default_timezone_set('Asia/Kolkata');
include 'DB.php';
require_once 'web_constant.php';
$bdd=new DB();

if($_POST['action']=='deleteTransitVehicle'){
	$values=array($_POST['id']);
	$row = $bdd->execute("DELETE FROM transit_vehicle_data WHERE tv_id=?",$values);
	$row = $bdd->execute("DELETE FROM transit_vehicle WHERE id=?",$values);
}

if($_POST['action']=='viewTruckDetail'){
	include('xcrud/xcrud.php');
	echo Xcrud::load_css();
	$xcrud=Xcrud::get_instance();
	$xcrud->table('truckdetails');
	$xcrud->columns('TruckNo,ChassisNo,ChassisImg,CylNo,KitNo,EngineNo,color,product');
	$xcrud->fields('CylNo,Cyl_Mfg,KitNo,Kit_Mfg');
	$xcrud->relation('Cyl_Mfg','cyl_mfg','name','name');
	$xcrud->relation('Kit_Mfg','kit_mfg','name','name');
	$xcrud->change_type('ChassisImg','image','',array('path'=>'../upload'));
	$xcrud->modal('ChassisImg');
	$xcrud->label('TruckNo','Truck No');
	$xcrud->label('ChassisNo','Chassis No');
	$xcrud->label('ChassisImg','Chassis Img');
	$xcrud->label('CylNo','Cyl No');
	$xcrud->label('KitNo','Kit No');
	$xcrud->label('EngineNo','Engine No');
	$xcrud->where('bill_no',$_POST['bill_no']);
	$xcrud->unset_title();
	$xcrud->unset_add();
	$xcrud->unset_view();
	$xcrud->unset_edit();
	$xcrud->unset_remove();
	echo $xcrud->render();
	echo Xcrud::load_js();
}

if($_POST['action']=='viewCustomerDetail'){
	$url= web_constant::$ip_scanner."/scanner/api.php";
	//echo $url;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query(array('type' => 'viewCustomerDetail','chassisNo'=>$_POST['chassisNo'])));
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	curl_close($ch);
	echo $output;
}

if($_POST['action']=='updateCompleteStatus'){
	$values=array($_POST['truckId']);
	$row = $bdd->execute("UPDATE truckno SET Status='completed' WHERE TruckId=?",$values);
}

if($_POST['action']=='updateApprovedStatus'){
	$result=array();
	
	$values=array($_POST['id']);
	$row = $bdd->execute("UPDATE truckdetails SET select_status='approved' WHERE id=?",$values);
	
	$bill_no_row = $bdd->getOne("SELECT bill_no FROM `truckdetails` WHERE id=?",$values);
	$bill_no=trim($bill_no_row['bill_no']);
	
	$values=array();
	$total_row = $bdd->getOne("SELECT COUNT(id) as total FROM `truckdetails` WHERE select_status IS NULL AND bill_no='{$bill_no}'",$values);
	
	if($total_row['total']==0){
		//echo "update statu to completed {$total_row['total']}";
		$values=array($bill_no);
		$row = $bdd->execute("UPDATE truckno SET Status='approved' WHERE bill_no=?",$values);
		
		$result['status']='redirect';
	}else{
		$result['status']='stay';
	}
	echo json_encode($result);
} 	

if($_POST['action']=='transitVehicle'){
	$result=array();
	
	/*$values=array();
	$row=$bdd->getOne("SELECT id FROM transit_vehicle WHERE status='Pending' LIMIT 1",$values);
	if(!empty($row)){
		$result['status']='error';
		$result['message']='Pending recotd already exists!';
		echo json_encode($result);
	}else{
		
	}*/
	$detail_array=json_decode($_POST['jsonData']);
	$values=array($detail_array[0]->bill_date,$detail_array[0]->invoice_no,$detail_array[0]->transporter,$detail_array[0]->truck_no,$detail_array[0]->contact_number);
	$id = $bdd->lastId("INSERT INTO `ticl_detail`.`transit_vehicle` (`date`, `bill_no`, `transporter`, `truck_no`, `contact_no`) VALUES (?, ?, ?, ?, ?)",$values);
	
	foreach($detail_array[1] as $vehicle){
		$values=array($id,$vehicle->id,$vehicle->quantity);
		$bdd->execute("INSERT INTO `ticl_detail`.`transit_vehicle_data` (`tv_id`, `model`, `qty`) VALUES (?, ?, ?)",$values);
	}
	$result['status']='success';
	echo json_encode($result);
}
$bdd->disconnect();
?>