<?php 
	include 'dheader.php';
?>
	<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" 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> 
                    	Kit 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">
                 <?php
					$maxID = $db->query("SELECT MAX(id) AS MAXID FROM kitsetup");
					$fetchID = $maxID->fetchArray();
				?>
					<label for="Code" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Kit Code :</label>
					<div class="col-sm-9 col-md-9 col-lg-10">	
						<input type="text" class="form-control form-inps" id="kit_code" value="k<?php echo $fetchID['MAXID']+1; ?>">
					</div>
               	</div>

				<div class="form-group">
				
					<label for="name" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Kit Name :</label>					<div class="col-sm-9 col-md-9 col-lg-10">
						<input type="text" name="name" value="" id="kit_name" list="listItems" class="form-control form-inps">
						<datalist id="listItems">
						<?php
							$listItems = $db->query("SELECT DISTINCT kit_name FROM kitsetup");

							while($listrow = $listItems->fetchArray()){
						?>
							<option value="<?php echo $listrow['kit_name']; ?>">
						<?php } ?>	
						</datalist>
					</div>
				</div>

				<div class="form-group">	
					<label for="unit" class="col-sm-3 col-md-3 col-lg-2 control-label required">Kit Unit :</label>			<div class="col-sm-9 col-md-9 col-lg-10">
					<select name="unit" class="ui search selection dropdown form-control" id="search-select">
					  <option value="">Unit</option>
					  <?php
						$unitNames = $db->query("SELECT * FROM units");
						while($rowNames = $unitNames->fetchArray()){
							$selected = "";
							if (isset($_POST["unit"])) {
							 	$unit_id = $_POST["unit"];
							 	if ($unit_id == $rowNames['id']) {
							 		$selected = "selected";
							 	}
							}    
					?>
						<option value="<?php echo $rowNames['id']; ?>" <?php echo $selected; ?>><?php echo $rowNames['unit']; ?></option>
					<?php } ?>
					</select>


					<a href="addunit.php" target="_blank"><input type="button"  value="Add Unit" class=" submit_button btn btn-success"></a>
					</div>
				</div>

				<div class="form-group">
					<label for="Kit Cost" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Kit Cost :</label>
					<div class="col-sm-9 col-md-9 col-lg-10">	
						<input type="text" class="form-control form-inps" id="kit_cost" value="">
					</div>
               	</div>

				<div class="form-group">
					<label for="Kit Price" class="col-sm-3 col-md-3 col-lg-2 control-label required wide">Kit Price :</label>
					<div class="col-sm-9 col-md-9 col-lg-10">	
						<input type="text" class="form-control form-inps" id="kit_price" value="">
					</div>
               	</div>


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


		<div class="register-box register-items paper-cut">
			<div class="register-items-holder">
									
					<table id="addItems" class="table table-hover">

					<thead>
						<tr class="register-items-header">
							<th class="item_name_heading">Item Name</th>
							<th class="sales_quantity">Qty.</th>
							<th class="action">Action</th>
						</tr>
					</thead>
				
					<tbody class="register-item-content">
						<td>
							<input type="text" id="item_name" class="form-control form-inps" list="itemList" />
							<datalist id="itemList">
							<?php
								$itemList = $db->query("SELECT * FROM itemsetup");
								while ($row = $itemList->fetchArray()) {
							?>
								<option value="<?php echo $row['item_name']; ?>">
							<?php
							}
							?>	
							</datalist>
						</td>
						<td><input type="text" id="item_qty" class="form-control form-inps"></td>		
						<td style="vertical-align: top; text-align: center;"><input type="button" id="add_kit"  value="Add Item" class="submit_button btn btn-success" style="line-height: 1.80"></td>
					</tbody>
					</table>
					
			</div>

			<!-- End of Sales or Return Mode -->
						<!-- End of Store Account Payment Mode -->

		</div>

	</div>	
	</form>
		<div class="panel panel-piluku">
			<div class="panel-heading">
			<a href="newkit.php" class=" submit_button btn btn-primary" style="float: right;">New Kit</a>
			</div>
		</div>

			</div>

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


	

			
<?php include 'dfooter.php'; ?>
<script>
	$(document).ready(function(){
		$('#add_kit').on('click', function(){
		
		var kit_code = $('#kit_code').val();
		var kit_name = $('#kit_name').val();
		var unit = $('#search-select').val();
		var kit_cost = $('#kit_cost').val();
		var kit_price = $('#kit_price').val();
		var item_name = $('#item_name').val();
		var item_qty = $('#item_qty').val();
			$.ajax({
			  method: "POST",
			  url: "addkit.php",
	          async:false,
			  data: { kit_code:kit_code, kit_name:kit_name, unit: unit, kit_price:kit_price, kit_cost:kit_cost, item_name:item_name, item_qty:item_qty },
			  success: function( data ) {
			  	$('#addItems').prepend(data);
				$('#item_name').val();
				$('#item_qty').val(1);
			  }
			});
		});
	});

	$(document).on('click', '.deleteItem', function(){
		var id = $(this).val();
		$(this).closest('tr').css('display', 'none');
		$.ajax({
		  method: "POST",
		  url: "deleteKit.php",
		  data: { id: id },
		  success: function( data ) {
			$(this).closest('tbody').css('display', 'none');
			//return false;
		  }
		});
	});	
</script>