@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">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('gadaian.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="datetarikh" name="datetarikh" 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>Nama</th>
                                        <th>No K/P</th>
                                        <th>No Rujukan</th>
                                        <th>Pembiayaan (RM)</th>
                                        <th>Keuntungan (RM)</th>
                                        <th>Nilai Marhun (RM)</th>
                                        <th>Berat (g)</th>
                                    </tr>
                                </thead>
                                <tfoot class="thead-dark">
                                    <th colspan="4">TOTAL</th>
                                    <th id="pinjamantotal"></th>
                                    <th id="upah12total"></th>
                                    <th id="nmtotal"></th>
                                    <th id="berattotal"></th>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                    {{-- <div class="row">
                        <div class="col-md-12 text-right">
                            Bilangan Surat : <span id="bilangan_surat">3</span>
                            Jumlah Pinjaman : <span id="jumlah_pinjaman">RM 3,000.00</span>
                        </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 tablegadai = $('#myTable').DataTable({
        "ordering": false
    });
    $(document).ready( function () {
        $('#datetarikh').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/gadai/laporan/'+kodcaw+'/' + today_date,
            success: function(gadai){
                gadaitable(gadai);
            }
        });
    });

    $('#datetarikh').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/gadai/laporan/'+kodcaw+'/' + today_date,
            success: function(gadai){
                gadaitable(gadai);
            }
        });
    });

    const formatter = new Intl.NumberFormat('en-US', {
        minimumFractionDigits: 2
    });
    
    function gadaitable(data){
        var counter = 1;
        var total_pinjaman = 0;
        var total_upah12 = 0;
        var total_nm = 0;
        var total_berat = 0;

        tablegadai.clear().draw();
        $.each(data,function(index,gadai){
                    total_pinjaman += Number(gadai['pinjaman']);
                    total_upah12 += Number(gadai['upah12']);
                    total_nm += Number(gadai['nilaimarhun']);
                    total_berat += Number(gadai['berat']);
                    tablegadai.row.add([
                        counter,
                        gadai['nama'],
                        gadai['nokp'],
                        gadai['norujukan'],
                        formatter.format(gadai['pinjaman']),
                        formatter.format(gadai['upah12']),
                        formatter.format(gadai['nilaimarhun']),
                        formatter.format(gadai['berat'])
                    ]).draw();
                    $('#pinjamantotal').text(formatter.format(total_pinjaman.toFixed(2)));
                    $('#upah12total').text(formatter.format(total_upah12.toFixed(2)));
                    $('#nmtotal').text(formatter.format(total_nm.toFixed(2)));
                    $('#berattotal').text(formatter.format(total_berat.toFixed(2)));
                    counter = counter + 1;
        });
        
    }
</script>
@endsection
