<style>
.paymentmodal {
    width: 75%;
    margin: 30px auto;
}
</style>
<script src="assets/js/king-table.js"></script>
<script src="assets/js/popper.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Charm' rel='stylesheet'>
<?php
$LoanTotalPaid=0;
foreach($data['LoanApplicantRecords'] as $LoanApplicantRec)
{
	$lacc_id = $LoanApplicantRec->lacc_id;
	$lacc_no = $LoanApplicantRec->lacc_no;
}
?>
<div class="printreport">
<!-- 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> View Loan Account detail</h3>

<div class='btn-toolbar pull-right'>
    <div class='btn-group'>
      <button type='button' class='btn btn-primary' id="idreload" onclick="reloadpage('<?php echo $lacc_id; ?>','<?php echo $lacc_no; ?>')" ><i class="fa fa-refresh" aria-hidden="true"></i></button>
    </div>
</div>
<script>
//#Reload Starts here
function reloadpage(lacc_id,lacc_no)
{
	document.getElementById("idreload").innerHTML = "<i class='fa fa-refresh fa-spin' aria-hidden='true'></i>";
	$.ajax({
		url: 'LoanAcPanel',
		type: 'get',
		data:'&lacc_id='+lacc_id+'&lacc_no='+lacc_no,
		success: function(data)
		{
			$('#sideMenu').html(data);
			document.getElementById("idreload").innerHTML = "<i class='fa fa-refresh' aria-hidden='true'></i>";
		}
	});
}
//#Reload ends here
</script>
			</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)
					<?php $lacc_id = $LoanApplicantRec->lacc_id; ?>
<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}}">
<input type="hidden" name="lacc_custname" id="lacc_custname" value="{{$LoanApplicantRec->lacc_custname}}">
						<tr>
							<th>Application No.</th><td><b><?php echo $loanacc = $LoanApplicantRec->lacc_no; ?></b></td>	
							<th>Loan amount</th><td><b> {{$LoanApplicantRec->lacc_loanamt}}</b></td>								
						</tr>						
						<tr>
							<th>Name</th><td><b>{{$LoanApplicantRec->lacc_custname}}</b></td>	
							<th>Tenor</th><td><b>{{$LoanApplicantRec->lacc_tenor}} months</b></td>							
						</tr>
						<tr>
							<th>Branch</th><td><b>{{$LoanApplicantRec->br_name}}</b></td>	
							<th>Interest</th><td><b>{{$LoanApplicantRec->lacc_intrate }}% (per year)</b></td>
						</tr>
						<tr><?php /* $LoanApplicantRec->br_name */ ?>
							<th>Loan Account date</th><td><b><?php echo  date("d-m-Y",strtotime($LoanApplicantRec->lacc_opendt)); ?></b></td>	
							<th>Delay Payment interest</th>
							<td><b><?php echo $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>SLNo.</th>
						<th>RECEIPT No.</th>					
						<th>Transaction detail</th>			
						<th>Loan(CR)</th>						
						<th>Payment(DR)</th>		
						<th>Balance</th>
					</tr>
				</thead>
				<tbody>	
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
				</tbody>
				<tfoot>
						<th></th>
						<th></th>
						<th></th>
						<th></th>
						<th></th>
						<th></th>
				</tfoot>
				</table>
			</div>
		</div>

		<!-- END DATA EXPORT -->

				<div class="widget">
					<div class="widget-header">
	<center>
		<button class="btn btn-info btn-default" onclick="loadcrdrreport('<?php echo $lacc_id; ?>')" ><span class="fa fa-print"></span> PRINT</button>
	</center>
					</div>
				</div>
		</form>					
	</div>
</div>
<!-- /main -->

</div>
<script>
	$('.createMod').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href'));
	});
	
	$('.edit').click(function(e)
	{
		e.preventDefault();
		$('#sideMenu').load($(this).attr('href')); 
	});
	
	$('#mypayment').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id1').val($("input[name='lacc_id']").val());
				$('#lins_id1').val(lins_id);
				var CurrentPayableAmt =Obj.CurrentPayableAmt;
				var PreviousPayableAmt =Obj.PreviousPayableAmt;
				var TotalPayableAmt =Obj.TotalPayableAmt;
				$('#currentpayable').val(CurrentPayableAmt);
				$('#previousbalance').val(PreviousPayableAmt);
				$('#totalpayable').val(TotalPayableAmt);
            }
        });
    })
	
	$('#myotheramt').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanpaymentcalculation',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id2').val($("input[name='lacc_id']").val());
				$('#lins_id2').val(lins_id);
            }
        });
    })
	
	
	$('#myDelayPayment').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lacc_id = $("input[name='lacc_id']").val();
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loandelaypaymentcalculation',
            data: 'lacc_id=' + lacc_id + '&lins_id=' + lins_id,
            success: function(data) {
				var Obj=JSON.parse(data);	
				$('#lacc_id3').val($("input[name='lacc_id']").val());
				$('#lins_id3').val(lins_id);
				$('#dpmtemiamt').val(Obj.CurrentPayableAmt);
				$('#dpmtdelayinterest').val(Obj.delaypmt_charge);
				$('#dpmtfdt').val(Obj.lins_date);
				$('#dpmtnodays').val(Obj.nodays);
				$('#dpmttotpenaltyamt').val(Obj.totaldelaysum);
            }
        });
    })
	
	//updatedelaypayment() lacc_id3 lins_id3 dpmtemiamt dpmtfdt dpmtnodays  dpmttdt dpmtdelayinterest
	function updatedelaypayment() 
	{
        var $modal = $(this),
            lacc_id = $("input[name='lacc_id3']").val();
            lins_id = $("input[name='lins_id3']").val();
            dpmtemiamt = $("input[name='dpmtemiamt']").val();
            dpmtfdt = $("input[name='dpmtfdt']").val();
            dpmtnodays = $("input[name='dpmtnodays']").val();
            dpmttdt = $("input[name='dpmttdt']").val();
            dpmtdelayinterest = $("input[name='dpmtdelayinterest']").val();
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loandelaypaymentrecalculation',
            data: 'lacc_id=' + lacc_id + '&lins_id=' + lins_id + '&dpmtemiamt=' + dpmtemiamt + '&dpmtfdt=' + dpmtfdt + '&dpmtnodays=' + dpmtnodays + '&dpmttdt=' + dpmttdt + '&dpmtdelayinterest=' + dpmtdelayinterest,
            success: function(data) {
				var Obj=JSON.parse(data);
				$('#dpmtnodays').val(Obj.nodays);
				$('#dpmttotpenaltyamt').val(Obj.totaldelaysum);
            }
        });
	}
	
	//### Payment submission entry code starts here
	flag=0;
	$('.btnloanpayment').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();		           
		var lacc_id1 = $("input[name='lacc_id1']").val();
		var lins_id1 = $("input[name='lins_id1']").val();
		var ltr_transdt = $("input[name='ltr_transdt']").val();
		var currentpayable = $("input[name='currentpayable']").val();
		var previousbalance = $("input[name='previousbalance']").val();
		var totalpayable = $("input[name='totalpayable']").val();
		var ltr_amt = $("input[name='ltr_amt']").val();
		var balanceamt = $("input[name='balanceamt']").val();
		var ltr_billno = $("input[name='ltr_billno']").val();
		var ltr_chqno = $("input[name='ltr_chqno']").val();
		var ltr_paymenttype = $('#ltr_paymenttype').val();
		var ltr_bank = $("input[name='ltr_bank']").val();
		var ltr_note = $("input[name='ltr_note']").val();
		if(ltr_billno == "" )
		{
			alert("Transaction No. Should not be empty...");
		}
		else if(ltr_transdt == "" )
		{
			alert("Transaction Date Should not be empty...");
		}
		else if(ltr_amt == "" )
		{
			alert("Paid Amount. Should not be empty...");
		}
		else if(parseFloat(ltr_amt) < parseFloat(previousbalance) )
		{
			alert("Paid amount should be greater than Previous balance...");
		}
		else if(ltr_paymenttype == "" )
		{
			alert("Payment type should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentpayment",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id1, lins_id:lins_id1, ltr_transdt:ltr_transdt, ltr_transtype:"Payment", ltr_billno:ltr_billno, ltr_amt:ltr_amt, ltr_paymenttype:ltr_paymenttype, ltr_chqno:ltr_chqno, ltr_bank:ltr_bank, ltr_note:ltr_note, ltr_status:"Active"},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan payment done successfully..");
						//$('#sideMenu').load('viewloanaccount');
						LoadLoanApplication(lacc_id1,lins_id1);
						$('#mypayment').modal('toggle');
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	//### Payment submission entry code ends here
	
	function LoadLoanApplication(b,acno)
	{
		$.ajax({
			url: 'LoanAcPanel',
			type: 'GET',
			data:'&lacc_id='+b+'&acno='+acno,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	
	//### Penalty submission entry code starts here
	$('.btnloanpenalty').click(function(e)
	{
		//_token2 lacc_id2 lins_id2 penaltydate penaltytype  penaltyamt penaltynote
		e.preventDefault();
		_token = $("input[name='_token2']").val();		           
		var lacc_id1 = $("input[name='lacc_id2']").val();
		var lins_id1 = $("input[name='lins_id2']").val();
		var ltr_transdt = $("input[name='penaltydate']").val();
		var penaltytype = $('#penaltytype').val();
		var currentpayable = 0;
		var previousbalance = 0;
		var totalpayable = 0;
		var ltr_amt = $("input[name='penaltyamt']").val();
		var balanceamt = 0;
		var ltr_billno = 0;
		var ltr_chqno = '';
		var ltr_paymenttype = '';
		var ltr_bank = '';
		var ltr_note = $("input[name='penaltynote']").val();
		if(penaltytype == "" )
		{
			alert("Type Should not be empty...");
		}
		else if(ltr_transdt == "" )
		{
			alert("Date Should not be empty...");
		}
		else if(ltr_amt == "" )
		{
			alert("Amount should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentpayment",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id1, lins_id:lins_id1, ltr_transdt:ltr_transdt, ltr_transtype:penaltytype, ltr_billno:ltr_billno, ltr_amt:ltr_amt, ltr_paymenttype:ltr_paymenttype, ltr_chqno:ltr_chqno, ltr_bank:ltr_bank, ltr_note:ltr_note, ltr_status:"Active"},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Other charges added successfully..");
						LoadLoanApplication(lacc_id1,lins_id1);
						$('#myotheramt').modal('toggle');
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
					}
				}
			});
		}
	});
	//### Penalty submission entry code ends here
	
	//Preclosure starts here
	function funLoanPreClosure(lacc_id,lacc_no)
	{
		$.ajax({
			url: 'loanAccountPreclosure',
			type: 'GET',
			data:'&lacc_id='+lacc_id+'&lacc_no='+lacc_no,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	//Preclosure ends here
	
	//View Report starts here
	$('#viewreport').on('show.bs.modal', function(e) {
        var $modal = $(this),
            lins_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanrptsingle',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				$('#rptsingle').html(data);
            }
        });
    })
	//View Report ends here
	
	//Load Loan Cancellation Starts here
	$('#modcancellation').on('show.bs.modal', function(e) {
        var $modal = $(this),
            ltr_id = e.relatedTarget.id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanCancellationLoad',
            data: 'ltr_id=' + ltr_id,
            success: function(data) {
				$('#rptCancellation').html(data);
            }
        });
    })
	//Load Loan Cancellation Ends here
	
	//loanCancellation insert
	flag=0;
	$('.btncancel').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();	           
		var ltr_id = $("input[name='ltr_id']").val();
		var lacc_id = $("input[name='lacc_id']").val();
		var lins_id = $("input[name='lins_id']").val();
		var ltr_cancellationtype = $("input[name='ltr_cancellationtype']").val();
		var ltr_cancellationreason = $('#ltr_cancellationreason').val();
		if(ltr_cancellationtype == "" )
		{
			alert("Cancellation type Should not be empty...");
		}
		else
		{
			$.ajax({
				url: "loaninstallmentcancellationins",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,ltr_id:ltr_id, lacc_id:lacc_id, lins_id:lins_id, ltr_cancellationtype:ltr_cancellationtype, ltr_cancellationreason:ltr_cancellationreason},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan Cancellation done successfully..");
						$('#modcancellation').modal('toggle');
						//############
						reloadcancelmodal(lins_id,lacc_id);
						//############
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	
//Coding to manually add penaltyamt starts here 
	$('.btndpmttotpenaltyadd').click(function(e)
	{
		e.preventDefault();
		_token = $("input[name='_token']").val();	  
		var lacc_id = $("input[name='lacc_id3']").val();
		var lins_id = $("input[name='lins_id3']").val();
		var dpmtemiamt = $("input[name='dpmtemiamt']").val();
		var dpmtfdt = $("input[name='dpmtfdt']").val();
		var dpmttdt = $("input[name='dpmttdt']").val();
		var dpmtnodays = $("input[name='dpmtnodays']").val();
		var dpmtdelayinterest = $("input[name='dpmtdelayinterest']").val();
		var dpmttotpenaltyamt = $("input[name='dpmttotpenaltyamt']").val();
		if(dpmttotpenaltyamt <= 0)
		{
			alert("Penalty amount not valid...");
		}
		else
		{
			$.ajax({
				url: "AddManualPenalty",
				type:'POST',
				dataType: 'json',
				//cache : false,
				//processData: false,
				data: {_token:_token,lacc_id:lacc_id, lins_id:lins_id, dpmtemiamt:dpmtemiamt, dpmtfdt:dpmtfdt, dpmttdt:dpmttdt, dpmtnodays:dpmtnodays, dpmtdelayinterest:dpmtdelayinterest, dpmttotpenaltyamt:dpmttotpenaltyamt},
				success: function(data)
				{
					if(data.status == "Success")
					{
						alert("Loan Cancellation done successfully..");
						$('#myDelayPayment').modal('toggle');
						LoadLoanApplication(lacc_id,lins_id);
					}
					else
					{
						alert("Failed to Generate Purchase Bill..");
						//printErrorMsg(data.message)
					}
				}
			});
		}
	});
	
	function reloadcancelmodal(lins_id,lacc_id)
	{
		var $modal = $(this),
            lins_id = lins_id;
        $.ajax({
            cache: false,
            type: 'GET',
            url: 'loanrptsingle',
            data: 'lins_id=' + lins_id,
            success: function(data) {
				$('#rptsingle').html(data);
            }
        });		
	}
	
	$('#viewreport').on('hidden.bs.modal', function () {	           
		var lacc_id = $("input[name='lacc_id']").val();
		var lacc_no = $("input[name='lacc_no']").val();		 
			LoadLoanApplication(lacc_id,lacc_no);
	})
	
	function funSingleTransactionRPT(lacc_id,acno)
	{
		$.ajax({
			url: 'RPTSingleTrans',
			type: 'GET',
			data:'&lacc_id='+lacc_id+'&acno='+acno,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	
	function loadreport(lins_id)
	{
		$.ajax({
			url: 'LoanEMIreport',
			type: 'GET',
			data:'&lins_id='+lins_id,
			success: function(data)
			{
				$('.printreport').html(data);
			}
		});
	}
	
	function loadsinglecustreport(lacc_id)
	{
		$.ajax({
			url: 'LoanSingleCustomerReport',
			type: 'GET',
			data:'&lacc_id='+lacc_id,
			success: function(data)
			{
				$('.printreport').html(data);
			}
		});
	}
	
	//Credit Debit Report starts here
	function loadcrdrreport(lacc_id)
	{
		$.ajax({
			url: 'rptCreditDebit',
			type: 'GET',
			data:'&lacc_id='+lacc_id,
			success: function(data)
			{
				$('#sideMenu').html(data);
			}
		});
	}
	//Credit Debit Report ends here
	
	//Make payment - Show Balance amount starts here
	function calculatebal()
	{
		document.getElementById("balanceamt").value= parseFloat(document.getElementById("totalpayable").value) - parseFloat(document.getElementById("ltr_amt").value);
	}
	//Make payment - Show Balance amount ends here
	
	//Payment Receipt starts here
	function funPaymentReceipt(billno,dt,amt,textamt,paytype,lins_no,towards)
	{
		document.getElementById("receiptbillno").innerHTML=billno;
		document.getElementById("receiptdate").innerHTML=dt;
		document.getElementById("receiptdate1").innerHTML=dt;
		document.getElementById("textsum").innerHTML=textamt;
		document.getElementById("receiptpaytype").innerHTML=paytype;
		document.getElementById("receiptname").innerHTML=document.getElementById("lacc_custname").value;
		document.getElementById("receiptlacc_no").innerHTML=document.getElementById("lacc_no").value;
		document.getElementById("receipttowards").innerHTML=towards;
		document.getElementById("receiptamt").innerHTML=amt;
		$('#modprintpaymentreceipt').modal({show:true});		 
	}
	//Payment Receipt ends here

	function printDiv(divName) 
	{
		 var printContents = document.getElementById(divName).innerHTML;
		 var originalContents = document.body.innerHTML;
		 document.body.innerHTML = printContents;
		 window.print();
		 document.body.innerHTML = originalContents;
	}
</script>
	
</script>