@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"><a href="{{ route('homepage.ansuranupah') }}">Ansuran Pembiayaan & Upah</a></li>
                      <li class="breadcrumb-item active" aria-current="page">Upah</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-">
                            <form action="{{ route('upah.laporancetak') }}" target="_blank" method="post">
                                @csrf
                                <div class="input-group mb-3">
                                    <div class="input-group-prepend ml-3">
                                        <span class="input-group-text" id="basic-addon1">Mula</span>
                                    </div>
                                    <input type="text" class="form-control col-4" placeholder="Pilih Tarikh" aria-label="Tarikh" id="datemula" name="datemula" aria-describedby="basic-addon1">
                                    
                                    <div class="input-group-prepend ml-2 ">
                                        <span class="input-group-text" id="basic-addon1">Hingga</span>
                                    </div>
                                    <input type="text" class="form-control col-4" placeholder="Pilih Tarikh" aria-label="Tarikh" id="datehingga" name="datehingga" 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>Tarikh</th>
                                        <th>No Rujukan</th>
                                        <th>Teller</th>
                                        <th>Pinjaman</th>
                                        <th>JBG</th>
                                        <th>Upah Dibayar</th>
                                    </tr>
                                </thead>
                                <tfoot class="thead-dark">
                                    <th colspan="4">TOTAL</th>
                                    <th id="pinjamantotal"></th>
                                    <th></th>
                                    <th id="bayarantotal"></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 advansurtable = $('#myTable').DataTable({
        "ordering": true
    });
    $(document).ready( function () {
        $('#datemula').datepicker({
            format: 'dd-mm-yyyy',
            maxDate: 0
        });
        $('#datehingga').datepicker({
            format: 'dd-mm-yyyy',
            maxDate: 0
        });

        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/AnsuranByRangeTarikh/'+kodcaw,
            data:{ 'mula' : today_date, 'akhir' : today_date },
            success: function(advansur){
                console.log(advansur);
                advansurantable(advansur);
            }
        });
    });

    $('#datemula').change(function(){

        //mula
        var input_date = this.value;
        var split_date = input_date.split('-');
        var mula_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

        var mula = mula_date;

        //current time
        var hinggainput = $('#datehingga').val();

        if(hinggainput != '')
        {
            var input_date = hinggainput;
            var split_date = input_date.split('-');
            var hingga_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

            var hingga = hingga_date;

        }else{
            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;

            var hingga = today_date;
        }

        $.ajax({
            method:'get',
            url: api_url + '/api/AnsuranByRangeTarikh/' + kodcaw,
            data:{ 'mula' : mula, 'akhir' : hingga },
            success: function(advansur){
                advansurantable(advansur);
            }
        });
    });
    
    $('#datehingga').change(function(){
        var input_date = this.value;
        var split_date = input_date.split('-');
        var hingga_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

        var hingga = hingga_date;

        //current time
        var mulainput = $('#datemula').val();

        if(mulainput != '')
        {
            var input_date = mulainput;
            var split_date = input_date.split('-');
            var mula_date = split_date[2] + '-' +split_date[1]+'-'+split_date[0];

            var mula = mula_date;

        }else{

            var dulu_date = '2017-01-01';

            var mula = dulu_date;
        }

        $.ajax({
            method:'get',
            url: api_url + '/api/AnsuranByRangeTarikh/' + kodcaw,
            data:{ 'mula' : mula, 'akhir' : hingga },
            success: function(advansur){
                advansurantable(advansur);
            }
        });
    });


    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits: 2
    });
    
    function advansurantable(data){
        var counter = 1;
        var total_bayaran = 0;
        var total_pinjaman = 0;
        advansurtable.clear().draw();
        $.each(data,function(index,advansur){
            total_bayaran += parseFloat(advansur['upahDibayar']);
            total_pinjaman += parseFloat(advansur['pinjaman']);

            advansurtable.row.add([
                counter,
                advansur['tarikh'],
                advansur['norujukan'],
                advansur['teller'],
                formatter.format(advansur['pinjaman']),
                advansur['jbg'],
                formatter.format(advansur['upahDibayar'])
            ]).draw();
            counter = counter + 1;
            $('#bayarantotal').text(formatter.format(total_bayaran.toFixed(2)));
            $('#pinjamantotal').text(formatter.format(total_pinjaman.toFixed(2)));
        });
        
    }
</script>
@endsection

