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

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

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

$store_item_id = $_GET['store_item_id'];

$table_name = $_SESSION['db_prefix']."_main_store";
$saved_item_query = "SELECT * FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$saved_item_result = $con->query($saved_item_query);
$saved_item_num = mysqli_num_rows($saved_item_result);
$row = mysqli_fetch_assoc($saved_item_result);
$store_item_name_saved = $row['store_item_name'];
$store_location_saved = $row['store_location'];
$supplier_id_saved = $row['supplier_id'];
$store_group_saved = $row['store_group_saved'];
$item_unit_saved = $row['item_unit'];
$min_stock_saved = $row['minimumStock'];
$store_price_saved = $row['store_price'];
$purchase_price_saved = $row['purchase_price'];
$item_specs_saved = $row['item_specs'];


$store_item_name = trim($_GET['item_text_display']);
$store_location = $_GET['location_select'];
$supplier_id = $_GET['supplier_id_select'];
$store_group = $_GET['store_group_select'];
$item_unit = $_GET['unit_select'];
$min_stock = $_GET['minimum_stock_text'];
$store_price = trim($_GET['store_price_text']);
$purchase_price = trim($_GET['purchase_price_text']);
$item_specs = $_GET['specs_text'];


$table_name = $_SESSION['db_prefix']."_store_items";
$edit_query = "UPDATE $table_name SET store_item_name = '".$store_item_name."', store_location = '".$store_location."', supplier_id = '".$supplier_id."', store_group = '".$store_group."', item_unit = '".$item_unit."', store_price = '".$store_price."', purchase_price = '".$purchase_price."', min_stock = '".$min_stock."',  last_mod_date = '".$today."', item_specs = '".$item_specs."' WHERE store_item_id = '".$store_item_id."' ";
$edit_result = $con->query($edit_query);


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

if ($store_item_name_saved!=$store_item_name) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$store_item_name_saved."','".$store_item_name."','Item name' )";
$tracking_result = $con->query($tracking_query);
}

if ($store_location_saved!=$store_location) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$store_location_saved."','".$store_location."','Stores location' )";
$tracking_result = $con->query($tracking_query);
}

if ($supplier_id_saved!=$supplier_id) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$supplier_id_saved."','".$supplier_id."','Supplier' )";
$tracking_result = $con->query($tracking_query);
}

if ($store_group_saved!=$store_group) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$store_group_saved."','".$store_group."','Group' )";
$tracking_result = $con->query($tracking_query);
}

if ($item_unit_saved!=$item_unit) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$item_unit_saved."','".$item_unit."','Unit' )";
$tracking_result = $con->query($tracking_query);
}

if ($min_stock_saved!=$min_stock) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$min_stock_saved."','".$min_stock."','Min stock' )";
$tracking_result = $con->query($tracking_query);
}

if ($store_price_saved!=$store_price) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$store_price_saved."','".$store_price."','Price' )";
$tracking_result = $con->query($tracking_query);
}

if ($purchase_price_saved!=$purchase_price) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$purchase_price_saved."','".$purchase_price."','Purchase price' )";
$tracking_result = $con->query($tracking_query);
}

if ($item_specs_saved!=$item_specs) {
$tracking_query = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' , '".$_SESSION['username']."', '24', '".$store_item_id."' ,'".$item_specs_saved."','".$item_specs."','Specifications' )";
$tracking_result = $con->query($tracking_query);
}



	if (isset($_GET['save_close_button']) )
	   { ?>
       <script>
	 		 window.location="store_item_display.php?store_item_id=<?php echo $store_item_id?>";
		</script>
        <?php		 
	   }
	   
	   if (isset($_GET['save_button']) )
	   {  ?>
       <script>
	          window.location="edit_store_item.php?show=1&store_item_id=<?php echo $store_item_id?>";
	   </script>
        <?php		 
	   }
	   ?>

