<?php
    $this->load->view('admin/master/header_2');
	$modul=$this->uri->segment(3);
	$menuid=$this->uri->segment(4);

	$ID=$this->uri->segment(5);

	setlocale(LC_ALL, 'id_ID.UTF-8');

	if($modul == 'purchase_preview')
	{
		$readonly = 'readonly="readonly"';
	}else
	{
		$readonly = '';
	}
?>

<div class="content-wrapper">
    <section class="content-header">
        <h1>Edit Purchase
            <small>Control panel</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="<?php echo base_url();?>admin/home/index/8"><i class="fa fa-dashboard"></i> Home</a></li>
            <li><a href="<?php echo base_url();?>admin/transaction/purchase/<?php echo $menuid;?>">Purchase</a></li>
            <li class="active">Edit Purchase</li>
        </ol>
    </section>
    <section class="content">
    	<?php echo $this->session->flashdata('message') ?>
        <?php echo $this->session->flashdata('mdt') ?>
	    <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title">Insert</h3>
                            <hr>
                            <form action="<?php echo base_url();?>admin/transaction/update_purchase/<?php echo $menuid.'/'.$ID;?>" method="post" enctype="multipart/form-data">
					            <div class="box-body">
					              	<div class="row">
						                <div class="col-md-4">
						                	<div class="form-group">
						                		<div class="form-line">
							                        <label>Purchase Order No *</label>
							                        <input type="text" name="purchase" class="form-control" placeholder="Purchase Order No" readonly="readonly" required="required" value="<?php echo $data_purchase[0]->purchase_no;?>">
							                    </div>
						                    </div>
						                    <div class="form-group">
						                    	<div class="form-line">
							                        <label>Date</label>
							                        <input type="text" name="date" id="datepicker" class="form-control" placeholder="Date" value="<?php echo date('m/d/Y', strtotime($data_purchase[0]->date_in));?>" <?php echo $readonly;?> >
							                    </div>
							                    <script type="text/javascript">
							                    	$(function(){
							                    		$("#datepicker").datepicker();
							                    	});
							                    </script>
						                    </div>
						                    <div class="form-group">
						                    	<div class="form-line">
							                        <label>Required Date</label>
							                        <input type="text" name="required_date" id="datepicker2" class="form-control" placeholder="Required Date" value="<?php echo date('m/d/Y', strtotime($data_purchase[0]->date_required));?>" <?php echo $readonly;?> >
							                    </div>
							                    <script type="text/javascript">
							                    	$(function(){
							                    		$("#datepicker2").datepicker();
							                    	});
							                    </script>
						                    </div>
						                </div>
						                <div class="col-md-4">
						                    <div class="form-group">
												<script>
													$(document).ready(function()
													{
														$(".supplier_code").bind('input', function(e)
														{
															var code = $(this).val();
															$.ajax({
																 type: "GET",
																 dataType: "html",
																 url: "<?php echo base_url();?>admin/transaction/get_supplier/<?php echo $menuid;?>",
																 data:"code="+code,
																 success: function(msg)
																 {
																	$("#supplier_code").html(msg);
																 }
															});
														});

														$(".supplier_get_detail").focusout(function(e)
														{
															var code = $(this).val();
															$("#code-supplier-result").show();
															$.ajax({
																 type: "POST",
																 dataType: "html",
																 url: "<?php echo base_url();?>admin/transaction/supplier_get_detail/<?php echo $menuid;?>",
																 data:"code="+code,
																 success: function(msg)
																 {
																	if(code=='')
																	{
																		$("#code-supplier-result").hide();
																		$("#supplier_name_2").val('');
																		$(".payment_terms").val('');
																	}else
																	{
																		var data_supplier = msg;
																		var explode = data_supplier.split("%");

																		var supplier_name  = explode[0];
																		var payment_terms  = explode[1];
																		var check   		  = explode[2];

																		if(check=='exist')
																		{
																			$("#code-supplier-result").hide();
																			$("#supplier_name_2").val(supplier_name);
																			$(".payment_terms").val(payment_terms);
																		}else
																		{
																			$("#code-supplier-result").html('<font style="color:#FF024F"><i class="fa fa-times"></i> Code does not exist</font>');
																			$("#supplier_name_2").val('');
																			$(".payment_terms").val('');
																		}
																	}
																 }
															});
														});
													});
												</script>
												<div class="form-line">
							                        <!-- <label>Supplier</label>
							                        <span id="code-supplier-result" style="display:none"><i class="fa fa-refresh fa-spin"></i></span>
							                        <input type="text" name="id_supplier" list="supplier_code" class="form-control supplier_code supplier_get_detail" placeholder="Supplier Code" id="supplier_code_2" required="required" value="<?php echo $data_purchase[0]->code;?>" <?php echo $readonly;?> >
							                        <datalist id="supplier_code"></datalist> -->
							                        <div class="form-line">
						                        	<label>Supplier</label>
						                        	<select class="form-control supplier_get" name="id_supplier" style="width: 100%;">
											              <option value="">Select Supplier</option>
											              <?php $supplier=$this->master_model->mst_data('ms_supplier');
											              ?>
											              <?php foreach ($supplier as $key => $value): ?>
											              	<?php if($data_purchase[0]->id_supplier == $value->ID): ?>
											              		<?php $select='selected="selected"'; ?>
											              	<?php else: ?>
											              		<?php $select=''; ?>
											              	<?php endif ?>
											              		<option value="<?php echo $value->ID ?>" <?php echo $select; ?>>
											                      <?php echo $value->code.' - '.$value->name; ?>
											                    </option>
											              <?php endforeach ?>
											              
											        </select>
						                        </div>
							                    </div>
						                    </div>
						                    <!-- <div class="form-group"> -->
												<script>
													$(document).ready(function()
													{
														$(".supplier_name").bind('input', function(e)
														{
															var name = $(this).val();
															$.ajax({
																 type: "GET",
																 dataType: "html",
																 url: "<?php echo base_url();?>admin/transaction/get_supplier_2/<?php echo $menuid;?>",
																 data:"name="+name,
																 success: function(msg)
																 {
																	$("#supplier_name").html(msg);
																 }
															});
														});

														$(".supplier_get_detail_2").focusout(function(e)
														{
															var name = $(this).val();

															$("#name-supplier-result").show();
															$.ajax({
																 type: "POST",
																 dataType: "html",
																 url: "<?php echo base_url();?>admin/transaction/supplier_get_detail_2/<?php echo $menuid;?>",
																 data:"name="+name,
																 success: function(msg)
																 {
																	if(name=='')
																	{
																		$("#name-supplier-result").hide();
																		$("#supplier_code_2").val('');
																		$(".payment_terms").val('');
																	}else
																	{
																		var data_supplier = msg;
																		var explode = data_supplier.split("%");

																		var supplier_code  = explode[0];
																		var payment_terms  = explode[1];
																		var check   		  = explode[2];

																		if(check=='exist')
																		{
																			$("#name-supplier-result").hide();
																			$("#supplier_code_2").val(supplier_code);
																			$(".payment_terms").val(payment_terms);
																		}else
																		{
																			$("#name-supplier-result").html('<font style="color:#FF024F"><i class="fa fa-times"></i> Name does not exist</font>');
																			$("#supplier_code_2").val('');
																			$(".payment_terms").val('');
																		}
																	}
																 }
															});
														});
													});
												</script>
												<!-- <div class="form-line">
							                        <label>Supplier Name</label>
													<span id="name-supplier-result" style="display:none"><i class="fa fa-refresh fa-spin"></i></span>
							                        <input type="text" name="supplier_name" list="supplier_name" id="supplier_name_2" class="form-control supplier_name supplier_get_detail_2" placeholder="Supplier Name" required="required" value="<?php echo $data_purchase[0]->name;?>" <?php echo $readonly;?> >
							                        <datalist id="supplier_name"></datalist>
							                    </div> -->
						                    <!-- </div> -->
						                	<div class="form-group">
						                		<div class="form-line">
							                        <label>Payment Terms</label>
							                        <input type="text" name="payment_term" class="form-control payment_terms" placeholder="Days" value="<?php echo $data_purchase[0]->payment_term;?>" <?php echo $readonly;?>>
							                    </div>
						                    </div>
						                    <div class="form-group">
					                    		<div class="form-line">
					                        		<label>PPN :</label>
					                        		<?php if ($data_purchase[0]->status == 1) {
					                        			$ppn = "Yes";
					                        		}else{
					                        			$ppn = "NO";
					                        		} ?>
					                        		
					                        		<input type="text" name="status" class="form-control st" value="<?php echo $ppn;?>" <?php echo $readonly;?>>
					                    		</div>
					                    	</div>
						                </div>
						                <div class="col-md-4">
						                    <div class="form-group">
						                    	<div class="form-line">
							                        <label>Ship Via</label>
							                        <input type="text" name="ship_via" class="form-control phone" placeholder="Ship Via" value="<?php echo $data_purchase[0]->ship_via;?>" <?php echo $readonly;?>>
							                    </div>
						                    </div>
						                    <div class="form-group">
												<script type="text/javascript">
													$(document).ready(function()
													{
														exist2 = 0;
														$("#ref").change(function (e)
														{
															var code = $(this).val();
															$("#code-result").show();
															$.ajax({
																 type:"POST",
																 url: "<?php echo base_url();?>admin/master/check_code_supplier",
																 data:"code="+code,
																 success:function(msg)
																 {
																	 if(msg == 1)
																	 {
																		 exist2 = 1;
																		 $("#code-result").html('<font style="color:#FF024F"><i class="fa fa-times"></i> Number already in use</font>');
																	 }else if(msg==0)
																	 {
																		 exist2 = 0;
																		 $("#code-result").html('<font style="color:#2BB0EA"><i class="fa fa-check" style="color:#2DBE00"></i> Number not in use</font>');
																	 }else if(msg==2)
																	 {
																		 exist2 = 2;
																		 $("#code-result").html('');
																	 }
																 }
															});
														});

														$(".save").click(function()
														{
															if(exist2 == 1)
															{
																alert('Number already in use');
																return false;
															}else
															{
																return true;
															}
														});

													});
												</script>
												<div class="form-line">
							                        <label>Ref. No</label>
													<span id="code-result" style="display:none"><i class="fa fa-refresh fa-spin"></i></span>
							                        <input type="text" name="ref_no" id="ref" class="form-control" placeholder="Ref. No" value="<?php echo $data_purchase[0]->ref_no;?>" <?php echo $readonly;?> >
							                    </div>
						                    </div>
						                    <div class="form-group">
						                    	<div class="form-line">
							                        <label>Status</label>
							                        <br />
							                        <input type="text" name="status" class="form-control" value="Pending" readonly="readonly" value="<?php echo $data_purchase[0]->status;?>">
							                    </div>
						                    </div>
						                </div>
					              	</div>
					              	<div class="table-responsive">
						              	<table class="table table-bordered table-striped table-hover js-basic-example dataTable">
						                    <thead>
						                        <tr>
						                            <th>No.</th>
						                            <th>Code</th>
						                            <th>Item</th>
						                            <th>Qty</th>
						                            <th>In Qty</th>
						                            <th>Unit Price</th>
						                            <th>Gross Ammount</th>

						                            <th>Delete</th>
						                        </tr>
						                    </thead>
						                    <tbody>
						                        <?php
													// $data_purchase_detail=$this->master_model->select_in('ts_purchase_detail','*',"WHERE id_purchase=$ID");

													$data_purchase_detail = $this->db->query('
															SELECT ts_purchase_detail.ID,
														        ts_purchase_detail.id_purchase,
														        ts_purchase_detail.id_inventory,
														        ts_purchase_detail.qty,
														        ts_purchase_detail.price,
														        ts_purchase_detail.gross_ammount,
														        ts_purchase_detail.in_qty,
														        ts_purchase_detail.status,
														        ts_purchase_detail.sort,
														        ms_inventory.code,
														        ms_inventory.name
														        FROM ts_purchase_detail
														        INNER JOIN ms_inventory ON ms_inventory.ID = ts_purchase_detail.id_inventory
														        WHERE ts_purchase_detail.id_purchase = '.$ID.'
														')->result();

													$count_detail=count($data_purchase_detail);

													for($a=0; $a < count($data_purchase_detail); $a++)
													{
														$b=$a+1;
												?>
						                        	<tr>
						                                <td>
						                                    <?php echo $b;?>
						                                    <input type="hidden" name="id_purchase_detail[]" value="<?php echo $data_purchase_detail[$a]->ID;?>">
						                                </td>
						                                <td>
						                                    <script>
															$(document).ready(function()
															{
																$(".inventory_code_<?php echo $b;?>").bind('input', function(e)
																{
																	var code = $(this).val();
																	$.ajax({
																		 type: "GET",
																		 dataType: "html",
																		 url: "<?php echo base_url();?>admin/transaction/get_code_inventory/<?php echo $menuid;?>",
																		 data:"code="+code,
																		 success: function(msg)
																		 {
																			$("#inventory_code_<?php echo $b;?>").html(msg);
																		 }
																	});
																});


																$(".inventory_get_detail_<?php echo $b;?>").focusout(function(e)
																{
																	var code = $(this).val();

																	$.ajax({
																		 type: "POST",
																		 dataType: "html",
																		 url: "<?php echo base_url();?>admin/transaction/inventory_get_detail/<?php echo $menuid;?>",
																		 data:"code="+code,
																		 success: function(msg)
																		 {
																			if(code=='')
																			{
																				$("#inventory_names_<?php echo $b;?>").val('');
																				$("#qty_<?php echo $b;?>").val(0);
																			}else
																			{
																				var data_inventory = msg;
																				var explode = data_inventory.split("%");

																				var inventory_name  = explode[0];
																				var qty  			 = explode[1];
																				var check   		   = explode[2];

																				if(check=='exist')
																				{
																					$("#inventory_names_<?php echo $b;?>").val(inventory_name);
																					$("#qty_<?php echo $b;?>").val(qty);
																				}else
																				{
																					$("#inventory_names_<?php echo $b;?>").val('');
																					$("#qty_<?php echo $b;?>").val(0);

																					alert('Code does not exist');
																				}
																			}
																		 }
																	});
																});
															});
														</script>
						                            	<input type="text" name="code[]" placeholder="Code" list="inventory_code_<?php echo $b;?>" id="inventory_codes_<?php echo $b;?>" class="form-control inventory_code_<?php echo $b;?> inventory_get_detail_<?php echo $b;?>" style="width:100px" value="<?php echo $data_purchase_detail[$a]->code;?>" <?php echo $readonly;?> />
						                                <datalist id="inventory_code_<?php echo $b;?>"></datalist>
						                                </td>
						                                <td>
						                                    <script>
																$(document).ready(function()
																{
																	$(".inventory_name_<?php echo $b;?>").bind('input', function(e)
																	{
																		var name = $(this).val();
																		$.ajax({
																			 type: "GET",
																			 dataType: "html",
																			 url: "<?php echo base_url();?>admin/transaction/get_name_inventory/<?php echo $menuid;?>",
																			 data:"name="+name,
																			 success: function(msg)
																			 {
																				$("#inventory_name_<?php echo $b;?>").html(msg);
																			 }
																		});
																	});


																	$(".inventory_name_get_detail_<?php echo $b;?>").focusout(function(e)
																	{
																		var name = $(this).val();

																		$.ajax({
																			 type: "POST",
																			 dataType: "html",
																			 url: "<?php echo base_url();?>admin/transaction/inventory_name_get_detail/<?php echo $menuid;?>",
																			 data:"name="+name,
																			 success: function(msg)
																			 {
																				if(name=='')
																				{
																					$("#inventory_codes_<?php echo $b;?>").val('');
																					$("#qty_<?php echo $b;?>").val(0);
																				}else
																				{
																					var data_inventory = msg;
																					var explode = data_inventory.split("%");

																					var inventory_code  = explode[0];
																					var qty  			 = explode[1];
																					var check   		   = explode[2];

																					if(check=='exist')
																					{
																						$("#inventory_codes_<?php echo $b;?>").val(inventory_code);
																						$("#qty_<?php echo $b;?>").val(qty);
																					}else
																					{
																						$("#inventory_codes_<?php echo $b;?>").val('');
																						$("#qty_<?php echo $b;?>").val(0);

																						alert('Item does not exist');
																					}
																				}
																			 }
																		});
																	});
																});
															</script>
															<input type="text" name="item[]" list="inventory_name_<?php echo $b;?>" placeholder="Item" id="inventory_names_<?php echo $b;?>" class="form-control inventory_name_<?php echo $b;?> inventory_name_get_detail_<?php echo $b;?>" style="width:250px" value="<?php echo $data_purchase_detail[$a]->name;?>" <?php echo $readonly;?> />
															<datalist id="inventory_name_<?php echo $b;?>"></datalist>
						                                </td>
						                                <td>
															<script>
						                                        $(document).ready(function()
						                                        {
						                                        	$("#qty_1").focus();

						                                            $(".qty_subtotal_<?php echo $b;?>").focusout(function(e)
						                                            {
						                                                var qty = $(this).val();
																		var in_qty = parseFloat($("#in_qty_<?php echo $b;?>").val());
						                                                var price = $("#price_<?php echo $b;?>").val();

																		if(qty < in_qty)
																		{
																			var x = document.getElementById("snackbar")
																			x.className = "show";
																			x.innerHTML = "Qty should not be smaller than in qty!";
																			setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);

																			$("#qty_<?php echo $b;?>").val(in_qty);
																		}else
																		{
																			$.ajax({
																				 type: "POST",
																				 dataType: "html",
																				 url: "<?php echo base_url();?>admin/transaction/qty_subtotal/"+price+"/<?php echo $menuid;?>",
																				 data:"qty="+qty,
																				 success: function(msg)
																				 {
																					$(".subtotal_<?php echo $b;?>").html(msg);
																				 }
																			});
																		}
						                                            });
						                                        });
						                                    </script>
						                                    <input type="text" name="qty[]" id="qty_<?php echo $b;?>" style="width:70px; float:left" class="form-control qty_subtotal_<?php echo $b;?>" value="<?php echo $data_purchase_detail[$a]->qty;?>" <?php echo $readonly;?> />
						                                </td>

						                                <td align="center">
						                                    <?php
						                                    	echo $data_purchase_detail[$a]->in_qty;
															?>
						                                    <input type="hidden" id="in_qty_<?php echo $b;?>" value="<?php echo $data_purchase_detail[$a]->in_qty;?>" />
						                                </td>

						                                <td>
						                                    <script>
						                                        $(document).ready(function()
						                                        {
						                                            $(".price_subtotal_<?php echo $b;?>").focusout(function(e)
						                                            {
						                                                var price = $(this).val();
						                                                var qty = $("#qty_<?php echo $b;?>").val();

						                                                qty=qty.replace(/,/g , '.');

						                                                $.ajax({
						                                                     type: "POST",
						                                                     dataType: "html",
						                                                     url: "<?php echo base_url();?>admin/transaction/price_subtotal/"+qty+"/<?php echo $menuid;?>",
						                                                     data:"price="+price,
						                                                     success: function(msg)
						                                                     {
						                                                     	$("#qty_<?php echo $d;?>").val(qty);
						                                                        $(".subtotal_<?php echo $b;?>").html(msg);
						                                                     }
						                                                });
						                                            });
						                                        });
						                                    </script>
						                                    <input type="text" name="price[]" id="price_<?php echo $b;?>" style="width:80px;" class="form-control price_subtotal_<?php echo $b;?>" placeholder="Price" value="<?php echo $data_purchase_detail[$a]->price;?>" <?php echo $readonly;?>/>
						                                </td>
						                                <td align="center" class="subtotal_<?php echo $b;?>">
						                                    <?php echo number_format($data_purchase_detail[$a]->gross_ammount);?>
						                                    <input type="hidden" name="subtotal[]" style="width:80px;" class="form-control" placeholder="Subtotal" value="<?php echo $data_purchase_detail[$a]->gross_ammount;?>"/>
						                                </td>

						                                <td style="text-align:center">
						                                	<?php

						                                		if($modul == 'purchase_preview')
						                                		{
						                                			echo'<a class="text-gray" style="cursor:pointer">
																			<i class="fa fa-trash-o"></i>
																		</a>';
						                                		}else
						                                		{
						                                			$in_qty=$data_purchase_detail[$a]->in_qty;

																	if($in_qty==0)
																	{
																		echo'
																			<script>
																				$(document).ready(function()
																				{
																					$(".delete_'.$b.'").click(function(e)
																					{
																						var row ='.$b.';
																						$("#inventory_codes_'.$b.'").val("");
																						$("#qty_'.$b.'").val(0);
																						$("#inventory_names_'.$b.'").val("");
																						$("#price_'.$b.'").val(0);
																						$(".subtotal_'.$b.'").html("0 <input type="hidden" name="subtotal[]" style="width:80px;" class="form-control" placeholder="Subtotal"/>");

																						var x = document.getElementById("snackbar")
																						x.className = "show";
																						x.innerHTML = "Fill is empty!";
																						setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);

																						if(row > 1)
																						{
																							$(this).closest("tr").remove();
																						}
																					});
																				});
																			</script>
																			<a alt="'.$b.'" class="text-red delete_'.$b.'" style="cursor:pointer">
																				<i class="fa fa-trash-o"></i>
																			</a>
																		';
																	}else
																	{
																		echo'-';
																	}
						                                		}

															?>

						                                </td>
						                            </tr>
						                        <?php } ?>


						                        <?php
													for($c=0; $c < 10; $c++)
													{
														$d=$c+$count_detail;
												?>
						                        	<tr id="row2<?php echo $c;?>" style="display:none" >
						                                <td>
						                                    <?php echo $d;?>
																								<input type="hidden" name="id_purchase_detail[]" value="1111111">
						                                </td>
						                                <td>
						                                    <script>
															$(document).ready(function()
															{
																$(".inventory_code_<?php echo $d;?>").bind('input', function(e)
																{
																	var code = $(this).val();
																	$.ajax({
																		 type: "GET",
																		 dataType: "html",
																		 url: "<?php echo base_url();?>admin/transaction/get_code_inventory/<?php echo $menuid;?>",
																		 data:"code="+code,
																		 success: function(msg)
																		 {
																			$("#inventory_code_<?php echo $d;?>").html(msg);
																		 }
																	});
																});


																$(".inventory_get_detail_<?php echo $d;?>").focusout(function(e)
																{
																	var code = $(this).val();

																	$.ajax({
																		 type: "POST",
																		 dataType: "html",
																		 url: "<?php echo base_url();?>admin/transaction/inventory_get_detail/<?php echo $menuid;?>",
																		 data:"code="+code,
																		 success: function(msg)
																		 {
																			if(code=='')
																			{
																				$("#inventory_names_<?php echo $d;?>").val('');
																				$("#qty_<?php echo $d;?>").val(0);
																			}else
																			{
																				var data_inventory = msg;
																				var explode = data_inventory.split("%");

																				var inventory_name  = explode[0];
																				var qty  			 = explode[1];
																				var check   		   = explode[2];

																				if(check=='exist')
																				{
																					$("#inventory_names_<?php echo $d;?>").val(inventory_name);
																					$("#qty_<?php echo $d;?>").val(qty);
																				}else
																				{
																					$("#inventory_names_<?php echo $d;?>").val('');
																					$("#qty_<?php echo $d;?>").val(0);

																					alert('Code does not exist');
																				}
																			}
																		 }
																	});
																});
															});
														</script>
						                            	<input type="text" name="code[]" placeholder="Code" list="inventory_code_<?php echo $d;?>" id="inventory_codes_<?php echo $d;?>" class="form-control inventory_code_<?php echo $d;?> inventory_get_detail_<?php echo $d;?>" style="width:100px"/>
						                                <datalist id="inventory_code_<?php echo $d;?>"></datalist>
						                                </td>
						                                <td>
						                                    <script>
																$(document).ready(function()
																{
																	$(".inventory_name_<?php echo $d;?>").bind('input', function(e)
																	{
																		var name = $(this).val();
																		$.ajax({
																			 type: "GET",
																			 dataType: "html",
																			 url: "<?php echo base_url();?>admin/transaction/get_name_inventory/<?php echo $menuid;?>",
																			 data:"name="+name,
																			 success: function(msg)
																			 {
																				$("#inventory_name_<?php echo $d;?>").html(msg);
																			 }
																		});
																	});


																	$(".inventory_name_get_detail_<?php echo $d;?>").focusout(function(e)
																	{
																		var name = $(this).val();

																		$.ajax({
																			 type: "POST",
																			 dataType: "html",
																			 url: "<?php echo base_url();?>admin/transaction/inventory_name_get_detail/<?php echo $menuid;?>",
																			 data:"name="+name,
																			 success: function(msg)
																			 {
																				if(name=='')
																				{
																					$("#inventory_codes_<?php echo $d;?>").val('');
																					$("#qty_<?php echo $d;?>").val(0);
																				}else
																				{
																					var data_inventory = msg;
																					var explode = data_inventory.split("%");

																					var inventory_code  = explode[0];
																					var qty  			 = explode[1];
																					var check   		   = explode[2];

																					if(check=='exist')
																					{
																						$("#inventory_codes_<?php echo $d;?>").val(inventory_code);
																						$("#qty_<?php echo $d;?>").val(qty);
																					}else
																					{
																						$("#inventory_codes_<?php echo $d;?>").val('');
																						$("#qty_<?php echo $d;?>").val(0);

																						alert('Item does not exist');
																					}
																				}
																			 }
																		});
																	});
																});
															</script>
															<input type="text" name="item[]" list="inventory_name_<?php echo $d;?>" placeholder="Item" id="inventory_names_<?php echo $d;?>" class="form-control inventory_name_<?php echo $b;?> inventory_name_get_detail_<?php echo $d;?>" style="width:250px"/>
															<datalist id="inventory_name_<?php echo $b;?>"></datalist>
						                                </td>
						                                <td>
															<script>
						                                        $(document).ready(function()
						                                        {
						                                            $(".qty_subtotal_<?php echo $d;?>").focusout(function(e)
						                                            {
						                                                var qty = $(this).val();
						                                                var price = $("#price_<?php echo $d;?>").val();

						                                                qty=qty.replace(/,/g , '.');

						                                                $.ajax({
						                                                     type: "POST",
						                                                     dataType: "html",
						                                                     url: "<?php echo base_url();?>admin/transaction/qty_subtotal/"+price+"/<?php echo $menuid;?>",
						                                                     data:"qty="+qty,
						                                                     success: function(msg)
						                                                     {
						                                                     	$("#qty_<?php echo $d;?>").val(qty);
						                                                        $(".subtotal_<?php echo $d;?>").html(msg);
						                                                     }
						                                                });
						                                            });
						                                        });
						                                    </script>
						                                    <input type="text" name="qty[]" id="qty_<?php echo $d;?>" style="width:70px; float:left" class="form-control qty_subtotal_<?php echo $d;?>" value="0"/>
						                                </td>
						                                <td align="center">
						                                    0
						                                </td>
						                                <td>
						                                    <script>
						                                        $(document).ready(function()
						                                        {
						                                            $(".price_subtotal_<?php echo $d;?>").focusout(function(e)
						                                            {
						                                                var price = $(this).val();
						                                                var qty = $("#qty_<?php echo $d;?>").val();

						                                                $.ajax({
						                                                     type: "POST",
						                                                     dataType: "html",
						                                                     url: "<?php echo base_url();?>admin/transaction/price_subtotal/"+qty+"/<?php echo $menuid;?>",
						                                                     data:"price="+price,
						                                                     success: function(msg)
						                                                     {
						                                                        $(".subtotal_<?php echo $d;?>").html(msg);
						                                                     }
						                                                });
						                                            });
						                                        });
						                                    </script>
						                                    <input type="text" name="price[]" id="price_<?php echo $d;?>" style="width:80px;" class="form-control price_subtotal_<?php echo $d;?>" placeholder="Price" value="0"/>
						                                </td>
						                                <td align="center" class="subtotal_<?php echo $d;?>">
						                                    0
						                                    <input type="hidden" name="subtotal[]" style="width:80px;" class="form-control" placeholder="Subtotal" value="0"/>
						                                </td>

						                                <td style="text-align:center">
						                                    <script>
																$(document).ready(function()
																{
																	$(".delete_<?php echo $d;?>").click(function(e)
																	{
																		$("#inventory_codes_<?php echo $d;?>").val('');
																		$("#qty_<?php echo $d;?>").val(0);
																		$("#inventory_names_<?php echo $d;?>").val('');
																		$("#price_<?php echo $d;?>").val(0);
																		$(".subtotal_<?php echo $d;?>").html('0 <input type="hidden" name="subtotal[]" style="width:80px;" class="form-control" placeholder="Subtotal"/>');

																		var x = document.getElementById("snackbar")
																		x.className = "show";
																		x.innerHTML = "Row successfully deleted!";
																		setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);

																		$(this).closest("tr").remove();
																	});
																});
															</script>
															<a alt="<?php echo $d;?>" class="text-red delete_<?php echo $d;?>" style="cursor:pointer">
																<i class="fa fa-trash-o"></i>
															</a>
						                                </td>
						                            </tr>
						                        <?php } ?>

						                    </tbody>
						                    <tfoot>
						                    	<tr>
						                        	<td colspan="9">
						                        		<?php
						                        			if($modul == 'purchase_preview')
															{
																echo'';
															}else
															{
																?>
																	<a class="btn btn-primary addOption loading_btn" onClick="clickME()" id="addRow" data-loading-text="<i class='fa fa-circle-o-notch fa-spin'></i> Processing">
									                                    Add Row
									                                </a>
																<?php
															}
						                        		?>

						                                <script>
						                                    $('.loading_btn').on('click', function()
						                                    {
						                                        var $this = $(this);
						                                        $this.button('loading');
						                                        setTimeout(function()
						                                        {
						                                           $this.button('reset');
						                                        }, 500);
						                                    });
						                                </script>
						                                <script>
						                                    $(document).ready(function()
						                                    {
						                                        var inc = <?php echo $count_detail;?>;
						                                        $(".addOption").on('click',function()
						                                        {
						                                            inc=inc+1;
						                                            $("#row2"+inc+"").show();
						                                        });
						                                    });
						                                </script>
						                            </td>

						                        </tr>
						                        <tr>

						                        	<?php
						                    			if($modul == 'purchase_preview')
														{
															?>
																<td colspan="3">
									                            	<strong>Note : </strong>
									                                <input name="note" type="text" class="form-control" value="<?php echo $data_purchase[0]->note;?>" disabled="disabled"/>
									                            </td>
																<td colspan="8" valign="middle">
																	<button type="submit" class="btn btn-info btn-fill pull-right" id="save" style="margin-top:10px;">Save</button>
									                                <a href="<?php echo base_url();?>admin/transaction/purchase/<?php echo $menuid;?>" class="btn btn-danger pull-right" style="margin:10px 5px 0 0;">Back</a>
									                            </td>
															<?php
														}else
														{
															?>
																<td colspan="3">
									                            	<strong>Note : </strong>
									                                <input name="note" type="text" class="form-control" value="<?php echo $data_purchase[0]->note;?>" />
									                            </td>
									                        	<td colspan="6" valign="middle">
									                        		<?if ($in_qty == 0){
															         ?>
									                        		
									                            	<button type="submit" class="btn btn-info btn-fill pull-right" id="save" style="margin-top:10px;">Save</button>
									                            	<?}?>
									                                <a href="<?php echo base_url();?>admin/transaction/purchase/<?php echo $menuid;?>" class="btn btn-danger pull-right" style="margin:10px 5px 0 0;">Cancel</a>
									                                <button type="reset" class="btn btn-default pull-right" style="margin:10px 5px 0 0;">Reset</button>
									                            </td>
															<?php
														}
						                    		?>

						                        </tr>
						                    </tfoot>
						                </table>
						            </div>
					          	</div>
	            <!-- /.box-body -->

	        				</form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>
<div id="snackbar"></div>


<?php $this->load->view('admin/master/footer_2');?>