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

@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <p class="card-title text-bold mb-0">Riwayat Transfer Uang Saku</p>
                            <p class="card-subtitle text-muted">Daftar Transfer</p>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="tTable" class="table table-hover dataTable w-100">
                                    <thead>
                                        <tr class="">
                                            <th class="">#</th>
                                            <th class=""></th>
                                            <th class="text-nowrap">Kode Order</th>
                                            {{-- <th class="text-nowrap">Kode Referensi</th> --}}
                                            <th class="text-start">Lokasi Transfer</th>
                                            <th class="text-nowrap">Bank</th>
                                            <th class="text-nowrap">Pemlik Rekening</th>
                                            <th class="text-nowrap text-end">No Rekening</th>
                                            <th class="text-nowrap text-end">Total (Rp)</th>
                                            <th class="text-center">Status</th>
                                            <th class="text-start">Catatan</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="">
                                            <td class="">1</td>
                                            <td class="">
                                                <a href="#" class="btnResend text-decoration-none me-1">
                                                    <span class="icon ion-undo text-danger fz-16"></span>
                                                </a>
                                            </td>
                                            <td class="text-nowrap">#52314789</td>
                                            {{-- <td class="text-nowrap">#89901280921890</td> --}}
                                            <td class="text-start">Jangan Lupa Bahagia (JLB)</td>
                                            <td class="text-nowrap">
                                                <p class="mb-0">BCA (014)</p>
                                                <p class="mb-0">Bank Central Asia</p>
                                            </td>
                                            <td class="text-start">Emrsyf</td>
                                            <td class="text-center">0101010100101</td>
                                            <td class="text-center">2.500.000</td>
                                            <td class="text-center"><span class="text-danger">Fail</span></td>
                                            <td class="text-start">-</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- modal resend pocket money tf --}}
    <div class="modal fade" id="mdlResendPocket" data-bs-backdrop="static" data-bs-keyboard="false" aria-labelledby="mdlResendPocketLabel" aria-hidden="true">
        <div class="modal-dialog modal-md modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="mdlResendPocketLabel">Transfer Ulang</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form action="">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="mb-3">
                                    <div class="form-check">
                                        {{-- <input class="form-check-input" name="isResendPocket" type="checkbox" value="no" id="isResendPocket"> --}}
                                        <label class="form-check-label text-dark" for="isResendPocket">
                                            <p class="mb-1">Transfer ulang uang saku ?</p>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" id="btnSubmitResendPocket" class="btn btn-sm btn-primary">Transfer</button>
                    <div id="btnSubmitResendPocketSpinner" class="d-none">
                        <div class="spinner-border" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {{-- modal resend pocket money tf --}}
@endsection

@section('customjs')
    <script>
        'use strict'

        const State = {
            stts: {
                unpaid: "{{ \App\Models\OrdersLogsTf::STTS_UNPAID }}",
                paid: "{{ \App\Models\OrdersLogsTf::STTS_PAID }}",
                fail: "{{ \App\Models\OrdersLogsTf::STTS_FAIL }}",
                pending: "{{ \App\Models\OrdersLogsTf::STTS_PENDING }}",
            },
            checkpoint_paid: {
                unpaid: "{{ \App\Models\OrdersCheckpoints::IS_UNPAID }}",
                paid: "{{ \App\Models\OrdersCheckpoints::IS_PAID }}",
                fail: "{{ \App\Models\OrdersCheckpoints::IS_TF_FAIL }}",
            },
            delay_type_number: 1000,
            storage_lara: "{{ asset('storage') }}/",
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
        };

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

        const DTable = {
            activate: function() {
                DTable.reload();
            },
            reload: function() {
                // $('#tTable').DataTable();
                // if (Driver.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { Driver.Table.firstInitDataTable = 1; }
                $('#tTable').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    // fixedColumns:   {
                    //     left: 3,
                    // },
                    ajax: {
                        url: "{{ route('api_finance_list_pocket_tf_history') }}",
                        type: 'GET',
                        complete: function() {
                            // 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 = '';
                                if (row.pocket_is_paid == State.checkpoint_paid.fail) {
                                    action += `
                                    <a href="#" class="btnResendPocket text-decoration-none me-1" data-bs-toggle="tooltip" data-bs-placement="top" title="Kirim Ulang">
                                        <span class="icon ion-refresh text-danger fz-16"></span>
                                    </a>
                                `;
                                }
                                // <a href="#" class="text-decoration-none text-danger btnDelDrv">
                                //     <span class="icon ion-trash-b fz-16"></span>
                                // </a>
                                return action;
                            }
                        },
                        {
                            data: 'ord_code',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            createdCell: function(td, cellData, rowData, row, col) {
                                // $(td).attr('data-id', rowData.id);
                                $(td).attr('data-ord_id', rowData.ord_id);
                                $(td).attr('data-ord_code', rowData.ord_code);
                                $(td).attr('data-checkpoint_id', rowData.ord_checkpoint_id);
                            },
                            render: function(data, type, row, meta) {
                                return '#' + data;
                            },
                        },
                        // {
                        //     data: 'ref_code',
                        //     className: 'text-nowrap',
                        //     visible: true,
                        //     orderable: true,
                        //     searchable: true,
                        //     render: function(data, type, row, meta) {
                        //         return '#'+data;
                        //     },
                        // },
                        {
                            data: 'pck_name',
                            className: 'text-start',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'bank_id',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                return `
                            <p class="mb-0">${row.bank_short_name} (${row.bank_code})</p>
                            <p class="mb-0">${row.bank_name}</p>
                            `;
                            },
                        },
                        {
                            data: 'bank_acc_name',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'bank_acc_number',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },

                        {
                            data: 'pocket_total',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                return (new Intl.NumberFormat('id-ID')).format(data);
                            },
                        },
                        // {
                        //     data: 'stts', // log.stts
                        //     className: 'text-center',
                        //     visible: true,
                        //     orderable: true,
                        //     searchable: true,
                        //     render: function(data, type, row, meta) {
                        //         if (data == State.stts.unpaid) {
                        //             return '<span class="text-dark">Unpaid</span>';
                        //         } else if (data == State.stts.paid) {
                        //             return '<span class="text-success">Success</span>';
                        //         } else if (data == State.stts.fail) {
                        //             return '<span class="text-danger">Fail</span>';
                        //         } else if (data == State.stts.pending) {
                        //             return '<span class="text-warning">Pending</span>';
                        //         }
                        //         return '<span class="text-danger">Fail</span>';
                        //     },
                        // },
                        {
                            data: 'pocket_is_paid', // checkpoint.is_paid
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (data == State.checkpoint_paid.unpaid) {
                                    return '<span class="text-dark">Unpaid</span>';
                                } else if (data == State.checkpoint_paid.paid) {
                                    return '<span class="text-success">Success</span>';
                                } else if (data == State.checkpoint_paid.fail) {
                                    return '<span class="text-danger">Fail</span>';
                                } else if (data == State.checkpoint_paid.pending) {
                                    return '<span class="text-warning">Pending</span>';
                                }
                                return '<span class="text-danger">Fail</span>';
                            },
                        },
                        {
                            data: 'tf_note',
                            className: 'text-start',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                    ],
                });
            },
        };

        const ResendPocket = {
            activate: function() {
                $('#tTable').on('click', '.btnResendPocket', async function(e) {
                    let checkpoint_id = $(e.target).closest('tr').find('td[data-checkpoint_id]').data('checkpoint_id');
                    console.log(checkpoint_id);
                    // let resp = await ResendPocket.reqData({
                    //     checkpoint_id
                    // });
                    // if (resp.type != 'success') {
                    //     Helper.toast('Riwayat tidak ditemukan', 'just now', 'please try again');
                    //     return false;
                    // }
                    // ResendPocket.passDataToView(resp.data);
                    $('#mdlResendPocket').data('checkoint_id', checkpoint_id);
                    $('#mdlResendPocket').modal('show');
                });

                $('#btnSubmitResendPocket').on('click', async function() {
                    let data = ResendPocket.getData();
                    let isValid = ResendPocket.checkData(data, true);
                    if (!isValid) return false;
                    let submitItems = await ResendPocket.submitResendPocket(data);
                });
            },
            getData: function() {
                let data = {};

                data.ord_checkpoint_id = $('#mdlResendPocket').data('checkoint_id');

                return data;
            },
            checkData: function(data, isAlert = false) {
                if (!data.ord_checkpoint_id) {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Riwayat transfer tidak valid');
                    return false;
                }
                return true;
            },
            submitResendPocket: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnSubmitResendPocket').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnSubmitResendPocket').attr('disabed', true);
                    $('#btnSubmitResendPocketSpinner').removeClass('d-none');
                    $.ajax({
                        url: "{{ route('api_finance_resend_pocket') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                            'x-api-key': Helper.getCookie('_trtk'),
                        },
                        data: data,
                        success: (data, textStatus, jqXHR) => {
                            $('#btnSubmitResendPocket').removeAttr('disabed');
                            $('#btnSubmitResendPocketSpinner').addClass('d-none');
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            Helper.toast('Success', 'just now', 'success transfer ulang');
                            $('#mdlResendPocket').modal('hide');
                            DTable.reload();
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            $('#btnSubmitResendPocket').removeAttr('disabed');
                            $('#btnSubmitResendPocketSpinner').addClass('d-none');
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                });
            },
        }

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