<?php
include('../include/session.php');

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time()); 


$vehicle_id = $_POST['vehicle_id_text'];


$table_name = $_SESSION['db_prefix']."_vehicle";
$saved_vehicle_query = "SELECT * FROM $table_name WHERE vehicle_id = '".$vehicle_id."' ";
$saved_vehicle_result = mySQL_query($saved_vehicle_query);
$saved_vehicle_num = mysql_num_rows($saved_vehicle_result);

//$id_number_saved = mysql_result($saved_vehicle_result, 0, 'id_number');
$make_saved = mysql_result($saved_vehicle_result, 0, 'make');
$model_saved = mysql_result($saved_vehicle_result, 0, 'model');
$fuel_saved = mysql_result($saved_vehicle_result, 0, 'fuel');
$in_service_saved = mysql_result($saved_vehicle_result, 0, 'in_service');
$average_consumption_saved = mysql_result($saved_vehicle_result, 0, 'average_consumption');
$department_saved = mysql_result($saved_vehicle_result, 0, 'department');
$driver_saved = mysql_result($saved_vehicle_result, 0, 'driver');
$service_saved = mysql_result($saved_vehicle_result, 0, 'service_interval');
$mileage_saved = mysql_result($saved_vehicle_result, 0, 'clock_reading');
$chassis_saved = mysql_result($saved_vehicle_result, 0, 'chassis_no');
$engine_saved = mysql_result($saved_vehicle_result, 0, 'engine_no');
$license_saved = mysql_result($saved_vehicle_result, 0, 'road_license_date');
$ba_license_saved = mysql_result($saved_vehicle_result, 0, 'ba_license_date');
$notes_saved = mysql_result($saved_vehicle_result, 0, 'notes');
$clock_saved = mysql_result($saved_vehicle_result, 0, 'clock');

//$id_number = $_POST['id_number_text'];
$make = $_POST['make_select'];
$model = $_POST['model_select'];
if (isset($_POST['in_service_checkbox'])) {
		$in_service = 'yes';
}
else {
	$in_service = 'no';
}
$fuel = $_POST['fuel_select'];
$average_consumption = $_POST['average_text'];
$department = $_POST['department_select'];
$driver = $_POST['driver_text'];
$service = $_POST['service_select'];
$mileage = $_POST['mileage_text'];
$chassis = $_POST['chassis_text'];
$engine = $_POST['engine_text'];
$license = $_POST['license_select'];
$ba_license = $_POST['ba_license_select'];
$notes = $_POST['notes_text'];
$clock = $_POST['clock_unit_select'];


$table_name = $_SESSION['db_prefix']."_vehicle";
$vehicle_query = "UPDATE $table_name SET in_service = '".$in_service."', make = '".$make."', model = '".$model."', clock = '".$clock."', fuel = '".$fuel."', average_consumption = '".$average_consumption."', department = '".$department."', driver = '".$driver."', service_interval = '".$service."', clock_reading = '".$mileage."', chassis_no = '".$chassis."', engine_no = '".$engine."', road_license_date = '".$license."', ba_license_date = '".$ba_license."', notes = '".$notes."' WHERE vehicle_id = '".$vehicle_id."' ";
execsql($vehicle_query);



$table_name = $_SESSION['db_prefix']."_tracking_store";

if ($in_service_saved!=$in_service) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$in_service_saved."','".$in_service."','in service' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($make_saved!=$make) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$make_saved."','".$make."','make' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($model_saved!=$model) {
		$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$model_saved."','".$model."','model' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($clock_saved!=$clock) {
		$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$clock_saved."','".$clock."','clock' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($fuel_saved!=$fuel) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$fuel_saved."','".$fuel."','fuel' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($average_consumption_saved!=$average_consumption) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$average_consumption_saved."','".$average_consumption."','average consumption' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($department_saved!=$department) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$department_saved."','".$department."','department' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($driver_saved!=$driver) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$driver_saved."','".$driver."','driver' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($service_saved!=$service) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$service_saved."','".$service."','service' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($mileage_saved!=$mileage) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$mileage_saved."','".$mileage."','mileage' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($chassis_saved!=$chassis) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$chassis_saved."','".$chassis."','chassis' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($engine_saved!=$engine) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$engine_saved."','".$engine."','engine' )";
		$trackingResult = mySQL_query($trackingQuery);
}



if ($license_saved!=$license) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$license_saved."','".$license."','license' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($ba_license_saved!=$ba_license) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$ba_license_saved."','".$ba_license."','ba license' )";
		$trackingResult = mySQL_query($trackingQuery);
}


if ($notes_saved!=$notes) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '19', '".$vehicle_id."' ,'".$notes_saved."','".$notes."','notes' )";
		$trackingResult = mySQL_query($trackingQuery);
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="store_main.php?date=0&branch=&subCatSelect=Tiles";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="vehicle_details_edit.php?vehicle_id=<?php echo $vehicle_id?>"
	   </script>
        <?php		 
	   }
	   ?>

