<?php 
	$header_file=APPPATH.'views/include/header.php';
	require($header_file);
	$sidenav_file=APPPATH.'views/include/sidenav.php';
	require($sidenav_file);	
?>
    <!-- BEGIN: Content-->
    <div class="app-content content">
      <div class="content-overlay"></div>
      <div class="content-wrapper">
        <div class="content-header row">
          <div class="content-header-left col-md-12 col-12 mb-2 breadcrumb-new">
            <h3 class="content-header-title mb-0 d-inline-block">Maintainance</h3>
            <div class="row breadcrumbs-top d-inline-block">
              <div class="breadcrumb-wrapper col-12">
                <ol class="breadcrumb">
                  <li class="breadcrumb-item"><a href="#">Home</a>
                  </li>
                  <li class="breadcrumb-item"><a href="#">Maintainance </a>
                  </li>
                  <li class="breadcrumb-item active">Maintainance GRN
                  </li>
                </ol>
              </div>
            </div>
          </div>
          
        </div>
        <div class="content-body"><!-- Basic form layout section start -->
<section id="basic-form-layouts">
<form class="form" autocomplete="off" action="<?php echo site_url('Maintainance/addMaintainance');?>" method="post">
	<div class="row justify-content-md-center">
		<div class="col-md-12">
			<div class="card">
				<div class="card-content collapse show">
					<div class="card-body">
						<div class="row justify-content-md-center card_margin" >
							<div class="col-md-4 mleft">
								<div class="form-group">
									<label>GRN Date</label>
									<?php $cur_date = date("d-m-Y");
										$branch_id = $_SESSION['branch_id'];
										$branch_name = $_SESSION['branch_name'];
										$full_name = $_SESSION['full_name'];	
										?>
									<input class="form-control" type="text" name="grn_date"  id="uk_dp_1" value="<?php echo $cur_date; ?>" readonly >
									<!--<input class="form-control" type="text" name="grn_date"  id="uk_dp_1" placeholder="Date" data-uk-datepicker="{format:'DD-MM-YYYY'}" required >-->
									<input type="hidden" name="uname" id="uname" class="form-control" value="<?php echo $full_name; ?>"/>
									<input type="hidden" name="login_id" id="login_id" class="form-control" value="<?php echo $_SESSION['user_id']?>"/>
								</div>
							</div>
							<div class="col-md-4">
								<div class="form-group">
									<label>Invoice No</label>
									<input class="form-control" type="text" name="grn_invoice" id="grn_invoice" required  placeholder="Invoice No">
								</div>
							</div>
							<div class="col-md-4">
								<div class="form-group">
									<label>Supplier Name</label>
									
									<input class="form-control" type="text" name="vender_name" id="vender_name" required  >
									<!--<select name="vender_name" id="vender_name" class="form-control select2" required >
											<option value="0">Select</option>
											<?php /* foreach($vendor as $vrow)
												{
													echo '<option value="'.$vrow->vendor_name.'">'.$vrow->vendor_name.'</option>';
												} */
											?>
									</select>-->
									
									<input type="hidden" id="branch_id" class="form-control"  name="branch_id" value="<?php echo $branch_id;?>">
									<input type="hidden" id="branch_name" class="form-control"  name="branch_name" value="<?php echo $branch_name;?>">
									<input type="hidden" id="full_name" class="form-control"  name="full_name" value="<?php echo $full_name;?>">
								</div>
							</div>
						</div>
				    </div>
		        </div>
        	</div>
		    <div class="card">
				<div class="card-content collapse show">
					<div class="card-body">
    					<div class="row justify-content-md-center card_margin">
    						<div class="text_increment">
    							<div>
    								<div class="row">
    									<div class="col-md-3">
    										<div class="form-group">
    											<input class="form-control product_details" type="text" name="item_name[]" id="item_name" required  placeholder="Product Name"/>
    										</div>
    									</div>
    									<div class="col-md-1">
    										<div class="form-group">
    											<input class="form-control product_details quantity" type="text"  name="quantity[]"  id="quantity" required placeholder="Req Qty"/>
    										</div>
    									</div>
    									<div class="col-md-1">
    										<div class="form-group">
    											<input class="form-control product_details price" type="text" name="price[]"   id="price" required Placeholder="Price" />
    											<input class="form-control batch_no" type="hidden" name="basic_amount[]"  id="basic_amount" />
    										</div>
    									</div>
    									<div class="col-md-1">
    										<div class="form-group">
    											<select id="tax" name="tax[]" class="form-control select2 txt1" required >
    												<option value="">Tax</option>
    												<?php  foreach($tax as $row)
    													{
    														echo '<option value="'.$row->tax_percentage.'">'.$row->tax_percentage.'</option>';
    													} 
    												?>
    											</select>
    										</div>
    									</div>
    									<div class="col-md-1">
    										<div class="form-group">
    											<input class="form-control batch_no" type="text" name="tax_amount[]"  id="tax_amount" readonly placeholder="Tax amt" />					
    										</div>
    									</div>
    									<div class="col-md-1">
    										<div class="form-group">
    											<input class="form-control batch_no txt" type="text" name="total_amount[]"  id="total_amount" readonly placeholder="amount"/>
    										</div>
    									</div>
    									<div class="col-md-3">
    										<div class="form-group">
    											<input class="form-control batch_no" type="text" name="remarks[]" id="remarks" placeholder="Remarks"/>
    											<input class="form-control product_details" type="hidden" name="item_sid[]" id="item_sid"  readonly />
    										</div>
    									</div>
								    </div>
							    </div>	
						    </div>
						    <input class="md-input result" type="hidden" name="products" id="products" />
					    </div>
                    </div>
			    </div>
		    </div>
			<div class="card">
				<div class="card-content collapse show">
					<div class="card-body">
							<div class="text-right">
								<div class="row card_margin">
									<div class="col-md-2 ">
										<div class="form-group">
											
											<input class="form-control batch_no" type="text" name="bbbb"  id="tot" readonly Placeholder="Total amount"/>
										</div>
									</div>
									<div class="col-md-8 mleft">
										<button type="reset" class="btn btn-warning mr-1">
											<i class="ft-x"></i> Cancel
										</button>
										<button type="submit" class="btn btn-primary">
											<i class="la la-check-square-o"></i> Save
										</button>
    								</div>
    							</div>
    						</div>
    					</div>
        				<div class="col-md-1 md-fab-wrapper plus mleft">
        					<a href="javascript:void(0);" class="add_button" title="Add field"><i class="la la1 la-plus-circle"></i>
        					<!--<img src="<?php //echo base_url(); ?>app-assets/images/add-icon.png" class="image_logo"/>-->
        					</a>
        				</div>
        				<br/>
			        </div>
			    </form>	
		    </div>
	    </div>
    </section>
</div>
</div>
</div>
    <?php //require('include/theme_customizer.php');?>

   <?php 
  $footer_file=APPPATH.'views/include/footer.php';
	require($footer_file);
	?>
<script type="text/javascript">
		$(document).ready(function(){
			var maxField = 100; //Input fields increment limitation
			var addButton = $('.add_button'); //Add button selector
			var wrapper = $('.text_increment'); //Input field wrapper
			var x = 1; //Initial field counter is 1
			var counter = 1;
			//$(addButton).click(function(){ 
			$(document).on("click",".add_button",function() {  //Once add button is clicked
				if(x < maxField){ //Check maximum number of input fields
					x++; //Increment field counter
					
					$(wrapper).append('<div><div class="row"><div class="col-md-3"><div class="form-group"><input class="form-control product_details" type="text" name="item_name[]" id="item_name'+counter+'" required placeholder="Product Name" /></div></div><input class="form-control product_details " type="hidden"  name="item_sid[]"  id="item_sid'+counter+'"/><div class="col-md-1"><div class="form-group"><input class="form-control product_details quantity1" type="text"  name="quantity[]" Placeholder="Req Qty" id="quantity'+counter+'" required/></div></div><div class="col-md-1"><div class="form-group"><input class="form-control batch_no price1" type="text" name="price[]" id="price'+counter+'" placeholder="Price" required /><input class="form-control batch_no total_amount" type="hidden" name="basic_amount[]"  id="basic_amount'+counter+'" /></div></div><div class="col-md-1"><div class="form-group"><select id="tax'+counter+'" name="tax[]" class="form-control select2 txt1" required ><option value="">Tax</option><?php  foreach($tax as $row) { echo '<option value="'.$row->tax_percentage.'">'.$row->tax_percentage.'</option>';	} ?></select></div></div><div class="col-md-1"><div class="form-group"><input class="form-control batch_no total_amount" type="text" name="tax_amount[]"  id="tax_amount'+counter+'" placeholder="Tax amt" readonly/></div></div><div class="col-md-1"><div class="form-group"><input class="form-control batch_no total_amount txt" type="text" name="total_amount[]"  id="total_amount'+counter+'" placeholder="amount" /></div></div><div class="col-md-3"><div class="form-group"><input class="form-control batch_no" type="text" name="remarks[]" id="remarks" placeholder="Remarks" /></div></div><div class="col-md-1 remove_button"><a href="javascript:void(0);" class="" title="Remove field"><img src="<?php echo base_url(); ?>app-assets/images/remove-icon.png" class="image_logo"/></a></div></div></div>'); 
					//alert(counter);
					$(function () {
						$(".select2").select2();
						$('#quantity' + counter).keypress(function(e) {
							var a = [];
							var k = e.which;
							for (i = 46; i < 58; i++)
							a.push(i);
							if (!($.inArray(k,a)>=0))
							e.preventDefault();
						});
					});
					
					$('#item_name'+counter).click(function(){
						var id = $(this).attr('id');
						id_length = id.length;
						//alert(id);
						$("#"+id).autocomplete({
							//source:'get_auoto_item_name.php', 
							source:'<?php echo 
				base_url();?>index.php/Master/get_auoto_item_name',
							minLength:1,
							select:function(evt, ui)
							{
								if(id_length == 10) {
									//counts = counter - 1;
									counts = id.substr(-1);
									//alert(counts);
									// when a item_name_auto is selected, populate related fields in this form
									//this.form.item_price.value = ui.item.item_price;
									$('#price' + counts).val(ui.item.price);
									$('#price' + counts).blur();
								}
								else {
									counts = id.substr(-2);
									$('#price' + counts).val(ui.item.price);
									$('#price' + counts).blur();
								}
							}
						});
					});
					//Total amount calculation
					$('#quantity'+counter).on('keyup',function() {
						var id = $(this).attr('id');
						id_length = id.length;
						//alert(id_length);
						if(id_length == 9) {
							//alert(id1);
							counts = id.substr(-1);
							var qty = $('#quantity'+ counts).val();
							var stock = $('#pr_stock'+ counts).val();
							var price = $('#price'+ counts).val();
							//alert(stock);
							var result = (parseFloat(qty) * parseFloat(price)).toFixed(2);
							//var basic = $('#quantity').val() * $('#price').val();
							if (!isNaN(result)) {
								$('#total_amount'+ counts).val(result);
								$('#total_amount'+ counts).blur();
								$('#basic_amount'+ counts).val(result);
								//$('#net_amount').val(result);
							}
						}
						else{
							counts = id.substr(-2);
							var qty = $('#quantity'+ counts).val();
							var stock = $('#pr_stock'+ counts).val();
							var price = $('#price'+ counts).val();
							//alert(stock);
							//$("div#result").css("display","none");
							var result = (parseFloat(qty) * parseFloat(price)).toFixed(2);
							//var basic = $('#quantity').val() * $('#price').val();
							if (!isNaN(result)) {
								$('#total_amount'+ counts).val(result);
								$('#total_amount'+ counts).blur();
								$('#basic_amount'+ counts).val(result);
								//$('#net_amount').val(result);
							}
						}
					});
					// tax caluclation 
					$('#tax' +counter).change(function () {
						var id = $(this).attr('id');
						id_length = id.length;
						//alert(id_length);
						if(id_length == 4) {
							//alert(id1);
							counts = id.substr(-1);
							var ba=$('#basic_amount'   + counts).val();
							var tax=$('#tax'   + counts).val();
							var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
							var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
							$('#tax_amount' +counts).val(tax_a);
							$('#tax_amount' +counts).blur();
							$('#total_amount' +counts).val(total);
							$('#total_amount' +counts).blur();
							var result = (parseFloat(ba));
							if (!isNaN(result)) {
								$('#total_price' +counts).val(result);
								$('#total_price' +counts).blur();
							}
						}
						else{
							counts = id.substr(-2);
							var ba=$('#basic_amount'   + counts).val();
							var tax=$('#tax'   + counts).val();
							var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
							var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
							$('#tax_amount' +counts).val(tax_a);
							$('#tax_amount' +counts).blur();
							$('#total_amount' +counts).val(total);
							$('#total_amount' +counts).blur();
							var result = (parseFloat(ba));
							if (!isNaN(result)) {
								$('#total_price' +counts).val(result);
								$('#total_price' +counts).blur();
							}
						}
					});
					// tax value auto complete 
					$('#tax'+counter).click(function(){
						var id = $(this).attr('id');
						//alert(id);
						$("#"+id).autocomplete({
							source:'tax_amount_auto.php', 
							minLength:1,
							select:function(evt, ui)
							{
								//counts = counter - 1;
								counts = id.substr(-1);
								//alert(counts);
								// when a item_name_auto is selected, populate related fields in this form
								//this.form.item_price.value = ui.item.item_price;
								//$('#unit_measurements' + counts).val(ui.item.unit_measurements);
								//$('#price' + counts).val(ui.item.price);
								//$('#close_stock' + counts).val(ui.item.close_stock);
							}
						});
					});
					//total calculation by selecting tax value
					$('.txt1').change(function () {
						//alert($("#tax").val());
						tax = $("#tax").val();
						tt = $("#total_amount").val();
						//alert(tt);
						var ba=$('#basic_amount').val();
						var tax=$('#tax').val();
						var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
						var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
						$('#tax_amount').val(tax_a);
						$('#tax_amount').blur();
						$('#total_amount').val(total);
						$('#total_amount').blur();
						var result = (parseFloat(ba));
						if (!isNaN(result)) {
							$('#total_price').val(result);
							$('#total_price').blur();
						}
						// initialize the sum (total price) to zero
						var sum = 0;
						$('.txt').each(function() {
							sum += Number($(this).val());
						});
						$('#tot').val(sum.toFixed(2));
						$('#tot').blur();
					});
					$('#price'+counter).on('keyup',function() {
						var id = $(this).attr('id');
						id_length = id.length;
						if(id_length == 6) {
							counts = id.substr(-1);
							var a = $("#quantity"+counts).val();
							var b = $("#price"+counts).val();
							//alert(a);
							//alert(b);
							var s = (parseFloat(a) * parseFloat(b)).toFixed(2);
							$('#basic_amount' + counts).val(s);
                                                        $('#total_amount'+ counts).val(s);
						}
						else
						{
							counts = id.substr(-2);
							var a = $("#quantity"+counts).val();
							var b = $("#price"+counts).val();
							//alert(a);
							//alert(b);
							var s = (parseFloat(a) * parseFloat(b)).toFixed(2);
							$('#basic_amount' + counts).val(s);
                                                        $('#total_amount'+ counts).val(s);
						}
					});
					$(document).on("keyup","#total_amount"+counter+",#tax"+counter,function() {
						var count = counter-1;
						var ba = $("#basic_amount"+count).val();
						var tax = $("#tax"+count).val();
						var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
						var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
						$('#tax_amount' + count).val(tax_a);
						$('#tax_amount' + count).blur();
						$('#total_amount' + count).val(total);
						$('#total_amount' + count).blur();	
					});	
				}
				//alert(counter);
				counter++;
			});
			$(wrapper).on('click', '.remove_button', function(e){ //Once remove button is clicked
				e.preventDefault();
				$(this).parent('div').remove(); //Remove field html
				x--; //Decrement field counter
				counter--;
				var sum = 0;
				$('.txt').each(function() {
					sum += Number($(this).val());
				});
				$('#tot').val(sum.toFixed(2));
				$('#tot').blur();
			});
			$(document).on("change",".batch_no",function(){
				var text = "";
				$('.product_details').each(function(){
					text += $(this).val()+' ';
				});
				text = text.substring(0,text.length-1);
				$('#products').val(text);
			});
		});
	</script>
	<script type='text/javascript'>
	
		$("#quantity,#price").keyup(function () {
			//$('#basic_amount').val($('#quantity').val() * $('#price').val());
			var a = $("#quantity").val();
			var b = $("#price").val();
			//alert(a);
			//alert(b);
			var s = (parseFloat(a) * parseFloat(b)).toFixed(2);
			$('#total_amount').val(s);
            $('#basic_amount').val(s);
		});
		$("#total_amount,#tax").change(function () {
			var ba=$('#basic_amount').val();
			var tax=$('#tax').val();
			var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
			var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
			$('#tax_amount').val(tax_a);
			$('#tax_amount').blur();
			$('#total_amount').val(total);
			$('#total_amount').blur();
			var result = (parseFloat(ba));
			if (!isNaN(result)) {
				$('#total_price').val(result);
				$('#total_price').blur();
			}
		});
	
	
		///total amount display
		$('#tax').change(function () {
			//alert($("#tax").val());
			tax = $("#tax").val();
			tt = $("#total_amount").val();
			var ba=$('#basic_amount').val();
			var tax=$('#tax').val();
			var tax_a=((parseFloat(ba) *  parseFloat(tax))/ 100).toFixed(2);
			var total=(parseFloat(ba) + parseFloat(tax_a)).toFixed(2);
			$('#tax_amount').val(tax_a);
			$('#tax_amount').blur();
			$('#total_amount').val(total);
			$('#total_amount').blur();
			var result = (parseFloat(ba));
			if (!isNaN(result)) {
				$('#total_price').val(result);
				$('#total_price').blur();
			}
			// initialize the sum (total price) to zero
			var sum = 0;
			$('.txt').each(function() {
				sum += Number($(this).val());
			});
			$('#tot').val(sum.toFixed(2));
			$('#tot').blur();
		});
	
/*
		$('#quantity').on('keyup',function() {
			var qty = $('#quantity').val();
			var price = $('#price').val();
			alert(qty);
			var result = (parseFloat(qty) * parseFloat(price)).toFixed(2);
			//var basic = $('#quantity').val() * $('#price').val();
			if (!isNaN(result)) {
				$('#total_amount').val(result);
				$('#basic_amount').val(result);
			}
		});
		
		$('#price').on('keyup',function() {
			var a = $("#quantity").val();
			var b = $("#price").val();
			//alert(a);
			//alert(b);
			var s = (parseFloat(a) * parseFloat(b)).toFixed(2);
			$('#basic_amount').val(s);
            $('#total_amount').val(s);
		});*/
	</script>

	<script type="text/javascript">
		
		$("#item_name").autocomplete({ 
			minLength: 1,
			source: '<?php echo 
			base_url();?>index.php/Master/get_auoto_item_name',
			select:function(evt, ui)
			{	
				//alert(ui.item.price);
				$('#price').val(ui.item.price);
					$('#price').blur();
			}	
		});
	</script>
	<script type="text/javascript">
	
		jQuery(document).ready(function(){
			$('#vender_name').autocomplete({
				source:'<?php echo 
			base_url();?>index.php/Master/supplier_name_auto', 
				minLength:1,
			});
		});
	</script>
	
	<script>
	$(window).on('beforeunload', function () {
         //$("input[type=submit], input[type=button]").prop("disabled", "disabled");
		 $("input[type=submit]").prop("disabled", "disabled");
		 $("#submit").css("background-color","#dcdcdc");
    });
</script>
  </body>
</html>