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

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


$warning_id = $_POST['warning_id_text'];

$table_name = $_SESSION['db_prefix']."_warnings";
$saved_warning_query = "SELECT * FROM $table_name WHERE warning_id = '".$warning_id."' ";
$saved_warning_result = mySQL_query($saved_warning_query);
$saved_warning_num = mysql_num_rows($saved_warning_result);

$warning_date_saved = mysql_result($saved_warning_result, 0, 'warning_date');
$expiry_date_saved = mysql_result($saved_warning_result, 0, 'exp_date');
$warning_saved = mysql_result($saved_warning_result, 0, 'warning');
$offence_saved = mysql_result($saved_warning_result, 0, 'offence');
$issued_by_saved = mysql_result($saved_warning_result, 0, 'issued_by');
$warning_text_saved = mysql_result($saved_warning_result, 0, 'warning_text');
$additional_notes_saved = mysql_result($saved_warning_result, 0, 'additional_notes');

$warning_date = $_POST['warning_date'];
$expiry_date = $_POST['expiry_date'];
$warning = $_POST['warning_select'];
$offence = $_POST['offence_select'];
$issued_by = $_POST['issued_select'];
$warning_text = $_POST['warning_text'];
$additional_notes = $_POST['additional_notes_text'];

			
$warning_text = str_replace("'", "-", $warning_text);
//$warning_text = htmlentities($warning_text, ENT_QUOTES);	
		
$additional_notes = str_replace("'", "-", $additional_notes);
//$additional_notes = htmlentities($additional_notes, ENT_QUOTES);	


$table_name = $_SESSION['db_prefix']."_warnings";
$warning_query = "UPDATE $table_name SET warning_date = '".$warning_date."', exp_date = '".$expiry_date."', warning = '".$warning."', offence = '".$offence."', issued_by = '".$issued_by."', warning_text = '".$warning_text."', additional_notes = '".$additional_notes."' WHERE warning_id = '".$warning_id."' ";
execsql($warning_query);


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

if ($warning_date_saved!=$warning_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$warning_date_saved."','".$warning_date."','Warning date' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($expiry_date_saved!=$expiry_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$expiry_date_saved."','".$expiry_date."','Expiry date' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($warning_saved!=$warning) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$warning_saved."','".$warning."','Warning' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($offence_saved!=$offence) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$offence_saved."','".$offence."','Offence' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($issued_by_saved!=$issued_by) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$issued_by_saved."','".$issued_by."','Issued by' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($warning_text_saved!=$warning_text) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '36', '".$warning_id."' ,'".$warning_text_saved."','".$warning_text."','Warning text' )";
		$trackingResult = mySQL_query($trackingQuery);
}

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


if (isset($_POST['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="warning_details.php?warning_id=<?php echo $warning_id?>"
		</script>
        <?php		 
	   }
	   
	if (isset($_POST['save_button']) )
	   {  ?>
       <script>
	          window.location="edit_warning_details.php?warning_id=<?php echo $warning_id?>"
	   </script>
        <?php		 
	   }
	   ?>

