<?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');
$department_saved = mysql_result($saved_vehicle_result, 0, 'department');
$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');
$purchase_date_saved = mysql_result($saved_vehicle_result, 0, 'purchase_date');
$disposal_date_saved = mysql_result($saved_vehicle_result, 0, 'disposal_date');
$cost_saved = mysql_result($saved_vehicle_result, 0, 'purchase_cost');
$insurance_value_saved = mysql_result($saved_vehicle_result, 0, 'insurance_value');
$license_saved = mysql_result($saved_vehicle_result, 0, 'road_license_date');
$ba_license_saved = mysql_result($saved_vehicle_result, 0, 'ba_license_date');
$insurance_saved = mysql_result($saved_vehicle_result, 0, 'insurance');
$renew_date_saved = mysql_result($saved_vehicle_result, 0, 'renewal_date');
$notes_saved = mysql_result($saved_vehicle_result, 0, 'notes');


//$id_number = $_POST['id_number_text'];
$make = $_POST['make_select'];
$model = $_POST['model_select'];
$fuel = $_POST['fuel_select'];
$department = $_POST['department_select'];
$service = $_POST['service_select'];
$mileage = $_POST['mileage_text'];
$chassis = $_POST['chassis_text'];
$engine = $_POST['engine_text'];
$purchase_date = $_POST['purchase_date'];
$disposal_date = $_POST['disposal_date'];
$cost = $_POST['cost_text'];
$insurance_value = $_POST['insurance_value_text'];
$license = $_POST['license_select'];
$license_year = $_POST['license_year_select'];
$ba_license = $_POST['ba_license_select'];
$ba_license_year = $_POST['ba_license_year_select'];
$insurance = $_POST['insurance_text'];
$renew_date = $_POST['renew_date'];
$notes = $_POST['notes_text'];

switch ($license) {
    case 'January': $license = $license_year.'-01-31';  break;	
	case 'February': $license = $license_year.'-02-28'; break;	
	case 'March': $license = $license_year.'-03-31'; break;	
	case 'April': $license = $license_year.'-04-30'; break;
	case 'May': $license = $license_year.'-05-31'; break;
	case 'June': $license = $license_year.'-06-30'; break;
	case 'July': $license = $license_year.'-07-31'; break;
	case 'August': $license = $license_year.'-08-31'; break;
	case 'September': $license = $license_year.'-09-30'; break;
	case 'October': $license = $license_year.'-10-31'; break;
	case 'November': $license = $license_year.'-11-30'; break;
	case 'December': $license = $license_year.'-12-31'; break;
	}
	
	switch ($ba_license) {
    case 'January': $ba_license = $ba_license_year.'-01-31';  break;	
	case 'February': $ba_license = $ba_license_year.'-02-28'; break;	
	case 'March': $ba_license = $ba_license_year.'-03-31'; break;	
	case 'April': $ba_license = $ba_license_year.'-04-30'; break;
	case 'May': $ba_license = $ba_license_year.'-05-31'; break;
	case 'June': $ba_license = $ba_license_year.'-06-30'; break;
	case 'July': $ba_license = $ba_license_year.'-07-31'; break;
	case 'August': $ba_license = $ba_license_year.'-08-31'; break;
	case 'September': $ba_license = $ba_license_year.'-09-30'; break;
	case 'October': $ba_license = $ba_license_year.'-10-31'; break;
	case 'November': $ba_license = $ba_license_year.'-11-30'; break;
	case 'December': $ba_license = $ba_license_year.'-12-31'; break;
	}


$table_name = $_SESSION['db_prefix']."_vehicle";
$vehicle_query = "UPDATE $table_name SET make = '".$make."', model = '".$model."', fuel = '".$fuel."', department = '".$department."', service_interval = '".$service."', clock_reading = '".$mileage."', chassis_no = '".$chassis."', engine_no = '".$engine."', purchase_date = '".$purchase_date."', disposal_date = '".$disposal_date."', purchase_cost = '".$cost."', insurance_value = '".$insurance_value."', road_license_date = '".$license."', ba_license_date = '".$ba_license."', insurance = '".$insurance."', renewal_date = '".$renew_date."', notes = '".$notes."' WHERE vehicle_id = '".$vehicle_id."' ";
execsql($vehicle_query);



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

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']."', '41', '".$vehicle_id."' ,'".$make_saved."','".$make."','make' )";
		//echo '<br>id_number'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$model_saved."','".$model."','model' )";
		//echo '<br>emp_first_name'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$fuel_saved."','".$fuel."','fuel' )";
		//echo '<br>emp_last_name'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$department_saved."','".$department."','department' )";
		//echo '<br>birth_date'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$service_saved."','".$service."','service' )";
		//echo '<br>gender'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$mileage_saved."','".$mileage."','mileage' )";
		//echo '<br>bag_box'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$chassis_saved."','".$chassis."','chassis' )";
		//echo '<br>box_no'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$engine_saved."','".$engine."','engine' )";
		//echo '<br>postal_place'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($purchase_date_saved!=$purchase_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$purchase_date_saved."','".$purchase_date."','purchase date' )";
		//echo '<br>phys_address'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($disposal_date_saved!=$disposal_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$disposal_date_saved."','".$disposal_date."','disposal date' )";
		//echo '<br>phys_address'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($cost_saved!=$cost) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$cost_saved."','".$cost."','cost' )";
		//echo '<br>phys_place'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($insurance_value_saved!=$insurance_value) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$insurance_value_saved."','".$insurance_value."','insurance value' )";
		//echo '<br>phys_place'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$license_saved."','".$license."','license' )";
		//echo '<br>emergency_name'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$ba_license_saved."','".$ba_license."','ba license' )";
		//echo '<br>emergency_phone'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($insurance_saved!=$insurance) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$insurance_saved."','".$insurance."','insurance' )";
		//echo '<br>position'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}

if ($renew_date_saved!=$renew_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '41', '".$vehicle_id."' ,'".$renew_date_saved."','".$renew_date."','renew_date' )";
		//echo '<br>pay_period'.$trackingQuery;
			$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']."', '41', '".$vehicle_id."' ,'".$notes_saved."','".$notes."','notes' )";
		//echo '<br>rate'.$trackingQuery;
			$trackingResult = mySQL_query($trackingQuery);
}


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="../transport/transport_main.php?";
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="vehicle_details_edit.php?vehicle_id=<?php echo $vehicle_id?>"
	   </script>
        <?php		 
	   }
	   ?>

