<script src="assets/js/king-table.js"></script>
<!-- MAIN CONTENT WRAPPER -->
<div class="content">	
	<div class="main-content">

		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> Add Loan Account detail</h3>
			</div>
			<div class="widget-content">
			<form id="frmLoanApplication" name="frmLoanApplication" data-parsley-validate novalidate method="POST">
				<table  class="table table-striped table-bordered">
					<?php  $i=1;?>
					@foreach($data['LoanApplicantRecords'] as $LoanApplicantRec)
				<input type="hidden" name="lacc_id" id="lacc_id" value="{{$LoanApplicantRec->lacc_id}}">
				<input type="hidden" name="lacc_no" id="lacc_no" value="{{$LoanApplicantRec->lacc_no}}">
						<tr>
							<th>Application No.</th><td><b>{{$LoanApplicantRec->lacc_no}}</b></td>	
							<th>Loan amount</th><td><b><input type="text" name="lacc_loanamt" id="lacc_loanamt" value="{{$LoanApplicantRec->lacc_loanamt}}" onkeyup="loadprocessingcharge()"  ></b></td>								
						</tr>						
						<tr>
							<th>Name</th><td><b>{{$LoanApplicantRec->lacc_custname}}</b></td>	
							<th>Tenor</th><td><b><input type="text" name="lacc_tenor" id="lacc_tenor" value="{{$LoanApplicantRec->lacc_tenor}}"  onkeyup="loaninstallmentdetail()" > months</b></td>								
						</tr>
						<tr>
							<th>Branch</th><td><b>{{$LoanApplicantRec->br_name}}</b></td>	
							<th>Interest</th><td><b><input type="text" name="lacc_intrate" id="lacc_intrate" value="{{$LoanApplicantRec->lacc_intrate }}"  onkeyup="loaninstallmentdetail()"> % (per year)</b></td>
						</tr>
						<tr>
							<th>Contact No.</th><td><b><?php
							$resaddr = unserialize($LoanApplicantRec->lacc_resaddr);
							echo $resaddr[5];
							?></b></td>	
							<th>Processing fee(<span id="spanlpf_id">0</span>)</th><td>
							<input type="hidden" name="lpf_id" id="lpf_id" value="0">
							<input type="hidden" name="lpf_amttype" id="lpf_amttype" value="0">
							<input type="hidden" name="lpf_amt" id="lpf_amt" value="0">
							<input type="text" name="lacc_lpfprocessingfee" id="lacc_lpfprocessingfee" readonly  value="0" >
							</td>
						</tr>
						<tr>
							<th>Loan open date</th><td><b><input type="date" name="lacc_opendt" id="lacc_opendt" value="<?php echo date("Y-m-d"); ?>"  onchange="loaninstallmentdetail()" ></b></td>
							<th>Late payment charge (in %)</th><td><b><input type="text" name="dpmt_charge" id="dpmt_charge" value="{{ $LoanApplicantRec->dpmt_charge }}" ></b></td>
						</tr>	
					@endforeach	
				</table>
			</div>
		</div>
		<!-- END DATA EXPORT -->
		
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i>Loan Installment detail</h3>
			</div>
			<div class="widget-content">
				<table  class="table table-striped table-bordered" id="tabloan">
				<thead>
				<tr>
					<th>SL No.</th>
					<th>Installment date</th>
					<th>Payable amount</th>				
					<th>Interest percentage</th>
					<th>Interest amount</th>					
					<th>Total payable amount</th>
					<th>Cheque No.</th>
					<th>Note</th>							
				</tr>
				</thead>
				<tbody>
				</tbody>
				</table>
				<hr>
				<b>Loan Paid details: </b>
				<textarea name="lacc_remarks" id="lacc_remarks" class="form-control" placeholder="Enter Loan Paid details.."></textarea>
			</div>
		</div>
		<!-- END DATA EXPORT -->
				<div class="widget">
					<div class="widget-header"><center>
						<button type="submit" class="btn btn-default btnloanapplicant_submit">Submit</button>
						<img src="img/inserting.gif" class="loadingimg1" style="height: 25px;">
						<img src="img/inserting.gif" class="loadingimg2" style="height: 25px;">
						<img src="img/inserting.gif" class="loadingimg3" style="height: 25px;">
						</center>
					</div>
				</div>	
		</form>					
	</div>
</div>
<!-- /main -->
<script>
	$('.createMod').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.edit').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href')); 
	});
	
	function Delete(id)
	{
		k = 0;
		if(k == 0)
		{
			k++;
			$.ajax({
				type: "GET",
				url:"/deluser",
				dataType: "json",
				data: {
					'_token': $('input[name=_token]').val(),
					'id': id
				},
				success: function (sdata) {
					if(sdata.status == 1)
					{
						//printSuccessMsg(sdata.message);
						$('#sideMenu').load('viewuser');
					}
					else
					{
						//printErrorMsg(sdata.message)
					}
				}
			});
		}		
	}
	//###Selected Loan type details starts here
	function loadprocessingcharge()
	{
		var percamt = 0;
		lacc_loanamt=$('#lacc_loanamt').val();
		$.ajax({
			url:'loanprocessingcharge',
			type:'GET',
			data:'&lacc_loanamt='+lacc_loanamt,
			success:function(data)
			{
				
				 var Obj=JSON.parse(data);
				$('#lpf_id').val(Obj[0].lpf_id);
				$('#lpf_amttype').val(Obj[0].lpf_amttype);
				$('#lpf_amt').val(Obj[0].lpf_amt);
				if(Obj[0].lpf_amttype =="Percentage")						
				{
					$('#spanlpf_id').html(Obj[0].lpf_amt+"%");
					percamt = lacc_loanamt * Obj[0].lpf_amt /100;
					$('#lacc_lpfprocessingfee').val(percamt);
				}
				else if(Obj[0].lpf_amttype =="Amount")
				{
					$('#spanlpf_id').html("Rs. "+Obj[0].lpf_amt);							
					$('#lacc_lpfprocessingfee').val(Obj[0].lpf_amt);
				}
				loaninstallmentdetail();
			}
		});
	}
	//###Selected Loan type details ends here
	//Loan installment starts here
	function loaninstallmentdetail()
	{
		var lacc_tenor=$('#lacc_tenor').val();
		var lacc_opendt=$('#lacc_opendt').val();
		var lacc_loanamt=$('#lacc_loanamt').val();
		var lacc_intrate=$('#lacc_intrate').val();
		$.ajax({
			url:'loaninstallmentdetail',
			type:'GET',
			data:'&lacc_tenor='+lacc_tenor+'&lacc_opendt='+lacc_opendt+'&lacc_loanamt='+lacc_loanamt+'&lacc_intrate='+lacc_intrate,
			success:function(data)
			{				
				$('#tabloan tbody').html('');
				$('#tabloan tbody').append(data);
			}
		});
	}
	//Loan installment ends here
	$(document).ready(function(){
		$(".loadingimg1").hide();
		$(".loadingimg2").hide();
		$(".loadingimg3").hide();
		loadprocessingcharge()
		loaninstallmentdetail()
	});
	
	//### Billing submission entry code starts here
	flag=0;
	$('.btnloanapplicant_submit').click(function(e)
	{
		e.preventDefault();
		$(".btnloanapplicant_submit").hide();
		$(".loadingimg1").show();
		$(".loadingimg2").show();
		$(".loadingimg3").show();
		_token = $("input[name='_token']").val();
		var lacc_id=$("input[name='lacc_id']").val();
		var acno=$("input[name='lacc_no']").val();
		var lacc_loanamt=$("input[name='lacc_loanamt']").val();
		var lacc_tenor=$("input[name='lacc_tenor']").val();
		var lacc_intrate=$("input[name='lacc_intrate']").val();
		var lacc_opendt=$("input[name='lacc_opendt']").val();
		var lpf_id=$("input[name='lpf_id']").val();
		var lpf_amttype=$("input[name='lpf_amttype']").val();
		var lpf_amt=$("input[name='lpf_amt']").val();
		var dpmt_charge=$("input[name='dpmt_charge']").val();
		var lacc_lpfprocessingfee=$("input[name='lacc_lpfprocessingfee']").val();
		var lacc_remarks=$('#lacc_remarks').val();
		//array starts
		lins_no = [];
		lins_date = [];
		lins_amt = [];
		lins_iperc = [];
		lins_iamt = [];
		lins_chqno = [];
		lins_note = [];
		
		arrlins_no = [];
		arrlins_date = [];
		arrlins_amt = [];
		arrlins_iperc = [];
		arrlins_iamt = [];
		arrlins_chqno = [];
		arrlins_note = [];
			
		var lins_no= document.getElementsByName("lins_no[]");
		var lins_date= document.getElementsByName("lins_date[]");
		var lins_amt= document.getElementsByName("lins_amt[]");
		var lins_iperc= document.getElementsByName("lins_iperc[]");
		var lins_iamt= document.getElementsByName("lins_iamt[]");
		var lins_chqno= document.getElementsByName("lins_chqno[]");
		var lins_note= document.getElementsByName("lins_note[]");
		for (index = 0; index < lins_no.length; ++index) 
		{
			arrlins_no[index] = lins_no[index].value;
			arrlins_date[index] = lins_date[index].value;
			arrlins_amt[index] = lins_amt[index].value;
			arrlins_iperc[index] = lins_iperc[index].value;
			arrlins_iamt[index] = lins_iamt[index].value;
			arrlins_chqno[index] = lins_chqno[index].value;
			arrlins_note[index] = lins_note[index].value;
		}
		//array ends
				
		$.ajax({
			url: "createloanaccount",
			type:'POST',
			dataType: 'json',
			//cache : false,
			//processData: false,
			data: {_token:_token,lacc_id:lacc_id,lacc_loanamt:lacc_loanamt,lacc_tenor:lacc_tenor,lacc_intrate:lacc_intrate,lacc_opendt:lacc_opendt,lpf_id:lpf_id,lpf_amttype:lpf_amttype,lpf_amt:lpf_amt,lacc_lpfprocessingfee:lacc_lpfprocessingfee,arrlins_no:arrlins_no,arrlins_date:arrlins_date,arrlins_amt:arrlins_amt,arrlins_iperc:arrlins_iperc,arrlins_iamt:arrlins_iamt,arrlins_chqno:arrlins_chqno,arrlins_note:arrlins_note,dpmt_charge:dpmt_charge,lacc_remarks:lacc_remarks},
			success: function(data)
			{
				if(data.status == "Success")
				{
					alert("Loan account created successfully..");					
					$.ajax({
						url: 'welcomekit',
						type: 'GET',
						data:'&lacc_id='+lacc_id+'&acno='+acno,
						success: function(data)
						{
							$('#sideMenu').html(data);
						}
					});
					 //$('#sideMenu').load('viewloanaccount');
					 //BillPrint(data.bil_id);
					 //BillPrint(Billno)
				}
				else
				{
					alert("Failed to Generate Purchase Bill..");
					//printErrorMsg(data.message)
				}
			}
		});
	});
	//### Billing submission entry code ends here
</script>