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

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

$today = date("Y-m-d");

$contract_card_no = $_GET['contract_card_no'];

$table_name = $_SESSION['db_prefix']."_maintenance_contracts";
$saved_item_query = "SELECT cluster, start_date  FROM $table_name WHERE contract_card_no = '".$contract_card_no."' ";
$saved_item_result = mySQL_query($saved_item_query);
$saved_item_num = mysql_num_rows($saved_item_result);

$cluster_saved = mysql_result($saved_item_result, 0, 'cluster');
$start_date_saved = mysql_result($saved_item_result, 0, 'start_date');


$cluster = $_GET['cluster_select'];
$start_date = $_GET['start_date'];

$temp_date = strtotime($start_date);
$end_date = date("Y-m-d", strtotime("+12 month", $temp_date));
$temp2_date = strtotime($end_date);
$end_date = date("Y-m-d", strtotime("-1 day", $temp2_date));


$table_name = $_SESSION['db_prefix']."_maintenance_contracts";
$edit_query = "UPDATE $table_name SET cluster = '".$cluster."', start_date = '".$start_date."', end_date = '".$end_date."' WHERE contract_card_no = '".$contract_card_no."' ";
execsql($edit_query);


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

if ($cluster_saved!=$cluster) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '129', '".$contract_card_no."' ,'".$cluster_saved."','".$cluster."','Cluster' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($start_date_saved!=$start_date) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '129', '".$contract_card_no."' ,'".$start_date_saved."','".$start_date."','Start date' )";
$trackingResult = mySQL_query($trackingQuery);
}




	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="maintenance_contract.php?contract_card_no=<?php echo $contract_card_no?>";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="maintenance_contract_edit.php?contract_card_no=<?php echo $contract_card_no?>";
	   </script>
        <?php		 
	   }
	   ?>

