<?php $__env->startSection('content'); ?>

<div class="page">
    <div class="top-content">
        <div class="row">
            <div class="col-sm-12 col-xs-12">
                <div class="title-add">
                    <div class="pull-left">
                        <h3>View Schedule</h3>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="" >
                <div class="">
                    <div class="col-md-12 ">
                        <button type="button" class="btn btn-info btn-sm fa fa-share pull-right" onclick="back()"> Back</button><br>
                        <a href="<?php echo e(route('print-preview-schedule',$id)); ?>" class="btn btn-primary btn-sm fa fa-print pull-right" style="margin-top: -19px;"> Print</a>
                        <div class="form-group">
                            <div class="col-md-8">
                                <input type="hidden" name="loan_amount" readonly="" class="form-control required znumber" id="loan_amount_main" value="" placeholder="" required="" aria-required="true">
                                <input type="hidden" name="loan_type" readonly="" class="form-control required znumber" id="loan_type" value="" placeholder="" required="" aria-required="true">
                                <input type="hidden" name="loan_duration" readonly="" class="form-control required znumber" id="loan_duration" value="<?php echo e($installment->loan_duration); ?>" placeholder="" required="" aria-required="true">
                                <input type="hidden" name="loan_interest" readonly="" class="form-control required znumber" id="loan_interest" value="<?php echo e($installment->loan_interest); ?>" placeholder="" required="" aria-required="true">
                            </div>
                        </div>
                    </div>
                    <table class="table table-bordered schedul" border="1" cellpadding="0" cellspacing="0" width="100%">
                        <thead>
                            <tr>
                                <th>លរ</th>
                                <th>ថ្ងៃបង់ប្រាក់</th>
                                <th>ប្រាក់ដើមត្រូវបង់</th>
                                <th>ការប្រាក់ត្រូវបង់</th>
                                <th>សរុបប្រាក់ត្រូវបង់</th>
                                <th>សមតុល្យប្រាក់ចុងគ្រា</th>
                            </tr>
                        </thead>

                        <tbody>
                        <?php
                            $total_step_amount  = $installment->loan_amountos;
                            $loanInstallment    = $installment->loan_amountos;
                            $principal_remain   = $installment->loan_amountos;
                            $index              = 0;
                            $interest           = empty($installment->loan_interest)?0:$installment->loan_interest;
                            $datepayment        = $installment->loan_firstpaydate;
                        ?>
                        
                        <?php if(count($installment_step)>0): ?>
                            <?php $__currentLoopData = $installment_step; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $key => $step): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <?php
                                    $total_step_amount  -= $step->itempro_payamount;
                                    $loanInstallment    -= $step->itempro_payamount;
                                    $principal_remain   -= $step->itempro_payamount;
                                    $index++;
                                ?>
                                <tr>
                                    <td width="8%">
                                       <?php echo e($key+1); ?>

                                    </td>
                                    <td>
                                        <?php echo e(date('d-m-Y',strtotime($step->itempro_date))); ?>

                                    </td>
                                    <td>
                                        $<?php echo e(number_format($step->itempro_payamount,2)); ?>

                                    </td>
                                    <td>
                                        $0.00
                                    </td>
                                    <td>
                                        $<?php echo e(number_format($step->itempro_payamount,2)); ?>

                                    </td>
                                    <td>
                                        $<?php echo e(number_format($principal_remain,2)); ?>

                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        <?php endif; ?>

                        <?php if($installment->loan_duration && $installment->loan_duration>0): ?>
                            <?php  $loanInstallment=$installment->loan_amount_payment;  ?>
                            <?php if($installment->loan_type==2 || ($installment->loan_type==1 && $installment->loan_interest<=0)): ?>
                                <?php
                                    $principal   = $loanInstallment / $installment->loan_duration;
                                    $remainingBalance = $loanInstallment;
                                ?>
                                <?php for($i=1;$i<=$installment->loan_duration;$i++): ?>
                                    <?php
                                        $AnnualInterest = $remainingBalance*$interest/100;
                                        $payment = $principal+$AnnualInterest;
                                        $remainingBalance -= $principal;
                                        $principal_remain -= $principal;
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                           $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php else: ?>
                                <?php 
                                    $rate               = $interest/100;
                                    $loan_duration      = $installment->loan_duration;
                                    $payment            = ($loanInstallment * $rate) / (1 - (pow((1 + $rate), -$loan_duration)));
                                    $remainingBalance   = $loanInstallment;
                                ?>
                                <?php for($i=1;$i<=$installment->loan_duration;$i++): ?>
                                    <?php 
                                        $start              = $i + 1;
                                        $AnnualInterest     = $remainingBalance * $rate;
                                        $principal          = $payment-$AnnualInterest;
                                        $remainingBalance  -= $principal;
                                        $principal_remain  -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;

                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if(!empty($installment_change_rate[0])): ?>
                            <?php  $loanInstallment=$installment_change_rate[0]->amount_generate_loan; ?>
                            <?php if($installment_change_rate[0]->loan_type==2 || ($installment_change_rate[0]->loan_type==1 && $installment_change_rate[0]->first_interest<=0 && count(installment_change_rate)<=0)): ?>
                                <?php
                                    $principal   = $loanInstallment / $installment_change_rate[0]->duration;
                                    $remainingBalance = $loanInstallment;
                                    $datepayment = $installment_change_rate[0]->first_payment_date;
                                ?>
                                <?php for($i=1;$i<=$installment_change_rate[0]->duration;$i++): ?>
                                    <?php

                                        $AnnualInterest = $remainingBalance*$installment_change_rate[0]->first_interest/100;
                                        foreach ($installment_change_rate as $key => $value) {
                                           if($value->month_from<=$i&&$value->month_to>=$i){
                                                $AnnualInterest = $remainingBalance*$value->interest_for_change/100;
                                           }
                                        }

                                        $payment = $principal+$AnnualInterest;
                                        $remainingBalance -= $principal;
                                        $principal_remain -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;
                                        
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                            <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                           $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php else: ?>
                                <?php 
                                    $rate               = $installment_change_rate[0]->first_interest/100;
                                    $loan_duration      = $installment_change_rate[0]->duration;
                                    $payment            = ($loanInstallment * $rate) / (1 - (pow((1 + $rate), -$loan_duration)));
                                    $remainingBalance   = $loanInstallment;
                                    $index_count_month = 0;
                                    $check_change_rate = $rate;
                                    $rate_cp = $rate;
                                    $datepayment = $installment_change_rate[0]->first_payment_date;
                                ?>
                                <?php for($i=1;$i<=$installment_change_rate[0]->duration;$i++): ?>
                                    <?php 
                                        $AnnualInterest    = $remainingBalance * $rate;
                                        $check_change_rate = $rate;
                                        foreach ($installment_change_rate as $key => $value) {
                                           if($value->month_from<=$i&&$value->month_to>=$i){
                                                $check_change_rate =  $value->interest_for_change/100;
                                           }
                                        }
                                        if($rate_cp!=$check_change_rate){
                                            $rate_cp = $check_change_rate;
                                            $payment = ($remainingBalance * $check_change_rate) / (1 - (pow((1 + $check_change_rate), -($loan_duration-$index_count_month))));
                                            
                                        }
                                        $AnnualInterest     = $remainingBalance * $check_change_rate;
                                        $start              = $i + 1;
                                        $principal          = $payment-$AnnualInterest;
                                        $remainingBalance   -= $principal;
                                        $principal_remain   -= $principal;

                                        if($principal_remain<1) $principal_remain = 0;
                                        if($remainingBalance<1){
                                            $remainingBalance = 0;
                                        }
                                        $index_count_month++;
                                    ?>
                                    <tr>
                                        <td width="8%">
                                            <?php echo e(++$index); ?>

                                        </td>
                                        <td>
                                              <?php echo e(date('d-m-Y',strtotime($datepayment))); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal,2)); ?> 
                                        </td>
                                        <td>
                                            $<?php echo e(number_format($AnnualInterest,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($payment,2)); ?>

                                        </td>
                                        <td>
                                            $<?php echo e(number_format($principal_remain,2)); ?>

                                        </td>
                                    </tr>
                                    <?php 
                                        $datepayment = \Globals::todayNextmonth($datepayment);
                                    ?>
                                <?php endfor; ?>
                            <?php endif; ?>
                        <?php endif; ?>
                        <?php if(isset($installment_loan_bank[0])&&$installment_loan_bank[0]->amount>0): ?>
                             <tr>
                                <td width="8%">
                                    <?php echo e(++$index); ?>

                                </td>
                                <td>
                                    <?php echo e(date('d-m-Y',strtotime($installment_loan_bank[0]->date_payment))); ?>

                                </td>
                                <td>
                                    $<?php echo e(number_format($installment_loan_bank[0]->amount,2)); ?> 
                                </td>
                                <td>
                                    $0
                                </td>
                                <td>
                                     $<?php echo e(number_format($installment_loan_bank[0]->amount,2)); ?>

                                </td>
                                <td>
                                    $0
                                </td>
                            </tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('js'); ?>
<link rel="stylesheet" href="<?php echo e(asset('assets/scripts/installment/components.min.css')); ?>">
<link rel="stylesheet" href="<?php echo e(asset('assets/scripts/installment/plugins.min.css')); ?>">


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/bootstrap-wizard/jquery.bootstrap.wizard.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/table-advanced.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/form-wizard.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/jquery-validation/js/jquery.validate.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/bootstrap-switch/js/bootstrap-switch.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/app.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/components-select2.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/installment/select2/js/select2.full.min.js')); ?>"></script>
<script type="text/javascript" src="<?php echo e(asset('assets/scripts/datepicker/bootstrap-datepicker.min.js')); ?>"></script>
<script>
    function back(){
        window.history.back();
    }
</script>

<?php $__env->stopSection(); ?>
<?php echo $__env->make('adminlte::page', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>