@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb pb-3">
                      <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
                      <li class="breadcrumb-item"><a href="{{ route('laporan') }}">Laporan</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Ansuran Pembiayaan</li>
                    </ol>
                  </nav>
                  
                <div class="card-body text-center">
                    <div class="row form-group">
                        <div class="col-md-2">
                        </div>
                        <div class="col-md-10">
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-5">
                            <form action="{{ route('ansuran.laporancetak') }}" target="_blank" method="post">
                                @csrf
                                <div class="input-group mb-3">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text" id="basic-addon1">Tarikh</span>
                                    </div>
                                    <input type="text" class="form-control" placeholder="Pilih Tarikh" aria-label="Tarikh" id="datetimepicker1" name="datetimepicker1" aria-describedby="basic-addon1">
                                    <input type="submit" class="btn btn-primary ml-md-5" id="submit_button" name="submit_button" value="Cetak">
                                </div>
                            </form>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <table class="table table-striped table-hover" id="myTable">
                                <thead class="thead-dark">
                                    <tr>
                                        <th>Bil</th>
                                        <th>No Rujukan</th>
                                        <th>Nama</th>
                                        <th>No K/P</th>
                                        <th>Tempoh</th>
                                        <th width="40%">Baki Pembiayaan (RM)</th>
                                        <th>Bayaran (RM)</th>
                                        <th width="60%">Lain-lain Caj (RM)</th> 
                                        <th>Keuntungan 1% (RM)</th>
                                        <th>Keuntungan (RM)</th>
                                    </tr>
                                </thead>
                                <tfoot class="thead-dark">
                                    <th colspan="5">TOTAL</th>
                                    <th id="pinjamantotal"></th>
                                    <th id="bayarantotal"></th>
                                    <th id="ansuranujrahtotal"></th>
                                    <th id="ansuranonepercenttotal"></th>
                                    <th id="keuntungantotal"></th>
                                </tfoot>
                            </table>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var api_url = "<?php echo $api_url ?>";
    var kodcaw = "<?php echo $cawangan_info['kodcaw'] ?>";

    var tabletebus = $('#myTable').DataTable({
        "ordering": false
    });
    $(document).ready( function () {
        $('#datetimepicker1').datepicker({
            format: 'dd-mm-yyyy'
        });
        var current = new Date();
        var current_date = current.getDate();
        var current_month = current.getMonth()+1;
        var current_year = current.getFullYear();
        var today_date = current_year + '-' +current_month+'-'+current_date;
        $.ajax({
            method:'get',
            url: api_url + '/api/ansuran/laporan/'+kodcaw,
            data:{ 'tarikh' : today_date },
            success: function(tebus){
                tebustable(tebus);
            }
        });
    });

    $('#datetimepicker1').change(function(){
        var input_date = this.value;
        var split_date = input_date.split('-');
        var today_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];
        $.ajax({
            method:'get',
            url: api_url + '/api/ansuran/laporan/'+kodcaw,
            data:{ 'tarikh' : today_date },
            success: function(tebus){
                tebustable(tebus);
            }
        });
    });
    
    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits: 2
    });
    
    function tebustable(data){
        var counter = 1;
        var total_pinjaman = 0;
        var total_bayaran = 0;
        var total_keuntungan = 0;
        var total_ansuranujrah = 0;
        var total_ansuranonepercent = 0;
        tabletebus.clear().draw();
        $.each(data,function(index,tebus){
            $.ajax({
                url:api_url + '/api/gadai/norujukan/' + tebus['norujukan'],
                method:'get',
                success:function(gadai){
                    $.ajax({
                        url:api_url + '/api/customers/' + gadai[0][0]['nokp'],
                        method:'get',
                        success: function(customer){
                            total_pinjaman += Number(gadai[0][0]['bakipjm']);
                            total_bayaran += Number(tebus['duit_masuk']);
                            total_keuntungan += Number(tebus['bayaran_keuntungan']);
                            total_ansuranujrah += Number(tebus['ansuranujrah']);
                            total_ansuranonepercent += Number(tebus['ansuranonepercent']);

                            tabletebus.row.add([
                                counter,
                                tebus['norujukan'],
                                customer['nama'],
                                gadai[0][0]['nokp'],
                                gadai[0][0]['tempoh'],
                                gadai[0][0]['bakipjm'],
                                tebus['duit_masuk'],
                                tebus['ansuranujrah'],
                                tebus['ansuranonepercent'],
                                tebus['bayaran_keuntungan']
                            ]).draw();
                            $('#pinjamantotal').text(formatter.format(total_pinjaman.toFixed(2)));
                            $('#bayarantotal').text(formatter.format(total_bayaran.toFixed(2)));
                            $('#ansuranujrahtotal').text(formatter.format(total_ansuranujrah.toFixed(2)));
                            $('#ansuranonepercenttotal').text(formatter.format(total_ansuranonepercent.toFixed(2)));
                            $('#keuntungantotal').text(formatter.format(total_keuntungan.toFixed(2)));
                            
                            counter = counter + 1;
                        }
                    });
                }
            });


        });
        
    }
</script>
@endsection

