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

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


$machine_id = $_POST['machine_id_text'];


$table_name = $_SESSION['db_prefix']."_machinery";
$saved_machine_query = "SELECT * FROM $table_name WHERE machine_id = '".$machine_id."' ";
$saved_machine_result = mySQL_query($saved_machine_query);
$saved_machine_num = mysql_num_rows($saved_machine_result);


$make_saved = mysql_result($saved_machine_result, 0, 'make');
$model_saved = mysql_result($saved_machine_result, 0, 'model');
$department_saved = mysql_result($saved_machine_result, 0, 'department');
$in_service_saved = mysql_result($saved_machine_result, 0, 'in_service');
$serial_no_saved = mysql_result($saved_machine_result, 0, 'serial_no');
$clock_saved = mysql_result($saved_machine_result, 0, 'clock');
$service_interval_saved = mysql_result($saved_machine_result, 0, 'service_interval');
$clock_reading_saved = mysql_result($saved_machine_result, 0, 'clock_reading');
$notes_saved = mysql_result($saved_machine_result, 0, 'notes');


//$id_number = $_POST['id_number_text'];
$make = $_POST['make_text'];
$model = $_POST['model_text'];
if (isset($_POST['in_service_checkbox'])) {
		$in_service = 'yes';
}
else {
	$in_service = 'no';
}
$department = $_POST['department_select'];
$serial_no = $_POST['serial_text'];
$clock = $_POST['clock_select'];
$service_interval = $_POST['service_text'];
$clock_reading = $_POST['clock_reading_text'];
$notes = $_POST['notes_text'];


$table_name = $_SESSION['db_prefix']."_machinery";
$machine_query = "UPDATE $table_name SET in_service = '".$in_service."', make = '".$make."', model = '".$model."', department = '".$department."', serial_no = '".$serial_no."', clock = '".$clock."', service_interval = '".$service_interval."', clock_reading = '".$clock_reading."', notes = '".$notes."' WHERE machine_id = '".$machine_id."' ";
execsql($machine_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']."', '39', '".$machine_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']."', '39', '".$machine_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']."', '39', '".$machine_id."' ,'".$model_saved."','".$model."','model' )";
		$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']."', '39', '".$machine_id."' ,'".$department_saved."','".$department."','department' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($serial_no_saved!=$serial_no) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '39', '".$machine_id."' ,'".$serial_no_saved."','".$serial_no."','service' )";
		$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']."', '39', '".$machine_id."' ,'".$clock_saved."','".$clock."','clock unit' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($service_interval_saved!=$service_interval) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '39', '".$machine_id."' ,'".$service_interval_saved."','".$service_interval."','servic _interval' )";
		$trackingResult = mySQL_query($trackingQuery);
}

if ($clock_reading_saved!=$clock_reading) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '39', '".$machine_id."' ,'".$clock_reading_saved."','".$clock_reading."','clock reading' )";
		$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']."', '39', '".$machine_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="machine_details_edit.php?machine_id=<?php echo $machine_id?>"
	   </script>
        <?php		 
	   }
	   ?>

