<?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];
	*/

	$countQuery = $db->query("SELECT count(*) totalCount FROM itemsetup WHERE item_name = '$name' AND category = '$category_id'");
 	$rowcount = $countQuery->fetchArray();
 	$totalCount = $rowcount['totalCount'];
 	if ($totalCount < 1) {
 		# code...
 		$sql = $db->query("INSERT INTO itemsetup (uei, item_code, item_name, pcs, dzn, ctn, category, purchase_price, selling_price, stock, alert, open_qty) VALUES ('$item_number', '$product_id', '$name', '$pcs', '$dzn', '$ctn', '$category_id', '$cost_price', '$selling_price', '$quantity', '$reorder_level', '$opening')");

 		//$sql2 = $db->query("INSERT INTO stock (date, show_date, php_date, type, supplierName, itemID, itemDescription, qty, unit) VALUES ('$date', '$show_date', '$php_date', 'Opening', 'Opening Item', '$product_id', '$name', '$quantity', '$unit')");
 		$success = "<strong>Success!</strong> Item added successfully."; 
 	}else{
 		$danger = "<strong>Warning!</strong> This item name with same category already exist.";
 	} 
 } 
?>
	<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>

	<?php include 'newcategoryform.php'; ?>
	<?php include 'newunitform.php'; ?>
	
	<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" autocomplete="off">
			<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 } ?>
                
				<?php
					$p_id = $db->query("SELECT MAX(id) AS maxid FROM itemsetup");
					$row = $p_id->fetchArray();
					$prID = $row['maxid'] + 1;
				?>	
				<div class="form-group">
					<label for="item_number" class="col-sm-3 col-md-3 col-lg-2 control-label wide required">UPC/EAN/ISBN :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="item_number" value="<?php echo $prID; ?>" id="item_number" class="form-control form-inps">
					</div>
				</div>
				<?php
					$p_id = $db->query("SELECT MAX(id) AS maxid FROM itemsetup");
					$row = $p_id->fetchArray();
					$prID = $row['maxid'] + 1;
				?>	
				<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="hidden" value="<?php echo $prID; ?>" 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="" 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']; ?>
						<?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="" 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="" 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="category_id">
					  <option value="">Category</option>
					  <?php
						$catNames = $db->query("SELECT * FROM categories");
						while($rowNames = $catNames->fetchArray()){
							$selected = "";
							if (isset($_POST["category_id"])) {
							 	$unit_id = $_POST["category_id"];
							 	if ($unit_id == $rowNames['id']) {
							 		$selected = "selected";
							 	}
							}    
					?>
						<option value="<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['name']; ?></option>
					<?php } ?>
					</select>
						<div>
									<a href="#" target="_blank" id="manage_cat" title="Manage 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="" 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="" size="8" id="selling_price" class="form-control form-inps">
						</div>
					</div>

					<div class="form-group">
						<label for="stock_qty" 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="" 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="" 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="" id="reorder_level" class="form-control form-inps">
					</div>
				</div>

						<div class="form-actions pull-right">
										&nbsp;
					<button type="Submit" name="submitf" value="Submit" id="submitf" class="submit_button btn btn-primary">Submit</button>
				</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').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>