<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include ("../navigation/header.php");
$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");

$store_item_id = $_GET['store_item_id'];
$error = $_GET['error'];

$table_name = $_SESSION['db_prefix']."_list_stores_location";
    $location_query = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$location_result = execsql($location_query);
	$location_num = mysql_num_rows($location_result);
	$arrId1 = array ();
	$arrName1 = array ();
	$k = 0;
	while ($k < $location_num) {
		$arrId1[$k] = mysql_result($location_result, $k, "item_name");
		$arrName1[$k] = mysql_result($location_result, $k, "item_name");
		$k++;
	}
	
	
$table_name = $_SESSION['db_prefix']."_suppliers";
$suppliersQuery = "SELECT supplier_id, account_name FROM $table_name  ORDER BY account_name ASC";
	$suppliersResult = execsql($suppliersQuery);
	$suppliersNum = mysql_num_rows($suppliersResult);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while ($w < $suppliersNum) {
		$arrId2[$w] = mysql_result($suppliersResult, $w, "supplier_id");
		$arrName2[$w] = mysql_result($suppliersResult, $w, "account_name");
		$w++;
	}
	
$table_name = $_SESSION['db_prefix']."_list_timber_class";
    $position_query = "SELECT item_name FROM $table_name ORDER BY item_name ASC";
	$arrId3 = array ();
	$arrName3 = array ();
	$position_result = execsql($position_query);
	if ($position_result!="") {
	$position_num = mysql_num_rows($position_result);
	$k = 0;
	while ($k < $position_num) {
		$arrId3[$k] = mysql_result($position_result, $k, "item_name");
		$arrName3[$k] = mysql_result($position_result, $k, "item_name");
		$k++;
	}
	}


$table_name = $_SESSION['db_prefix']."_store_items";
$item_query = "SELECT * FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
$item_result = mySQL_query($item_query);
$item_num = mysql_num_rows($item_result);
$item_name = mysql_result($item_result, 0, "item_name");
$supplier_id = mysql_result($item_result, 0, "supplier_id");
$category = mysql_result($item_result, 0, "category");
$sub_category = mysql_result($item_result, 0, "subCategory");
$location = mysql_result($item_result, 0, "location");
$unit = mysql_result($item_result, 0, "unit");
$quantity = mysql_result($item_result, 0, "quantity");
$minimumStock = mysql_result($item_result, 0, "minimumStock");
$price = mysql_result($item_result, 0, "price");
$price_2 = mysql_result($item_result, 0, "price_2");
$date = mysql_result($item_result, 0, "date");
$lastModDate = mysql_result($item_result, 0, "lastModDate");
$area = mysql_result($item_result, 0, "area");
$class = mysql_result($item_result, 0, "class");
$specs = mysql_result($item_result, 0, "productSpecs");

$table_name = $_SESSION['db_prefix']."_suppliers";
$supplier_query = "SELECT account_name FROM $table_name WHERE supplier_id = '".$supplier_id."' ";
$supplier_result = mySQL_query($supplier_query);
$supplier_num = mysql_num_rows($supplier_result);
if ($supplier_num != 0) {
$account_name = mysql_result($supplier_result, 0, "account_name");
}
else {
	$account_name='';
}

if ($category!='Old') {
	$active='yes';
	}
	else {
		$active='no';
	}

?>
 <!--style sheets-->
<link href="../css/main.css" rel="stylesheet" type="text/css">

<!--jscript-->
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/numbers_only_decimal.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/numbers_only.js"  type="text/javascript"></script>    
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script>  





</head>

<body onLoad="<?php if ($active=='yes') { ?> 
				document.getElementById('cat_1').hidden=false;
                document.getElementById('cat_2').hidden=true;
                document.getElementById('sub_cat_1').hidden=false;
                document.getElementById('sub_cat_2').hidden=true;
               <?php } else { ?>
                document.getElementById('cat_1').hidden=true;
                document.getElementById('cat_2').hidden=false;
                document.getElementById('sub_cat_1').hidden=true;
                document.getElementById('sub_cat_2').hidden=false;
               <?php } ?>
               <?php if ($error==1) { ?>alertMessage(1, '', 'The item name you entered already exists, the name was not changed.'); <?php } ?> 
               " >
				    
<div class="main_frame">
<div class="page_head_frame">
	<label class="label_page_head">Edit store item</label>
    &nbsp;&nbsp;&nbsp;&nbsp;
<label class="label_page_head_2"></label>
  </div>
<div class="page_middle_frame">  
<form action="store_item_edit_save.php" method="get" name="form1" id="form1">
        <table width="664" align="left"  id="table_display">
          <tr>
        <td width="132">Item Id:</td>
        <td><?php echo $store_item_id ?>&nbsp;&nbsp;&nbsp;&nbsp;
          <input type="checkbox" name="active_checkbox" id="active_checkbox" <?php if ($active=='yes') {?> checked='checked' <?php } ?> 
          onClick="
          <?php if ($quantity>0) { ?>
          alertMessage(1, '', 'This item still has a stock of <?php echo $quantity?>.<br>Item cannot be set inactive when stock is not 0.<br>Set stock quantity to zero before making the item inactive.');
          document.getElementById('active_checkbox').checked=true;
		  <?php } else { ?>  
          if (document.getElementById('active_checkbox').checked==false) {
          		document.getElementById('cat_1').hidden=true;
                document.getElementById('cat_2').hidden=false;
                document.getElementById('sub_cat_1').hidden=true;
                document.getElementById('sub_cat_2').hidden=false;
                document.getElementById('category_select').value='Old';
                document.getElementById('sub_category_select').value='Old';
                alertMessage(1, '', 'Category and sub-category are set to<br>- Old -.<br><br>Click Save to effect this change.'); 
             } 
          else {
                document.getElementById('cat_1').hidden=false;
                document.getElementById('cat_2').hidden=true;
                document.getElementById('sub_cat_1').hidden=false;
                document.getElementById('sub_cat_2').hidden=true;
                document.getElementById('category_select').selectedIndex=-1;
                document.getElementById('sub_category_select').selectedIndex=-1;
                alertMessage(1, '', 'Select category and sub-category before saving.'); 
                
          }
          <?php } ?>
          " >
          Active
          </td>
        <td width="149" align="right">Product Name:</td>
        <td width="247">
          <label for="item_text"></label>
          <input name="item_name_text" type="text" class="text200" id="item_text" value="<?php echo $item_name?>" onBlur="capitalize(this)" onfocus = "this.select();">
        </td>
      </tr>
      <tr>
        <td>Location:</td>
        <td>
         <select name="location_select" class="select120"  tabindex="1" >
            <option value="<?php echo $location ?>" selected="selected"><?php echo $location ?></option>
            <?php
        	for ($k = 0; $k < count($arrId1); $k++) {
    		echo "<option value=" . "\"" . $arrId1[$k] . "\"" . ">" . $arrName1[$k] . "</option>";
	}
?>
          </select>
          </td>
        <td align="right">Suppliers Name:</td>
        <td><span class="wizard_frame">
          <select name="supplier_id_select" id="supplier_id_select" class="select160">
            <option value="<?php echo $supplier_id?>" <selected="selected"><?php echo $account_name?></option>
            <?php 
    	for ($w = 0; $w < count($arrId2); $w++) {
		echo "<option value=\"".$arrId2[$w] . "\"" . ">" . $arrName2[$w] . "</option>";	
	    }
     ?>
          </select>
        </span></td>
      </tr>
      <tr>
        <td>Category:</td>
        <td>
        <div id='cat_1'>
          <select name="category_select" class="select160" id="category_select" >
            <option value="Direct materials">Direct materials</option>
            <option value="Stores">Stores</option>
            <option value="Old">Old</option>
            <option value="<?php echo $category?>" selected><?php echo $category?></option>
          </select>
          </div>
          <div id='cat_2'>
          <input name="category_text" id="category_text" type="text" value="Old" readonly>
          </div>
          </td>
        <td align="right">Sub category:</td>
        <td>
        <div id='sub_cat_1'>
          <select name="sub_category_select" class="select160" id="sub_category_select" >
          <option value="Cleaning materials">Cleaning materials</option>
          <option value="Consumables">Consumables</option>
          <option value="Fuel">Fuel</option>
          <option value="Lubricants">Lubricants</option>
          <option value="Machine spares">Machine spares</option>
          <option value="Mitek">Mitek</option>
          <option value="PPE">PPE</option>
          <option value="Sundries">Sundries</option>
          <option value="Timber">Timber</option>
          <option value="Tools">Tools</option>
          <option value="Welding &amp; Engineering">Welding &amp; Engineering</option>
          <option value="Old">Old</option>
          <option value="<?php echo $sub_category?>" selected="selected"><?php echo $sub_category?></option>
        </select>
        </div>
        <div id='sub_cat_2'>
        <input name="sub_category_text" id="sub_category_text" type="text" value="Old" readonly>
        </div>
        </td>
      </tr>
      <tr>
        <td>Unit:</td>
        <td>
		<select name="unit_select" class="select120" id="unit_select">
		  <option value="kg">kg</option>
		  <option value="ltr">ltr</option>
		  <option value="m">m</option>
		  <option value="m2">m2</option>
		  <option value="m3">m3</option>
		  <option value="no">no</option>
		  <option value="pair">pair</option>
		  <option value="<?php echo $unit?>" selected="selected"><?php echo $unit?></option>
        </select>
		</td>
        <td align="right">&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Quantity in stock: </td>
        <td><?php echo $quantity?></td>
        <td align="right">Minimum Stock:</td>
        <td>
		<input name="minimum_stock_text" type="text" class="text100" id="minimum_stock_text" value="<?php echo $minimumStock?>" onfocus = "this.select();">
		</td>
      </tr>
      <tr>
        <td>Base price per unit:</td>
        <td>
        <input name="base_price_text" type="text" class="text100" id="base_price_text" value="<?php echo $price?> " onKeyPress="return isNumberKey(event)" onChange=" this.value = this.value.replace (/(\...).*/, '$1')" onfocus = "this.select();" <?php if ($sub_category=='Timber') {?> readonly <?php } ?>></td>
        <td align="right">Sales price per unit:</td>
        <td><input name="sales_price_text" type="text" class="text100" id="sales_price_text" value="<?php echo $price_2?> " onKeyPress="return isNumberKey(event)" onChange=" this.value = this.value.replace (/(\...).*/, '$1')" onfocus = "this.select();" <?php if ($sub_category=='Timber') {?> readonly <?php } ?>></td>
        </tr>
      <tr>
        <td style="border-top:hidden;">&nbsp;</td>
        <td colspan="3" style="border-top:hidden;"><?php if ($sub_category=='Timber') { echo '<font size="-2">Note: price for this item is based on the mtr price and cannot be edited here. <?font>';} ?></td>
      </tr>
      <tr>
        <td>Class:</td>
        <td><select name="class_select" class="select120" >
          <option value="<?php echo $class ?>" selected="selected"><?php echo $class ?></option>
            <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=" . "\"" . $arrId3[$k] . "\"" . ">" . $arrName3[$k] . "</option>";
	}
?>
        </select>
        </td>
        <td>Plate area:</td>
        <td><input name="plate_area_text" type="text" class="text100" id="plate_area_text" value="<?php echo $area ?>" onKeyPress="return isNumberKey(event)" onChange=" this.value = this.value.replace (/(\.......).*/, '$1')" onfocus = "this.select();"  ></td>
      </tr>
      <tr>
        <td>Product specifications:</td>
        <td colspan="3"><input name="specs_text" type="text" class="text300" id="specs_text" value="<?php echo $specs?>"> </td>
        </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>Date entered:</td>
        <td><?php echo date("d-m-Y", strtotime($date))?></td>
        <td>Last modification:</td>
        <td><?php echo date("d-m-Y", strtotime($lastModDate))?></td>
      </tr>
      
      <tr>
        <td>&nbsp;</td>
        <td colspan="3">&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td colspan="3">&nbsp;</td>
      </tr>
      
      <tr>
        <td>&nbsp;</td>
        <td colspan="3">&nbsp;</td>
      </tr>
  </table>
	
<?php  
if ($category=="Timber") {
echo "<label class='label_std' style='color:#FF0000' > NOTE:<br> Dimensions and class of the timber cannot not be edited.<br>If you need a timber with different sizes, you need to make a new store item. You can make this item invisible by changing the category to 'old'.</label>";
 } 
?>

</div>

<div class="page_middle_frame">
  <table width="645" border="0">
    <tr>
      <td width="20"></td>
      <td width="615" align="right"><input type="hidden" name="store_item_id" id="store_item_id" value="<?php echo $store_item_id ?>" />
        <input name="button2" type="button" class="imagebutton" id="button2" onClick="cancel_confirmation(3, '../store/store_main.php');" value="Close"/>
        <input name="save_button"  id="save_button" type="submit" class="imagebutton" value="Save" onClick="needToConfirm = false; confirmRefresh();" />
        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onClick="needToConfirm = false; confirmRefresh();" /></td>
    </tr>
  </table>
</div>
</form>

<script language="JavaScript" type="text/javascript" xml:space="preserve">
	
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("category_select","req","Select a category.");
  frmvalidator.addValidation("sub_category_select","req","Select a sub-category.");

</script>
</div>
</body>
<?php } else {
$_SESSION['denied_return'] = $_SESSION['location'];	
include ("../navigation/store_sidebar.php");?>
	<body onLoad="accessdenied(); return false">
	</body>
<?php }
?>

<?php include ('../navigation/side_bar.php'); ?>
