<?php 
include 'dheader.php';
$success = "";
$danger = "";
if (isset($_POST['submitf'])) {
 	# code...
 	extract($_POST, EXTR_OVERWRITE);
 	/*
	 	$date = $_POST['date'];
		$change_date = explode("/", $date);
		$show_date = $change_date[1]."/".$change_date[0]."/".$change_date[2];
		$php_date = $change_date[2]."/".$change_date[0]."/".$change_date[1];
	*/
 		$id = $_GET['edit'];
 		$sql = $db->query("UPDATE itemsetup SET uei = '$item_number', item_name = '$name', pcs = '$pcs', dzn = '$dzn', ctn = '$ctn', category = '$category_id', purchase_price = '$cost_price', selling_price = '$selling_price', stock = '$quantity', alert = '$reorder_level', open_qty = '$opening' WHERE id =  $id ");

 		//$sql2 = $db->query("UPDATE stock SET date = '$date', show_date = '$show_date', php_date = '$php_date', itemID = '$product_id', itemDescription = '$name', qty = '$opening', unit = '$unit' WHERE itemID =  $id AND type = 'Opening' ");

 	if($sql){
 		$success = "<strong>Success!</strong> Item update successfully."; 
 	}else{
 		$danger = "<strong>Warning!</strong> This item name with same category already exist.";
 	} 
 }
 	$id = $_GET['edit'];
	$items = $db->query("SELECT * FROM itemsetup WHERE id = '$id'");
	$row = $items->fetchArray();  
?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<div class="row" id="form">
	<div class="spinner" id="grid-loader" style="display:none">
	  <div class="rect1"></div>
	  <div class="rect2"></div>
	  <div class="rect3"></div>
	</div>
	
	<div class="col-md-12">
		<form action="" id="customer_form" class="form-horizontal" enctype="multipart/form-data" method="post" accept-charset="utf-8" novalidate="novalidate">
			<div class="col-md-12">
	<div class="panel panel-piluku">
		<div class="panel-heading">
                <h3 class="panel-title">
                    <i class="ion-edit"></i> 
                    	Item Information						<small>(Fields in red are required)</small>
                </h3>
        </div>

			<div class="panel-body">
				<?php
                    if (isset($_POST['submitf'])) {
                    ?>
                    <div class="alert alert-<?php echo ($success) ? "success" : "danger"; ?>"> 
                            <?php echo ($success) ? $success : $danger; ?>                          
                    </div>
                 <?php } ?>
                

				<div class="form-group">
					<label for="item_number" class="col-sm-3 col-md-3 col-lg-2 control-label wide">UPC/EAN/ISBN :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="item_number" autofocus="on" value="<?php echo $row['uei']; ?>" id="item_number" class="form-control form-inps">
					</div>
				</div>
				<div class="form-group" style="display: none;">
					<label for="product_id" class="col-sm-3 col-md-3 col-lg-2 control-label wide">Product ID :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" value="<?php echo $row['item_code']; ?>" name="product_id" value=""  id="product_id" class="form-control form-inps">
						
					</div>
				</div>

				<div class="form-group">
					<label for="name" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Item Name :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="name" value="<?php echo $row['item_name']; ?>" id="name" list="listItems" class="form-control form-inps">
						<datalist id="listItems">
						<?php
							$listItems = $db->query("SELECT * FROM itemsetup");
							while($listrow = $listItems->fetchArray()){
						?>
							<option value="<?php echo $listrow['item_name']; ?>"><?php echo $listrow['item_name']; ?></option>
						<?php } ?>	
						</datalist>
					</div>
				</div>

				<div class="form-group" style="display: none;">
					<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Pcs :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="pcs" value="1" size="8" id="pcs" class="form-control form-inps">
					</div>
				</div>

				<div class="form-group" style="display: none;">
					<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label wide">Dzn :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="dzn" value="<?php echo $row['dzn']; ?>" size="8" id="dzn" class="form-control form-inps">
					</div>
				</div>

				<div class="form-group" style="display: none;">
					<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label wide">Ctn :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="ctn" value="<?php echo $row['ctn']; ?>" size="8" id="ctn" class="form-control form-inps">
					</div>
				</div>

				<div class="form-group">
					<label for="category_id" class="col-sm-3 col-md-3 col-lg-2 control-label  required wide">Category :</label>					
					<div class="col-sm-9 col-md-9 col-lg-10">
						<select name="category_id" class="ui search selection dropdown form-control" id="search-select">
					  <option value="">Category</option>
					  <?php
						$catNames = $db->query("SELECT * FROM categories");
						while($rowNames = $catNames->fetchArray()){
							$selected = "";
							if ($row['category'] == $rowNames['id']) {
								$selected = "selected";
							}
					?>
						<option value="<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['name']; ?></option>
					<?php } ?>
					</select>
						<div>
									<a href="categories.php" target="_blank" title="Add Category" class=" submit_button btn btn-success">Add Category</a>								</div>
					</div>
				</div>
 	
							</div><!--/panel-body -->
		</div><!-- /panel-piluku -->


		<div class="panel panel-piluku">
			<div class="panel-heading pricing-widget">
				Pricing and Inventory			</div>
			<div class="panel-body">
					<div class="form-group">
						<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Cost Price :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="cost_price" value="<?php echo $row['purchase_price']; ?>" size="8" id="cost_price" class="form-control form-inps">
						</div>
					</div>

					<div class="form-group"  style="display: none;">
						<label for="item_discount" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Discount% :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="item_discount" value="0" size="8" id="item_discount" class="form-control form-inps">
						</div>
					</div>


					<div class="form-group">
						<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Selling Price :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="selling_price" value="<?php echo $row['selling_price']; ?>" size="8" id="selling_price" class="form-control form-inps">
						</div>
					</div>

					<div class="form-group">
						<label for="cost_price" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Stock Quantity :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="quantity" value="<?php echo $row['stock']; ?>" size="8" id="quantity" class="form-control form-inps">
						</div>
					</div>

					<div class="form-group">
						<label for="open_qty" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Opening Quantity :</label>						<div class="col-sm-9 col-md-9 col-lg-10">
							<input type="text" name="opening" value="<?php echo $row['open_qty']; ?>" size="8" id="opening" class="form-control form-inps">
						</div>
					</div>

					<div class="form-group reorder-input ">
					<label for="reorder_level" class="col-sm-3 col-md-3 col-lg-2 control-label wide">Reorder Level :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="reorder_level" value="<?php echo $row['alert']; ?>" id="reorder_level" class="form-control form-inps">
					</div>
				</div>


						<div class="form-actions pull-right">
										&nbsp;
					<input type="submit" name="submitf" value="Submit" id="submitf" class="submit_button btn btn-primary">
				</div>

			</div><!-- /panel-body-->
		</div><!--/panel-piluku-->

	</div>
			
		</form>
			</div>
		</div><!-- /row -->


	

			
<?php include 'dfooter.php'; ?>
<script>
	$(document).ready(function(){

		  $('#submitf').attr('disabled', 'disabled');
        $('#name,#item_number,#search-select,#cost_price,#selling_price,#quantity,#opening,#reorder_level').on('change', function(e){
			 $('#submitf').removeAttr('disabled', 'disabled');
		});


		$('#manage_cat').on('click', function(e){
			e.preventDefault();
			$('#cat_form').show();
		});

		$('#closecatform').on('click', function(){
			$('#cat_form').hide();
		});

		$('#catsubmit').on('click', function(e){
			e.preventDefault();

			var cat_name = $('#cname').val();
			$.ajax({
			  method: "POST",
			  url: "addnewcatfromitem.php",
			  async:false,
			  data: { cat_name: cat_name },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#category_id').append(data);
			  	$('#category_id').focus();
			  }
		});
			$('#cat_form').hide();
		});
	});

	$(document).ready(function(){
		$('#manageunit').on('click', function(e){
			e.preventDefault();
			$('#unitform').show();
		});

		$('#closeunitform').on('click', function(){
			$('#unitform').hide();
		});

		$('#unitsubmit').on('click', function(e){
			e.preventDefault();

			var uname = $('#uname').val();
			$.ajax({
			  method: "POST",
			  url: "addnewunitfromitem.php",
			  async:false,
			  data: { uname: uname },
			  dataType: 'html',
			  success: function( data ) {
			  	$('#add_unit').append(data);
			  	$('#add_unit').focus();
			  }
		});
			$('#unitform').hide();
		});
	});

	</script>