<?php 
	include 'dheader.php';

	if (isset($_POST['update'])) {
		extract($_POST, EXTR_OVERWRITE);
		$update = $db->query("UPDATE kitsetup SET kit_name = '$name', unit = '$unit', cost_price = '$cost', selling_price = '$price' WHERE kit_code = '".$_GET['edit']."'");

		if ($update) {
			$success = "<strong>Success!</strong> Customer added successfully."; 
	 	}else{
	 		$danger = "<strong>Warning!</strong> This Customer name already exist.";
	 	} 
	}


	$kit = $db->query("SELECT * FROM kitsetup WHERE kit_code = '".$_GET['edit']."'");
	$frow = $kit->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" 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['update'])) {
                    ?>
                    <div class="alert alert-<?php echo ($success) ? "success" : "danger"; ?>"> 
                            <?php echo ($success) ? $success : $danger; ?>                          
                    </div>
                 <?php } ?>
               
				<div class="form-group">
				<?php
						if(isset($_GET['edit'])){
				?>
					<input type="hidden" id="kit_code" value="<?php echo $frow['kit_code']; ?>">
				<?php
					}else{
				?>
				<?php
					$maxID = $db->query("SELECT MAX(kit_code) AS MAXID FROM kitsetup");
					$fetchID = $maxID->fetchArray();
				?>
						<input type="hidden" id="kit_code" value="<?php echo $fetchID['MAXID']+1; ?>">
				<?php
					}
				?>	
					<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="<?php echo $frow['kit_name']; ?>" 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($_GET["edit"])) {
							 	$unit_id = $frow['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" name="cost" id="kit_cost" value="<?php echo $frow['cost_price']; ?>">
					</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" name="price" value="<?php echo $frow['selling_price']; ?>">
					</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">
					<?php
						if(isset($_GET['edit'])){
							$kitList = $db->query("SELECT * FROM kitsetup WHERE kit_code = '".$_GET['edit']."'");
							while ($row = $kitList->fetchArray()) {
					?>		
						<tr>
							<td><?php echo $row['kit_items']; ?></td>
							<td><?php echo $row['kit_qty']; ?></td>
							<td style="vertical-align: top; text-align: center;"><button type="button" class="btn btn-danger deleteItem" value="<?php echo $row['id']; ?>">Delete</button></td>
						</tr>
					<?php	
						} }
					?>
						<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>
		<div class="panel panel-piluku">
			<div class="panel-heading">
			<a href="newkit.php" class=" submit_button btn btn-primary" style="float: right; line-height: 1.60;">New Kit</a>
			<input type="submit" name="update" class=" submit_button btn btn-warning" value="Update Kit" style="float: right; margin-right: 10px;">
			</div>
		</div>

		</form>
			</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, item_name:item_name, item_qty:item_qty, kit_price:kit_price, kit_cost:kit_cost, },
			  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>