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

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 

$maintenance_card_no = $_GET['maintenance_card_no_text'];
$result_1 = $_GET['survey_result_1'];
$result_2 = $_GET['survey_result_2'];
$result_3 = $_GET['survey_result_3'];
$result_4 = $_GET['survey_result_4'];

$cust_notes = $_GET['cust_notes_text'];

$table_name = $_SESSION['db_prefix']."_survey_maintenance";
$delete_query = "DELETE FROM $table_name WHERE maintenance_card_no =  '".$maintenance_card_no."'   "; 
execsql($delete_query);

$table_name = $_SESSION['db_prefix']."_survey_maintenance";
$survey_query = "INSERT INTO $table_name ( maintenance_card_no, result_1, result_2, result_3, result_4, cust_notes) VALUES ('".$maintenance_card_no."', '".$result_1."', '".$result_2."', '".$result_3."', '".$result_4."', '".$cust_notes."' )";
execsql($survey_query);

$table_name = $_SESSION['db_prefix']."_maintenance_jobs";
$update_query = "UPDATE $table_name SET survey = 'yes' WHERE maintenance_card_no = '".$maintenance_card_no."' ";
execsql($update_query);


$survey_perc = (($result_1+$result_2+$result_3+$result_4)/8)*100;


if (isset($_GET['save_close_button']) )
	   { 
	   	$survey_perc = number_format((($result_1+$result_2+$result_3+$result_4)/8)*100, 0, '.', ',');

		$table_name = $_SESSION['db_prefix']."_tracking_maintenance";
		$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '133', '".$maintenance_card_no."', '".$survey_perc."' ,'','' )";
		$trackingResult = mySQL_query($trackingQuery);
?>
       <script>
	 		 window.location="maintenance_main.php";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="survey_maintenance.php?maintenance_card_no=<?php echo $maintenance_card_no?>";
	   </script>
        <?php		 
	   }
	   ?>

