<div class="content">
<form id="frmPurchase" name="frmPurchase" data-parsley-validate novalidate method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="billtype" id="billtype" value="<?php echo $data['ftyp_module']; ?>">
<input type="hidden" name="ftyp_id" id="ftyp_id" value="<?php echo $data['ftyp_id']; ?>">
<input type="hidden" name="txtsubtotal" id="txtsubtotal" value="0">
	<div class="main-content">
		<!-- Puchase detail -->		
		<div class="widget widget-table">
			<div class="widget-header">
				<h3> <i class="fa fa-id-card-o" aria-hidden="true"></i> {{ __('incexp.loan_voucher') }}</h3>
				<?php
				/*
				<div class="btn-group widget-header-toolbar">
					<a class="btn-borderless btn-focus clickme" href="purchaseView" title="Focus" ><i class="fa fa-eye"></i></a>
				</div>
				*/
				?>
			</div>
			<div class="widget-content">
					<div class="row">
						<div class="col-md-3">
							<!-- ADVANCED VALIDATION -->
								<div class="form-group">
									<label for="text-input1">{{ __('incexp.billno') }}*</label>
									<input type="text" name="billno" id="billno" class="form-control" readonly value="{{ $data['slno'] }}">
									<small class="text-danger" id="erritemcode"></small>
								</div>
							<!-- END ADVANCED VALIDATION -->
						</div>
						
						<div class="col-md-5">
							<!-- ADVANCED VALIDATION -->
								<div class="form-group">
									<label for="text-input1">{{ __('incexp.billname') }}*</label>
									<input type="text" name="name" id="name" class="form-control" readonly value="{{ $data['fundtype'][0]->ftyp_fundtype }}" >
									<small class="text-danger" id="erritemtype"></small>
								</div>
							<!-- END ADVANCED VALIDATION -->
						</div> 
						
						<div class="col-md-4">
							<!-- ADVANCED VALIDATION -->
								<div class="form-group">
									<label for="text-input1">{{ __('incexp.loandate') }}*</label>
									<input type="date" name="billdate" id="billdate" class="form-control"value="<?php echo date('Y-m-d');?>" data-date-format="yyyy-mm-dd" >
									<small class="text-danger" id="erritemtype"></small>
								</div>
							<!-- END ADVANCED VALIDATION -->
						</div> 
					</div>		
			</div>
		</div>
		<!-- END Puchase detail -->		
		
		
		<!-- ITEM RECORD STARTS HERE-->		
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-book" aria-hidden="true"></i> {{ __('incexp.loan_details') }}</h3> 
			</div>
			<div class="widget-content">
				<div class="row">
					<div class="col-sm-8 nopadding">
						<div class="form-group">
							<label for="multiselect1">{{ __('incexp.loan_details') }}*</label>
							<br/>
							<input type="text" name="itemname[]" id="itemname" class="form-control" >
							<small class="text-danger" id="erritemname"></small>
						</div>
					</div>
					<div class="col-sm-4 nopadding">
						<div class="form-group">
							<label for="multiselect1">{{ __('incexp.loan_amt') }}*</label>
							<br/>
							<input type="text" name="itemcost[]" id="itemcost0" class="form-control" onkeyup="calculate(0)" >
							<small class="text-danger" id="errstatus"></small>
						</div>
					</div>
<input type="hidden" name="itemquantity[]" id="itemquantity0" class="form-control"  value="1" >
<input type="hidden" name="itemtotal[]" id="itemtotal0" onkeyup="findTotal()" class="form-control" readonly>
					<div class="clear"></div>
				</div>
				<?php
				/*
				<div id="education_fields"  class="row">
				</div>
				
				<div class="row">
					<div class="col-sm-3 nopadding">
						<div class="form-group">
							<div class="input-group">
								<div class="input-group-btn">
<button type="button"  value="Add" class="form-control" onclick="education_fields()" >ADD</button>
								</div>
							</div>
						</div>
					</div>
				</div>
				*/
				?>
			</div>
			<div class="widget-header">
					<h3> Total - Rs. <label id="lblsubtotal">0</label></h3> 
			</div>			
		</div>
		<!-- ITEM RECORD ENDS HERE -->
		
		<!-- GRAND TOTAL WITH BUTTON STARTS HERE -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-comment-o" aria-hidden="true"></i> {{ __('incexp.credited_details') }}</h3> 
			</div>
			<div class="widget-content">									
					<div class="col-md-12">
						<div class="form-group">
							<textarea class="form-control" name="billingdescription" id="billingdescription"  ></textarea>
							<small class="text-danger" id="errdescription" ></small>
						</div>
						<!-- END ADVANCED VALIDATION -->
					</div>                      
			
			</div>
		</div>
		<!-- GRAND TOTAL WITH BUTTON ENDS HERE -->
		<div class="widget widget-table">
			<div class="widget-header">
				<center><button  class="btn btn-primary billgenerate">Click Here to Add Loan Voucher</button></center>
			</div>
		</div>
	</div>		
	</form>	
</div>	
<!-- /main -->
<!-- ###### The Modal Payment Receipt starts here ###### -->
<div class="modal" id="modprintpaymentreceipt">
		<div class="modal-dialog paymentmodal">
		  <div class="modal-content">
			<!-- Modal Header -->
			<div class="modal-header">
			  <h4 class="modal-title">Payment Receipt...</h4>
			</div>
			
			<!-- Modal body -->
			<div class="modal-body" id="divbillprint">
				<!-- PRINT RECEIPT START HERE -->
<div class="widget widget-table">
	<div class="widget-content">
	
		<div class="row">
			<div class="col-md-12">
				<center>
					<center><img src="img/printlogo.png" style="width: 100%;"></center>
				</center>
				<hr>
				<center><b>LOAN VOUCHER</b></center>
				<hr>
			</div>
			
			<div class="col-md-6">
				<b>{{ __('mylanguage.billno') }} <label id="receiptbillno"></label></b></b><br>
				<b>{{ __('mylanguage.name') }} <label id="receiptname"></label></b>
			</div>
			<div class="col-md-6 text-right">
				<b>{{ __('mylanguage.date') }} <label id="receiptdate" >00-00-0000</label></b>
			</div>

			<div class="col-md-12"><hr></div>

			<div class="col-md-12" id="receipttable"></div>
			
<input type="hidden" name="transaction_id" id="transaction_id" value="">

			<div class="col-md-6 text-left"><font style="border:1px solid #cecece;padding: 10px;">Cheque Detail: <label id="trnnote"></label></font></div>
			<div class="col-md-6 text-right"><h1 style="border:1px solid #cecece;">Rs. <label id="receiptamt1">000</label>/-</h2></div>
			
		</div>
	</div>
</div>
<!-- PRINT RECEIPT ENDS HERE -->
			</div>
			<!-- Modal footer -->
			<div class="modal-footer">			
				<button type="button" class="btn btn-primary"  onclick="printDiv('divbillprint')">Print</button>
				<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> 
			</div>
			
		  </div>
		</div>
</div>
<!-- ###### The Modal Payment Receipt ends here ###### -->
<script>
	$('.clickme').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.view').click(function(e)
	{
		$('#sideMenu').load('purchaseView');
	});
	
	//### Calculate individual Item cost starts here
	function  calculate(number) {
		var summs;
		var nValue;
		var amount;
		
		var price=$('#itemcost'+number).val();
		nValue = document.getElementById("itemquantity"+number).value;
		amount=(nValue*price);
		
		document.getElementById("itemtotal"+number).value=amount ;	
		findTotal();
	}
	//### Calculate individual Item cost ends here
	
	
	//###Coding to create new row for items starts here
	var room = 0;
	function education_fields() 
	{
		room++;
		var objTo = document.getElementById('education_fields');
		var divtest = document.createElement("div");
		divtest.setAttribute("class", "form-group removeclassa"+room);
		var rdiv = 'removeclass'+room;
		divtest.innerHTML = '<div class="col-sm-4 nopadding"><div class="form-group"><input type="text" name="itemname[]" id="itemname'+ room +'" class="form-control" required ></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" onkeyup="calculate('+ room +')" name="itemcost[]" id="itemcost'+ room +'" class="form-control"></div></div><div class="col-sm-2 nopadding"><div class="form-group"><input type="text" name="itemquantity[]" id="itemquantity'+ room +'" class="form-control" onkeyup="calculate('+ room +')" ></div></div><div class="col-sm-3 nopadding"><div class="form-group"><input type="text" name="itemtotal[]"  id="itemtotal'+ room +'" class="form-control" readonly ></div></div><div class="col-sm-1 nopadding"><div class="form-group"><div class="input-group"> <div class="input-group-btn"><button type="button" class="form-control" onclick="remove_education_fields('+ room +');" ><i class="fa fa-times-circle" aria-hidden="true" style="color: red;"></i></button></div></div></div></div><div class="clear"></div>';		
		objTo.appendChild(divtest)
		$('#num').val(room);
		ijk=$('#num').val(room);
	}
	//###Coding to create new row for items ends here
	//###Coding to remove row of items starts here
	function remove_education_fields(rid) {
		$('.removeclassa'+rid).remove();
		findTotal();
	}
	//###Coding to remove row of items ends here
	
	//### Total amount of the items starts here
	function findTotal()//quantity
	{
		var sum=0;
		var inps = document.getElementsByName('itemtotal[]');
		for (var i = 0; i <inps.length; i++) 
		{
			sum = sum + parseFloat(inps[i].value);
			//alert("pname["+i+"].value="+inp.value);
		}
		document.getElementById('lblsubtotal').innerHTML=sum;
		document.getElementById('txtsubtotal').value=sum;
	}
	//### Total amount of the items ends here

	//### Billing submission entry code starts here
	flag=0;
	$('.billgenerate').click(function(e)
	{
		var _token = $("input[name='_token']").val();
		var ftyp_id = $("input[name='ftyp_id']").val();
		var billtype = $("input[name='billtype']").val();
		var billno = $("input[name='billno']").val();
		var name = $("input[name='name']").val();	
		var txtsubtotal = $("input[name='txtsubtotal']").val();	
		var billdate = $("input[name='billdate']").val();
		var bil_note = $("#billingdescription").val();
		var bil_note = $("#billingdescription").val();
		//itemname[] itemcost[] itemquantity[] itemtotal[]		
		arritemnames = [];
		arritemcosts = [];
		arritemquantities = [];
		var itemname= document.getElementsByName("itemname[]");
		var itemcost= document.getElementsByName("itemcost[]");
		var itemquantity= document.getElementsByName("itemquantity[]");
		for (index = 0; index < itemname.length; ++index) {
			arritemnames[index] = itemname[index].value;
			arritemcosts[index] = itemcost[index].value;
			arritemquantities[index] = itemquantity[index].value;
		}
		
		$.ajax({
			url: "ieexpensevoucher",
			type:'POST',
			dataType: 'json',
    //cache : false,
	//processData: false,
			data: {_token:_token,billtype:billtype, billno:billno,name:name, billdate:billdate,bil_note:bil_note,itemname:arritemnames,itemcost:arritemcosts,itemquantity:arritemquantities,ftyp_id:ftyp_id,txtsubtotal:txtsubtotal},
			success: function(data)
			{
				if(data.status == "Success")
				{
					alert("Loan Voucher generated successfully...");
					BillPrint(data.bil_id);
				}
				else
				{
					alert("Failed to Generate Loan Voucher...");
					//printErrorMsg(data.message)
				}
			}
		});
		return false;
	});
	//### Billing submission entry code ends here
	function BillPrint(transaction_id)
	{
		$("#modprintpaymentreceipt").modal();
		$.ajax({
			url: 'printloanbill',
			type: 'get',
			data:'&transaction_id='+transaction_id,
			success: function(data)
			{
				//Update balance in table starts here
				$('#tot_'+data.trn_transaction.cst_id).html(""+data.bil_billrec[0].totalamt);
				paidamt= parseFloat(parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
				$('#paid_'+data.trn_transaction.cst_id).html(""+ paidamt);
				amt= parseFloat(data.bil_billrec[0].totalamt) - (parseFloat(data.bil_billrec[0].paidamt) + parseFloat(data.bil_billrec[0].advamt) + parseFloat(data.bil_billrec[0].prepayamt));
				$('#bal_'+data.trn_transaction.cst_id).html(""+amt);
				//Update balance in table ends here							
				document.getElementById("receiptbillno").innerHTML=data.trn_billno;
				//document.getElementById("receiptmember_id").innerHTML= data.cst_grade_prefix + "-" + data.cst_code;
				document.getElementById("receiptdate").innerHTML=data.trn_date;
				document.getElementById("receiptname").innerHTML=data.cst_name;
				document.getElementById("receiptamt1").innerHTML=data.bilr_gtotal;
				document.getElementById("transaction_id").value=transaction_id;
				document.getElementById("trnnote").innerHTML=data.trn_note;
				$('#receipttable').html(data.transrecord);
			}
		});
	}
	
	function printDiv(divName)
	{
		var transaction_id = document.getElementById("transaction_id").value;
		window.open('printexpensebillvoucher?transaction_id=' + transaction_id + '&ftyp_module=Loan' ,'_blank');
	}
	
	$('#modprintpaymentreceipt').on('hidden.bs.modal', function (e) {
	  $('#sideMenu').load('viewincomeexpense?ftyp_module=Loan');
	})
</script>