<style>
.paymentmodal {
    width: 75%;
    margin: 30px auto;
}
</style>
<script src="assets/js/king-table.js"></script>
<?php
$LoanTotalPaid=0;
?>
<!-- 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>
			<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;?>
					<?php $__currentLoopData = $data['LoanApplicantRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanApplicantRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
				<input type="hidden" name="lacc_id" id="lacc_id" value="<?php echo e($LoanApplicantRec->lacc_id); ?>">
						<tr>
							<th>Application No.</th><td><b><?php echo e($LoanApplicantRec->lacc_no); ?></b></td>	
							<th>Loan amount</th><td><b> <?php echo e($LoanApplicantRec->lacc_loanamt); ?></b></td>								
						</tr>						
						<tr>
							<th>Name</th><td><b><?php echo e($LoanApplicantRec->lacc_custname); ?></b></td>	
							<th>Tenor</th><td><b><?php echo e($LoanApplicantRec->lacc_tenor); ?> months</b></td>								
						</tr>
						<tr>
							<th>Branch</th><td><b><?php echo e($LoanApplicantRec->br_name); ?></b></td>	
							<th>Interest</th><td><b><?php echo e($LoanApplicantRec->lacc_intrate); ?>% (per year)</b></td>
						</tr>
						<tr>
							<th>Loan open date</th><td><b><?php echo date("Y-m-d"); ?></b></td>	
							<th>Paid Amount</th>
							<td><font color="green"><b> <?php echo $LoanTotalPaid = $data['LoanTotalPaid']; ?></b></font></td>
						</tr>		
					<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>	
				</table>
			</div>
		</div>
		<!-- END DATA EXPORT -->
		
		<!-- DATA EXPORT -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> Preclosure settings </h3>
			</div>
			<div class="widget-content">
				<table  class="table table-striped table-bordered" id="tabloan">
				<thead>
					<tr>
						<th>SLNo.</th>
						<th>Date</th>					
						<th>Principal</th>							
						<th>Interest(%)</th>
						<th>Interest</th>						
						<th>Others</th>					
						<th>EMI</th>							
						<th>Paid</th>
						<th>Balance</th>
						<th>[o]</th>
					</tr>
				</thead>
				<tbody>	
				<?php					
					$totinterestamt =0;
					$tototheramt = 0;
					$totemiamt = 0;
					$totpaidamt = 0;
					$totbalamt = 0;
					$btn=0;
					$paid_amt = 0;
					$bal_amt=0;
					$totpaiddecreaseonloop = $LoanTotalPaid;
				?>
						<?php $__currentLoopData = $data['LoanInstallmentRecords']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $LoanInstallmentRec): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
						<?php
						$lins_amt=$LoanInstallmentRec->lins_amt;						
						$lins_iamt=$LoanInstallmentRec->lins_iamt;						
						$othr_amt=$LoanInstallmentRec->penaltysum;
						$totamt = $lins_amt+$lins_iamt+$othr_amt;
						
						//Sum Total
						$totinterestamt = $totinterestamt +  $lins_iamt;
						$tototheramt = $tototheramt + $othr_amt;
						$totemiamt = $totemiamt+ ($lins_amt+$lins_iamt+$othr_amt);
						
						
						if($totpaiddecreaseonloop > $totamt)
						{
							$paid_amt=$totamt;
							$totpaiddecreaseonloop = $totpaiddecreaseonloop - $totamt;
						}
						else
						{
							$paid_amt = $totpaiddecreaseonloop;
							$totpaiddecreaseonloop = 0;
						}
												
						$bal_amt=$totamt-$paid_amt;
						
						$totpaidamt = $totpaidamt + $paid_amt;
						$totbalamt = $totbalamt + $bal_amt;	
						
						?>
						<tr  
						<?php
						if($bal_amt > 0)
						{
						echo 'style="color:red;"';
						}
						else
						{
						echo 'style="color:green;"';
						}
						?>						
						>
							<td><?php echo e($LoanInstallmentRec->lins_no); ?></td><!-- SL No. -->
							<td><?php
							if($bal_amt == 0)
							{
								echo $LoanInstallmentRec->lins_date;
							}
							else
							{
								echo "<input type='date' name='lins_date[]' id='lins_date[]' value='$LoanInstallmentRec->lins_date'>";
							}
							?></td><!-- Installment dt -->
							<td> <?php echo e($LoanInstallmentRec->lins_amt); ?></td><!-- Principle Amt -->
							<td><?php
						if($bal_amt > 0)
						{
						echo '<input type="text" name="lins_iperc[]" value="'. $LoanInstallmentRec->lins_iperc . '" size=5>';
						}
						else
						{
						echo $LoanInstallmentRec->lins_iperc;
						}
						?>%</td>    <!-- Interest -->
							<td> <?php echo e($LoanInstallmentRec->lins_iamt); ?></td>  <!-- Interest Amt -->
							<td>
							<?php
								echo " " . $LoanInstallmentRec->penaltysum;
							?>
							</td><!-- Others Amt -->
							<td> <?php echo $totamt; ?></td><!-- EMI Amt -->
							<td> <?php echo $paid_amt; ?></td><!-- Paid Amt -->
							<td><b> <?php echo $bal_amt; ?></b></td><!-- Balance Amt -->
							<td> 
							<?php
								if($bal_amt == 0)
								{
									echo "<input type='checkbox'  class='custom-control-input' checked disabled>";
								}
								else
								{
									echo "<input type='checkbox'  class='custom-control-input' checked >";
								}
							?></td><!-- Balance Amt -->
						</tr>
						<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
				</tbody>
				<tfoot>
						<th></th>
						<th></th>					
						<th> <?php echo e($LoanApplicantRec->lacc_loanamt); ?></th>							
						<th></th>
						<th> <?php echo $totinterestamt; ?></th>						
						<th> <?php echo $tototheramt; ?></th>					
						<th> <?php echo $totemiamt; ?></th>							
						<th> <?php echo $LoanTotalPaid; ?></th>
						<th> <?php echo $totbalamt; ?></th>
						<th>-</th>
				</tfoot>
				</table>
			</div>
			<div class="widget">
				<div class="widget-header"><center>
					<button type="submit" class="btn btn-default btnloanapplicant_submit">Confirm and Close</button>
					</center>
				</div>
			</div>			
		</div>
		<!-- END DATA EXPORT -->		
		</form>					
		
		
		<!-- Starts Preclosure Payment -->
		<div class="widget widget-table">
			<div class="widget-header">
				<h3><i class="fa fa-table"></i> Preclosure Payment</h3>
			</div>
			<div class="widget-content">
			<form id="frmLoanApplication" name="frmLoanApplication" data-parsley-validate novalidate method="POST">
			
				<table  class="table table-striped table-bordered">
						<tr>
							<th>Transaction No.</th><td><input type="text" name="ltr_billno" id="ltr_billno" class="form-control" ><small class="text-danger" id="err_ltr_billno"></small></td>	
							<th>Transaction date</th><td><input type="date" name="ltr_transdt" id="ltr_transdt" class="form-control" value="<?php echo  date("Y-m-d"); ?>" ><small class="text-danger" id="err_ltr_transdt"></small></td>
						</tr>						
						<tr>
							<th>Total Amount</th><td><input type="text" readonly  name="totalpayable" id="totalpayable" class="form-control" value="<?php echo $totbalamt; ?>"></td>	
							<th>Payment type</th><td><select name="ltr_paymenttype" id="ltr_paymenttype" class="form-control" required> 
								<option value=""> -- Select -- </option>
								<?php
								$arr = array("Cash", "Cheque", "NEFT", "Others");
								foreach($arr as $val)
								{
									echo "<option value='$val'>$val</option>";
								}
								?>
							</select><small class="text-danger" id="err_ltr_paymenttype"></small></td>							
						</tr>
						<tr>
							<th>Paid Amount</th><td><input type="text" name="ltr_amt" id="ltr_amt" class="form-control" onkeyup="calculatebal()" ><small class="text-danger" id="err_ltr_amt"></small></td>
							<th>Cheque No.</th><td><input type="text" name="ltr_chqno" id="ltr_chqno" class="form-control" ><small class="text-danger" id="err_ltr_chqno"></small>
							</td>		
						</tr>
						<tr>
							<th>Balance Amount</th><td><input type="text" name="balanceamt" id="balanceamt" class="form-control" readonly>					
							<small class="text-danger" id="err_balanceamt"></small></td>	
							<th>Bank Name</th><td><input type="text" name="ltr_bank" id="ltr_bank" class="form-control" ><small class="text-danger" id="err_ltr_bank"></small></td>
						</tr>
						<tr>
							<th>Note:</th>
							<td colspan="3"><input type="text" name="ltr_note" id="ltr_note" class="form-control" >			
							<small class="text-danger" id="err_ltr_note"></small>
							</td>
						</tr>
						<tr>
							<td colspan="4"><center><button type="button" class="btn btn-primary btnloanpayment">Make Payment</button><center></td>
						</tr>
				</table>
			</div>
		</div>
		<!-- END Preclosure Payment -->
		
	</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')); 
	});
	
	$('#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);
            }
        });
    })

	
	function calculatebal() 
	{
		balanceamt = parseFloat($('#totalpayable').val()) - parseFloat($('#ltr_amt').val());
		$('#balanceamt').val(balanceamt);
		if($('#balanceamt').val() == "NaN")
		{
			$('#balanceamt').val(0);
		}
	}
	
	//### 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);
			}
		});
	}
	
</script>