<?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']."_store_items";
$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);
$item_name_saved = $row['item_name'];
$location_saved = $row['location'];
$item_name_saved = $row['item_name'];
$location_saved = $row['location'];
$supplier_id_saved = $row['supplier_id'];
$category_saved = $row['category'];
$sub_category_saved = $row['subCategory'];
$unit_saved = $row['unit'];
$min_stock_saved = $row['minimumStock'];
$price_saved = $row['price'];
$purchase_price_saved = $row['purchase_price'];
$specs_saved = $row['productSpecs'];


$item_name = trim($_GET['item_text']);
$location = $_GET['location_select'];
$supplier_id = $_GET['supplier_id_select'];
$category = $_GET['category_select'];
$sub_category = $_GET['sub_category_select'];
$unit = $_GET['unit_select'];
$min_stock = $_GET['minimum_stock_text'];
$price = trim($_GET['base_price_text']);
$purchase_price = trim($_GET['purchase_price_text']);
$specs = $_GET['specs_text'];


$table_name = $_SESSION['db_prefix']."_store_items";
$edit_query = "UPDATE $table_name SET item_name = '".$item_name."', location = '".$location."', supplier_id = '".$supplier_id."', category = '".$category."', subCategory = '".$sub_category."', unit = '".$unit."', price = '".$price."', purchase_price = '".$purchase_price."', minimumStock = '".$min_stock."',  lastModDate = '".$today."', productSpecs = '".$specs."' WHERE store_item_id = '".$store_item_id."' ";
$edit_result = $con->query($edit_query);


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

if ($item_name_saved!=$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."' ,'".$item_name_saved."','".$item_name."','Item name' )";
$tracking_result = $con->query($tracking_query);
}

if ($location_saved!=$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."' ,'".$location_saved."','".$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 ($category_saved!=$category) {
$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."' ,'".$category_saved."','".$category."','Category' )";
$tracking_result = $con->query($tracking_query);
}

if ($sub_category_saved!=$sub_category) {
$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."' ,'".$sub_category_saved."','".$sub_category."','Sub category' )";
$tracking_result = $con->query($tracking_query);
}

if ($unit_saved!=$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."' ,'".$unit_saved."','".$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 ($price_saved!=$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."' ,'".$price_saved."','".$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 ($specs_saved!=$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."' ,'".$specs_saved."','".$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="store_item_edit.php?store_item_id=<?php echo $store_item_id?>";
	   </script>
        <?php		 
	   }
	   ?>

