<?php $__env->startSection("addCss"); ?>
<style media="screen">
    @media (min-width: 768px){
            .table-responsive {
            overflow-x: hidden;
        }
    }
   
</style>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
    <section class="content-header">
    <h1>
        <?php echo Trans('reports.menu'); ?>

        <small><?php echo trans("payments.payment_history"); ?></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active"><a href="#"><i class="fa fa-usd"></i> <?php echo Trans('reports.menu'); ?></a></li>
        <li class="active"><a href="#"><?php echo trans("payments.payment_history"); ?></a></li>
    </ol>
    </section>

  <!-- Main content -->
  <section class="content">
    <?php if(Session::has('message')): ?>
        <p class="alert alert-warning">
            <?php echo Trans(Session::get('message')); ?>

        </p>
    <?php endif; ?>
    <div class="box box-body">
        <div class="row">
            <?php echo Form::open(['method' => 'get']); ?>

            <div class="col-md-4">
                <div class="form-group">
                    <label for="start_date"><?php echo trans('invoices.start_date'); ?></label>
                    <div class="input-group date">
                        <?php
                        $value = Request::get('start_date') ? Request::get('start_date') : null;

                        ?>
                        <?php echo Form::text('start_date', $value, [
                        'class' => "form-control",
                        'id'    =>  "start_date",
                        'placeholder' => Trans('invoices.start_date'),
                        'autocomplete' => "off"
                        ]); ?>

                        <div class="input-group-addon">
                            <i class="fa fa-calendar"></i>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="end_date"><?php echo trans('invoices.end_date'); ?></label>
                    <div class="input-group date">
                        <?php
                        $value = Request::get('end_date') ? Request::get('end_date') : null;

                        ?>
                        <?php echo Form::text('end_date', $value, [
                        'class' => "form-control",
                        'id'    =>  "end_date",
                        'placeholder' => Trans('invoices.end_date'),
                        'autocomplete' => "off"
                        ]); ?>

                        <div class="input-group-addon">
                            <i class="fa fa-calendar"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label for=""><?php echo trans("reports.show"); ?></label>
                    <?php
                    $selected = Input::get('paginate') ? Input::get('paginate') : 10;
                    ?>
                    <?php echo Form::select("paginate", [
                        'all'   =>  "All",
                        10      =>  10,
                        15      =>  15,
                        20      =>  20,
                        25      =>  25,
                        30      =>  30,
                        50      =>  50,
                        100     => 100
                    ], $selected, [
                        'class' => 'form-control select2',
                      ]); ?>

                </div>
                <div class="form-group">
                    <label for=""><?php echo trans('users.users'); ?></label>
                    <?php
                        $selected = Input::get('user') ? Input::get('user') : 'all';
                    ?>
                    <?php echo Form::select('user', $users, $selected, [
                        'class' => 'form-control margin select2',
                    ]); ?>

                    </div>

            </div>
            <div class="col-md-4">
                <div class="form-group pull-right">
                <br>
                    <button type="submit" class="btn btn-info"><i class="fa fa-search"></i> <?php echo Trans('invoices.search'); ?></button>
                </div>
            </div>
        <?php echo Form::close(); ?>

        </div>
        <hr>
        <div class="box box-solid box-primary">
            <div class="box-header">
                <h3 class="box-title">
                    <i class="fa fa-bar-chart"> <?php echo trans("payments.grahp"); ?></i>
                </h3>
            </div>
            <div class="box-body">
                <div class="row">
                    <div class="col-md-6">
                        <div id="canvas-holder" style="width:100%">
                            <canvas id="chart-area" />
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div id="container" style="width: 100%;">
                            <canvas id="canvas-bar"></canvas>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <hr>
            <div class="col-md-12">
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th><?php echo trans("installments.id"); ?></th>
                            <th><?php echo trans("installments.code"); ?></th>
                            <th><?php echo trans("payments.payment_date"); ?></th>
                            <th><?php echo trans("payments.customer"); ?></th>
                            <th><?php echo trans("payments.total_payment"); ?></th>
                            <th><?php echo trans("payments.principal"); ?></th>
                            <th><?php echo trans("payments.interest"); ?></th>
                            <th><?php echo trans("payments.late"); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                    $i = 1;
                    $totalAmount = 0;
                    $totalLate = 0;
                    $totalPriciple = 0;
                    $totalInterest = 0;

                    ?>
                    <?php if(count($payments) > 0): ?>

                        <?php foreach($payments as $payment): ?>
                    <?php 
                        $installment = Installment::find($payment->installment_id);
                        $total = $payment->is_paid_all ? installment_paid_off($payment->installment_id) : $payment->amount;
                        $p = $payment->is_paid_all ? installment_paid_off($payment->installment_id, "principal") : $installment->getMonthlyPriciple();
                        $totalPriciple += $p;
                        $rate = $payment->is_paid_all ? installment_paid_off($payment->installment_id, "interest") : $total - $installment->getMonthlyPriciple();
                        $totalInterest += $rate;
                        
                        $totalAmount += $total;
                        $totalLate   += $payment->fee_late;

                    ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><a href="<?php echo $installment->loan_type == 'emi' ? route('emiloans.show', $installment->id) :route('installments.show', $installment->id); ?>"><?php echo convertInvoiceNumber($installment->id); ?> <?php if($installment->loan_type == "emi"): ?> <span class="label label-primary">(EMI)</span> <?php endif; ?></a></td>
                            <td><?php echo dt_format($payment->fee_date); ?></td>
                            <td><?php echo $payment->full_name_kh; ?> (<?php echo $payment->tel; ?>)</td>
                            <td><?php echo money($total); ?></td>
                            <td><?php echo money($p); ?></td>
                            <td><?php echo money($rate); ?></td>
                            <td><?php echo money($payment->fee_late); ?></td>
                        </tr>
                        <?php $i++ ?>
                        <?php endforeach; ?>
                        <tr>
                            <td colspan="4" class="text-right"><?php echo trans("payments.total"); ?></td>
                            <td><strong><?php echo money($totalAmount); ?></strong></td>
                            <td><strong><?php echo money($totalPriciple); ?></strong></td>
                            <td><strong><?php echo money($totalInterest); ?></strong></td>
                            <td><strong><?php echo money($totalLate); ?></strong></td>
                        </tr>
                    <?php else: ?> 
                        <td colspan="8">No Result...</td>
                    <?php endif; ?>
                    </tbody>
                </table>
                </div>
                <?php if(!empty(Input::get("paginate")) && Input::get("paginate") != "all"): ?>
                    <div class="row">
                        <div class="col-md-4">
                            <p style="margin-top: 10px;">Total Records : <?php echo $payments->getTotal(); ?> Page <?php echo $payments->getCurrentPage(); ?> of <?php echo $payments->getLastPage(); ?></p>
                        </div>
                        <div class="col-md-8 text-right">
                            <?php echo $payments->appends(Request::except('page'))->links(); ?>

                        </div>
                    </div>
                <?php else: ?>
                    <?php if(count($payments) > 0): ?>
                        Total Records : <?php echo count($payments); ?>

                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
      </div>
  </section>
<?php $__env->stopSection(); ?>
<?php $__env->startSection('addJs'); ?>
  <?php echo HTML::script('plugins/datepicker/bootstrap-datepicker.js'); ?>

  <?php echo HTML::script('plugins/datepicker/locales/bootstrap-datepicker.kh.js'); ?>

  <?php echo HTML::script("http://www.chartjs.org/dist/2.7.1/Chart.bundle.js"); ?>

  <?php echo HTML::script("http://www.chartjs.org/samples/latest/utils.js"); ?>

    <script>
        $("#start_date, #end_date").datepicker({
            format: 'dd/mm/yyyy',
            language: 'kh',
            autoclose: true,
            todayHighlight: true
        });
        var randomScalingFactor = function() {
            return Math.round(Math.random() * 100);
        };
        var config = {
            type: 'pie',
            data: {
                datasets: [{
                    data: [
                        <?= round($totalPriciple, 2) ?>,  <?= round($totalInterest, 2) ?>, <?= round($totalLate, 2) ?>
                    ],
                    backgroundColor: [
                        window.chartColors.yellow,
                        window.chartColors.green,
                        window.chartColors.red
                    ],
                    label: 'Dataset 1'
                }],
                labels: [
                    "<?php echo trans('reports.principal'); ?>",
                    "<?php echo trans('reports.interest'); ?>",
                    "<?php echo trans('reports.late'); ?>",
                ]
            },
            options: {
                responsive: true
            }
        };
        var color = Chart.helpers.color;
        var barChart = {
            labels: ["<?php echo trans('reports.payment'); ?>"],
            datasets: [{
                label: "<?php echo trans('reports.principal'); ?>",
                backgroundColor: color(window.chartColors.yellow).alpha(0.5).rgbString(),
                borderColor: window.chartColors.yellow,
                borderWidth: 1,
                data: [
                    <?= round($totalPriciple, 2) ?>
                ]
            },
                {
                    label: "<?php echo trans('reports.interest'); ?>",
                    backgroundColor: color(window.chartColors.green).alpha(0.5).rgbString(),
                    borderColor: window.chartColors.green,
                    borderWidth: 1,
                    data: [
                        <?= round($totalInterest, 2) ?>
                    ]
                },
                {
                    label: "<?php echo trans('reports.late'); ?>",
                    backgroundColor: color(window.chartColors.red).alpha(0.5).rgbString(),
                    borderColor: window.chartColors.red,
                    borderWidth: 1,
                    data: [
                        <?= round($totalLate, 2) ?>
                    ]
                }]
        };
        window.onload = function() {
            var ctx = document.getElementById("chart-area").getContext("2d");
            window.myPie = new Chart(ctx, config);
            var bar = document.getElementById("canvas-bar").getContext("2d");
            window.myBar = new Chart(bar, {
                type: 'bar',
                data: barChart,
                options: {
                    responsive: true,
                    legend: {
                        position: 'top',
                    },
                    title: {
                        display: false,
                        text: 'Chart.js Bar Chart'
                    }
                }
            });
        };
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.production', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>