@extends('app.app')
@section('title')
    Finance - Ledger Balance
@endsection

@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="row">
                <div class="col-12">
                    <label for="" class="form-label">Periode</label>
                </div>
                <div class="col-sm-12 col-md-6 col-lg-4">
                    <div class="mb-3 d-flex align-items-center">
                        <input class="form-control" type="date" id="filter-startDate">
                        <div class="mx-3">-</div>
                        <input class="form-control" type="date" id="filter-endDate">
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12 col-md-4 col-lg-3">
                    <div class="card">
                        <div class="card-body">
                            <h6 class="mb-3">Uang Masuk</h6>
                            <h4 class="text-end mb-0">Rp<span id="totAllIn">2.500.000</span></h4>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-4 col-lg-3">
                    <div class="card">
                        <div class="card-body">
                            <h6 class="mb-3">Uang Keluar</h6>
                            <h4 class="text-end mb-0">Rp<span id="totAllOut">2.500.000</span></h4>
                        </div>
                    </div>
                </div>
                <div class="col-sm-12 col-md-4 col-lg-3">
                    <div class="card">
                        <div class="card-body">
                            <h6 class="mb-3">Balance</h6>
                            <h4 class="text-end mb-0">Rp<span id="totAllBl">2.500.000</span></h4>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-12">
                    <div class="card">
                        <div class="card-header">
                            <div class="row d-flex align-items-center">
                                <div class="col">
                                    <p class="card-title text-bold mb-0">Ledger Balance</p>
                                    <p class="card-subtitle text-muted">Daftar Ledger Balance</p>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="tLedgerBl" class="table table-hover dataTable w-100">
                                    <thead>
                                        <tr class="">
                                            <th class="">#</th>
                                            <th class=""></th>
                                            <th class="text-nowrap">Kode Transaksi</th>
                                            <th class="w-25">item</th>
                                            <th class="text-center">Uang Masuk (Rp)</th>
                                            <th class="text-center">Uang Keluar (Rp)</th>
                                            <th class="text-center">Balance (Rp)</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="">
                                            <td class="">1</td>
                                            <td class="">
                                                <a href="{{ route('view_keuangan_payment_view') }}" class="">
                                                    <span class="icon ion-eye text-danger fz-16"></span>
                                                </a>
                                            </td>
                                            <td class="text-nowrap">#89901280921890</td>
                                            <td class="text-nowrap">
                                                <p class="mb-0">Pengantaran Logistik</p>
                                                <p class="mb-0">Jakarta - yogyakarta</p>
                                                <p class="mb-0">22 Apr 2022</p>
                                            </td>
                                            <td class="text-end">2.500.000</td>
                                            <td class="text-end text-success">
                                                <p class="mb-0">500 .000</p>
                                            </td>
                                            <td class="text-end text-danger">0</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('customcss')
@endsection

@section('customjs')
    <script>
        // $('.dataTable').DataTable({
        //     "columnDefs": [ {
        //         "targets": 1,
        //         "orderable": false
        //     } ]
        // });
    </script>
    <script>
        'use strict';
        const State = {
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
            storage_lara: "{{ asset('storage') }}/",
            periodeStartDate: '', // unix timestamps
            periodeEndDate: '', // unix timestamps
            stts_order: {
                wait: "{{ App\Models\Orders::STTS_WAIT }}",
                confirm: "{{ App\Models\Orders::STTS_CONFIRM }}",
                have_get_vhc: "{{ App\Models\Orders::STTS_HAVE_GET_VHC }}",
                pck: "{{ App\Models\Orders::STTS_PCK }}",
                go: "{{ App\Models\Orders::STTS_GO }}",
                arv: "{{ App\Models\Orders::STTS_ARV }}",
                drop: "{{ App\Models\Orders::STTS_DROP }}",
                client_pay: "{{ App\Models\Orders::STTS_CLIENT_PAY }}",
                vendor_payed: "{{ App\Models\Orders::STTS_VENDOR_PAYED }}",
                close: "{{ App\Models\Orders::STTS_CLOSE }}",
                cancel: "{{ App\Models\Orders::STTS_CANCEL }}",
            },
            flow_type: {
                payment: "{{ App\Models\OrdersAItems::FLOW_TYPE_PAYMENT }}",
                billing: "{{ App\Models\OrdersAItems::FLOW_TYPE_BILLING }}",
                hybrid: "{{ App\Models\OrdersAItems::FLOW_TYPE_HYBRID }}",
            },
            cash_type: {
                out: "{{ App\Models\OrdersAItems::TYPE_CASH_OUT }}",
                in: "{{ App\Models\OrdersAItems::TYPE_CASH_IN }}",
                hybrid: "{{ App\Models\OrdersAItems::TYPE_CASH_HYBRID }}",
            },
            invc_to_client: {
                not: "{{ App\Models\OrdersAItems::INVC_TO_CLIENT_NO }}",
                yes: "{{ App\Models\OrdersAItems::INVC_TO_CLIENT_YES }}",
            },
            only_client: {
                not: "{{ App\Models\OrdersAItems::ONLY_CLIENT_NO }}",
                yes: "{{ App\Models\OrdersAItems::ONLY_CLIENT_YES }}",
            },
            calc_to_vdr: {
                not: "{{ App\Models\OrdersAItems::CALC_TO_VDR_NO }}",
                yes: "{{ App\Models\OrdersAItems::CALC_TO_VDR_YES }}",
            },
            is_a_item_paid: {
                not: "{{ App\Models\OrdersAItems::IS_PAID_NO }}",
                paid: "{{ App\Models\OrdersAItems::IS_PAID_YES }}",
            },
            is_aprv: {
                not: "{{ App\Models\OrdersAItems::IS_APRV_NO }}",
                yes: "{{ App\Models\OrdersAItems::IS_APRV_YES }}",
            },
            url_view_detail: "{{ route('view_keuangan_payment_view') }}",
            delay_typing_front: 1000,
        };

        const Wrapper = {
            activate: function() {
                Filter.activate();
                Wrapper.event();
                DTable.activate();
            },
            event: function() {},
        };

        const Filter = {
            activate: function() {
                const startDate = moment().startOf('month');
                const endDate = moment().endOf('month');
                $('#filter-startDate').val(startDate.format('YYYY-MM-DD')).trigger('change');
                $('#filter-endDate').val(endDate.format('YYYY-MM-DD')).trigger('change');
                State.periodeStartDate = startDate.unix();
                State.periodeEndDate = endDate.unix();
                Filter.evPeriodeDate();
            },
            evPeriodeDate: function() {
                $('#filter-startDate').on('change', function(e) {
                    const {
                        isValid,
                        data
                    } = Filter.calcPeriodeDate(moment($('#filter-startDate').val()), moment($('#filter-endDate').val()))
                    if (!isValid) return false;
                    State.periodeStartDate = data.startDate.unix();
                    State.periodeEndDate = data.endDate.unix();
                    DTable.reload();
                });
                $('#filter-endDate').on('change', function(e) {
                    const {
                        isValid,
                        data
                    } = Filter.calcPeriodeDate(moment($('#filter-startDate').val()), moment($('#filter-endDate').val()))
                    if (!isValid) return false;
                    State.periodeStartDate = data.startDate.unix();
                    State.periodeEndDate = data.endDate.unix();
                    DTable.reload();
                });
            },
            calcPeriodeDate: function(startDate, endDate) {
                if (startDate.format('YYYY-MM-DD') === 'Invalid date') {
                    Helper.toast('Warning', 'just now', 'Tanggal mulai periode tidak valid');
                    return {
                        isValid: false
                    };
                }
                if (endDate.format('YYYY-MM-DD') === 'Invalid date') {
                    Helper.toast('Warning', 'just now', 'End date of the period is not valid');
                    return {
                        isValid: false
                    };
                }
                const duration = moment.duration(endDate.diff(startDate));
                const diffDays = duration.days();
                if (diffDays > 30) {
                    Helper.toast('Warning', 'just now', 'Maksimal periode 1 bulan');
                    return {
                        isValid: false
                    };
                }
                if (diffDays < 0) {
                    Helper.toast('Warning', 'just now', 'Rentang periode tidak valid');
                    return {
                        isValid: false
                    };
                }
                return {
                    isValid: true,
                    data: {
                        startDate,
                        endDate
                    }
                };
            },
        }

        const DTable = {
            table: null,
            activate: function() {
                DTable.reload();
                DTable.eventChildRow();
            },
            reload: function() {
                // $('#tLedgerBl').DataTable();
                // if (Driver.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { Driver.Table.firstInitDataTable = 1; }
                DTable.setSummary({
                    total_bl: 0,
                    total_in: 0,
                    total_out: 0,
                });
                DTable.table = $('#tLedgerBl').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    ajax: {
                        url: "{{ route('api_finance_list_ledger_balances') }}?start_date=" + State.periodeStartDate + "&end_date=" + State.periodeEndDate,
                        type: 'GET',
                        complete: function(jqXHR, textStatus, c) {
                            const {
                                total_bl,
                                total_in,
                                total_out,
                            } = jqXHR.responseJSON.summary;
                            DTable.setSummary({
                                total_bl,
                                total_in,
                                total_out,
                            });
                            // removeTableSkeletonLoading()
                        },
                    },
                    deferRender: true,
                    columns: [{
                            data: 'DT_RowIndex',
                            className: 'text-end',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        // {
                        //     data: 'action',
                        //     className: 'text-center',
                        //     visible: true,
                        //     orderable: true,
                        //     searchable: true,
                        //     render: function(data, type, row, meta) {
                        //         let action = `
                    //             <a href="${State.url_view_detail}?code=${row.ord_code}" class="">
                    //                 <span class="icon ion-eye text-danger fz-16"></span>
                    //             </a>
                    //             `;
                        //         return action;
                        //     }
                        // },
                        {
                            "className": 'dt-control',
                            "orderable": false,
                            "data": null,
                            "defaultContent": '',
                        },
                        {
                            data: 'ord_code',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            createdCell: function(td, cellData, rowData, row, col) {
                                $(td).attr('data-ord_id', rowData.ord_id);
                                $(td).attr('data-ord_code', rowData.ord_code);
                            },
                            render: function(data, type, row, meta) {
                                return `#${data}`;
                            },
                        },
                        {
                            data: 'item_desc',
                            className: 'text-start',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'total_in',
                            className: 'text-end text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                return '<span class="text-success">' + (new Intl.NumberFormat('id-ID')).format(data) + '</span>';
                            },
                        },
                        {
                            data: 'total_out',
                            className: 'text-end text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                return '<span class="text-danger">' + (new Intl.NumberFormat('id-ID')).format(data) + '</span>';
                            },
                        },
                        {
                            data: 'total_bl',
                            className: 'text-end text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                return (new Intl.NumberFormat('id-ID')).format(data);
                                // return (new Intl.NumberFormat('id-ID')).format(row.total_in - row.total_out);
                            },
                        },
                    ],
                });
            },
            setSummary: function({
                total_bl,
                total_in,
                total_out,
            }) {
                $('#totAllBl').text((new Intl.NumberFormat('id-ID')).format(total_bl));
                $('#totAllIn').text((new Intl.NumberFormat('id-ID')).format(total_in));
                $('#totAllOut').text((new Intl.NumberFormat('id-ID')).format(total_out));
            },
            eventChildRow: function() {
                $('#tLedgerBl tbody').on('click', 'td.dt-control', function() {
                    var tr = $(this).closest('tr');
                    var row = DTable.table.row(tr);

                    if (row.child.isShown()) {
                        // This row is already open - close it
                        row.child.hide();
                        tr.removeClass('shown');
                    } else {
                        // Open this row
                        row.child(DTable.formatChildRow(row.data())).show();
                        tr.addClass('shown');
                    }
                });
            },
            formatChildRow: function(d) {
                // `d` is the original data object for the row
                // let template = `<table cellpadding="5" cellspacing="0" border="0" style="padding-left:50px;">
            let template = `<table class="table table-bordered table-hover">
                        <thead style="border-style:none;">
                            <tr>
                                <th>Item</th>
                                <th class="text-end">Jumlah</th>
                                <th>Satuan</th>
                                <th class="text-center">Invoice (Rp)</th>
                                <th class="text-center">Pembayaran (Rp)</th>
                                <th class="text-center">Masuk (Rp)</th>
                                <th class="text-center">Keluar (Rp)</th>
                                <th class="text-center">Balance (Rp)</th>
                            </tr>
                        </thead>
                        <tbody>`
            for (let item of d.a_items) {
                // perlu diperbaiki cara if di query, jika kalkulasi ke vendor maka is_aprv = , jika ke client is_bill_aprv = 1
                if (item.calc_to_vdr == State.calc_to_vdr.yes && item.is_aprv != State.is_aprv.yes) continue;
                if (item.invc_to_client == State.invc_to_client.yes && item.is_bill_aprv != State.is_aprv.yes) continue;

                let small_desc = '',
                    amt_in = 0,
                    amt_out = 0,
                    amt_bl = 0;
                if (item.is_bill_paid == State.is_a_item_paid.paid) {
                    amt_in = item.amt_in;
                    small_desc += `<br><small>waktu dibayarkan <span class="text-success">${moment.unix(item.paid_bill_at).format('DD MMM YYYY HH:mm:ss')}</span></small>`;
                }
                if (item.is_paid == State.is_a_item_paid.paid) {
                    amt_out = item.amt_out;
                    small_desc += `<br><small>waktu keluar <span class="text-danger">${moment.unix(item.paid_at).format('DD MMM YYYY HH:mm:ss')}</span></small>`;
                }
                if (amt_in != 0 || amt_out != 0) {
                    amt_bl = item.amt_bl;
                }
                template += `<tr>
                            <td>${item.item_desc}${small_desc}</td>
                            <td class="text-end">${item.unit_qty}</td>
                            <td>${item.unit_type_name}</td>
                            <td class="text-end">${(new Intl.NumberFormat('id-ID')).format(item.amt_in)}</td>
                            <td class="text-end">${(new Intl.NumberFormat('id-ID')).format(item.amt_out)}</td>
                            <td class="text-end text-success">${(new Intl.NumberFormat('id-ID')).format(amt_in)}</td>
                            <td class="text-end text-danger">${(new Intl.NumberFormat('id-ID')).format(amt_out)}</td>
                            <td class="text-end">${(new Intl.NumberFormat('id-ID')).format(amt_bl)}</td>
                        </tr>`;
            }
            template += `</tbody>
                    </table>`;
                return template
            },
        };

        Wrapper.activate();
    </script>
@endsection
