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

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

$today = date("Y-m-d");
$error=0;
$item_name = $_GET['item_name_text'];
$store_item_id = $_GET['store_item_id'];

$table_name = $_SESSION['db_prefix']."_store_items";
	$check_query = "SELECT item_name FROM $table_name WHERE  item_name = '".trim($item_name)."' " ;
	//echo '<br>*****'.$check_query;
	$check_result = mySQL_query($check_query);
	$check_num = mysql_num_rows($check_result);
	//echo '<br>*****'.$check_num;
	if ($check_num>0) { 
	$table_name = $_SESSION['db_prefix']."_store_items";
	$item_name_query = "SELECT item_name FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
	//echo '<br>*****'.$item_name_query;
	$item_name_result = mySQL_query($item_name_query);
	$stored_item_name = trim(mysql_result($item_name_result, 0, "item_name"));
	//echo '<br>*****'.$stored_item_name;
	//echo '<br>*****'.$item_name;
	if ($stored_item_name==$item_name) {
	$error=0;
	}
	else {
	$error=1;
	$item_name=$stored_item_name;
	}
	}
	
	

$table_name = $_SESSION['db_prefix']."_store_items";
$saved_item_query = "SELECT * FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$saved_item_result = mySQL_query($saved_item_query);
$saved_item_num = mysql_num_rows($saved_item_result);

$item_name_saved = mysql_result($saved_item_result, 0, 'item_name');
$location_saved = mysql_result($saved_item_result, 0, 'location');
$supplier_id_saved = mysql_result($saved_item_result, 0, 'supplier_id');
//$category_saved = mysql_result($saved_item_result, 0, 'category');
$sub_category_saved = mysql_result($saved_item_result, 0, 'subCategory');
$unit_saved = mysql_result($saved_item_result, 0, 'unit');
$min_stock_saved = mysql_result($saved_item_result, 0, 'minimumStock');
$max_stock_saved = mysql_result($saved_item_result, 0, 'maximumStock');
$price_saved = mysql_result($saved_item_result, 0, 'price');
$specs_saved = mysql_result($saved_item_result, 0, 'productSpecs');
$register_saved = mysql_result($saved_item_result, 0, 'register');



$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'];
$max_stock = $_GET['maximum_stock_text'];
$price = $_GET['base_price_text'];
$price = $price/1;
$specs = $_GET['specs_text'];
$register = $_GET['register_select'];



$table_name = $_SESSION['db_prefix']."_store_items";
$edit_query = "UPDATE $table_name SET item_name = '".$item_name."', location = '".$location."', supplier_id = '".$supplier_id."', subCategory = '".$sub_category."', unit = '".$unit."', price = '".$price."', minimumStock = '".$min_stock."', maximumStock = '".$max_stock."', lastModDate = '".$today."', productSpecs = '".$specs."', register = '".$register."' WHERE store_item_id = '".$store_item_id."' ";
execsql($edit_query);


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

if ($item_name_saved!=$item_name) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$item_name_saved."','".$item_name."','Item name' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($location_saved!=$location) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$location_saved."','".$location."','Stores location' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($supplier_id_saved!=$supplier_id) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$supplier_id_saved."','".$supplier_id."','Supplier' )";
$trackingResult = mySQL_query($trackingQuery);
}

//if ($category_saved!=$category) {
//	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$category_saved."','".$category."','Category' )";
//$trackingResult = mySQL_query($trackingQuery);
//}

if ($sub_category_saved!=$sub_category) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$sub_category_saved."','".$sub_category."','Sub category' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($unit_saved!=$unit) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$unit_saved."','".$unit."','Unit' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($min_stock_saved!=$min_stock) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$min_stock_saved."','".$min_stock."','Min stock' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($max_stock_saved!=$max_stock) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$max_stock_saved."','".$max_stock."','Max stock' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($price_saved!=$price) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$price_saved."','".$price."','Price' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($specs_saved!=$specs) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$specs_saved."','".$specs."','Specifications' )";
$trackingResult = mySQL_query($trackingQuery);
}

if ($register_saved!=$register) {
	    $trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entryDate."' ,'".$_SESSION['username']."', '81', '".$store_item_id."' ,'".$register_saved."','".$register."','Product registration' )";
$trackingResult = mySQL_query($trackingQuery);
}


if (isset($_GET['save_close_button']) )
	   { 
	   if ($error==1) { ?>
       <script>
	        window.location="store_item_edit.php?store_item_id=<?php echo $store_item_id?>&error=<?php echo $error ?>";
	   </script>
       <?php } else { ?>
       <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?>&error=<?php echo $error ?>";
	   </script>
        <?php		 
	   }
	   ?>

