@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('pembayaran.approval') }}
@endsection

@section('content')
    <div class="grid">
        <div class="card border border-agi-100 card-grid min-w-full" data-datatable="false" data-datatable-page-size="10" data-datatable-state-save="false"
             id="pembayaran-table" data-api-url="{{ route('pembayaran.approval.datatables') }}">
            <div class="card-header light:bg-agi-50 py-5 flex-wrap">
                <h3 class="card-title">
                    Daftar Pembayaran
                </h3>
                <div class="flex flex-wrap gap-2 lg:gap-5">
                    <div class="flex">
                        <label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
                            <input placeholder="Search Pembayaran" id="search" type="text" value="">
                        </label>
                    </div>
                    <div class="flex flex-wrap gap-2.5">
                        <div class="h-[24px] border border-r-gray-200"></div>
                        <a class="btn btn-sm btn-light" href="#"> Export to Excel </a>
                    </div>
                </div>
            </div>
            <div class="card-body">
                <div class="scrollable-x-auto">
                    <table class="table table-auto table-border align-middle text-gray-700 font-medium text-sm"
                           data-datatable-table="true">
                        <thead>
                        <tr>
                            <th class="w-14">
                                <input class="checkbox checkbox-sm" data-datatable-check="true" type="checkbox"/>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="nomor_registrasi">
                                <span class="sort"> <span class="sort-label"> Nomor Registrasi </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="tanggal_permohonan">
                                <span class="sort"> <span class="sort-label"> Tanggal Permohonan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="user_id">
                                <span class="sort"> <span class="sort-label"> User Pemohon </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="branch_id">
                                <span class="sort"> <span class="sort-label"> Cabang Pemohon </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="debitur_id">
                                <span class="sort"> <span class="sort-label"> Debitur </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>

                            <th class="min-w-[150px] text-center" data-datatable-column="tujuan_penilaian_id">
                                <span class="sort"> <span class="sort-label"> Status Bayar </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>

                            <th class="min-w-[150px]" data-datatable-column="tanggal_setor">
                                <span class="sort"> <span class="sort-label"> Tanggal KSL </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="nominal_bayar">
                                <span class="sort"> <span class="sort-label"> Nominal bayar </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="bukti_bayar">
                                <span class="sort"> <span class="sort-label"> Bukti Bayar </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="approval_by">
                                <span class="sort"> <span class="sort-label"> Approval By </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>

                            <th class="min-w-[150px]" data-datatable-column="approve_keterangan_bayar">
                                <span class="sort"> <span class="sort-label"> Keterangan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>

                            <th class="min-w-[50px] text-center" data-datatable-column="actions">Action</th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div
                    class="card-footer justify-center md:justify-between flex-col md:flex-row gap-3 text-gray-600 text-2sm font-medium">
                    <div class="flex items-center gap-2">
                        Show
                        <select class="select select-sm w-16" data-datatable-size="true" name="perpage"> </select> per
                        page
                    </div>
                    <div class="flex items-center gap-4">
                        <span data-datatable-info="true"> </span>
                        <div class="pagination" data-datatable-pagination="true">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script type="text/javascript">
        function handlePembayaran(id, type) {
            let rv = type
            let title = type === 'revisi' ? 'Apakah Anda yakin ingin merevisi pembayaran ini?' : 'Apakah Anda yakin?';
            let text = type === 'revisi' ? 'Untuk melakukan revisi pembayaran!' : 'Untuk melakukan approve pembayaran!';

            Swal.fire({
                title: title,
                text: text,
                icon: 'warning',
                input: 'textarea', // Menambahkan input textarea
                inputLabel: 'Keterangan',
                inputPlaceholder: 'Masukkan keterangan...',
                inputAttributes: {
                    'aria-label': 'Masukkan keterangan'
                },
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Ya, Lanjutkan!',
                cancelButtonText: 'Lain kali',
            }).then((result) => {
                if (result.isConfirmed) {

                    let useURL = "{{ URL::to('pembayaran') }}" + "/" + id;
                    const keterangan = result.value || ''; // Ambil pesan dari textarea

                    var input_data = new Object();
                    input_data._method = 'PUT';
                    input_data.id = id;
                    input_data.keterangan = keterangan;
                    input_data.type = rv;

                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token() }}'
                        },
                    });
                    $.ajax({
                        url: useURL,
                        type: 'PUT',
                        data: input_data,
                        dataType: "json",
                        success: (response) => {
                            if ('success' == response.status) {
                                Swal.fire('Berhasil!', response.message, 'success').then(() => {
                                    window.location.reload();
                                });
                            } else if ('error' == response.status) {
                                Swal.fire('Error!', response.message, 'error').then(() => {
                                    window.location.reload();
                                });
                            }

                            console.log(response);
                        },
                        error: (error) => {
                            console.error('Error:', error);
                            Swal.fire('Gagal!', 'Terjadi kesalahan saat melakukan otorisator.',
                                'error');
                        }
                    });
                }
            });
        }
    </script>
    <script type="module">
        const element = document.querySelector('#pembayaran-table');
        const searchInput = document.getElementById('search');

        const apiUrl = element.getAttribute('data-api-url');
        const dataTableOptions = {
            apiEndpoint: apiUrl,
            pageSize: 5,
            columns: {
                select: {
                    render: (item, data, context) => {
                        const checkbox = document.createElement('input');
                        checkbox.className = 'checkbox checkbox-sm';
                        checkbox.type = 'checkbox';
                        checkbox.value = data.id.toString();
                        checkbox.setAttribute('data-datatable-row-check', 'true');
                        return checkbox.outerHTML.trim();
                    },
                },
                nomor_registrasi: {
                    title: 'Nomor Registrasi',
                    render: (item, data) => {
                        if (data.permohonan) {
                            return `${data.permohonan.nomor_registrasi}`;
                        } else if(data.penawaran) {
                            return `${data.penawaran.permohonan.nomor_registrasi}`;
                        }
                        return "";

                    },
                },
                tanggal_permohonan: {
                    title: 'Tanggal Permohonan',
                    render: (item, data) => {
                        if (data.permohonan) {
                            return `${data.permohonan.tanggal_permohonan}`;
                        } else if(data.penawaran) {
                            return `${data.penawaran.permohonan.tanggal_permohonan}`;
                        }
                        return "";
                    },
                },
                user_id: {
                    title: 'User Pemohon',
                    render: (item, data) => {
                        if (data.permohonan) {
                            return `${data.permohonan.user.name}`;
                        }
                        else if(data.penawaran) {
                            return `${data.penawaran.permohonan.user.name}`;
                        }
                        return "";

                    },
                },
                branch_id: {
                    title: 'Cabang Pemohon',
                    render: (item, data) => {
                        if (data.permohonan) {
                            return `${data.permohonan.branch.name}`;
                        }
                        else if(data.penawaran) {
                            return `${data.penawaran.permohonan.branch.name}`;
                        }
                        return "";

                    },
                },
                debitur_id: {
                    title: 'Debitur',
                    render: (item, data) => {
                        if (data.permohonan) {
                            return `${data.permohonan.debiture.name}`;
                        }
                        else if(data.penawaran) {
                            return `${data.penawaran.permohonan.debiture.name}`;
                        }
                        return "";

                    },
                },
                status_bayar: {
                    title: 'Status Bayar',
                    render: (item, data) => {
                        if(data.permohonan){
                            var permohonan = data.permohonan;
                        } else if(data.penawaran){
                            var permohonan = data.penawaran.permohonan;
                        }

                        if(permohonan) {

                            const status = permohonan.status_bayar.replace(/_/g,
                                ' ');
                            const statusClass = permohonan.status_bayar === 'belum_bayar' ? 'text-red-600' :
                                'text-green-600';
                            return `<span class="text-md font-bold ${statusClass} uppercase">
                            ${status}
                            </span>`;
                        }

                        return "-";
                    },
                },
                tanggal_setor: {
                    title: 'Tanggal Setor',
                    render: (item, data) => {
                        return `${window.formatTanggalIndonesia(data.created_at)}`;
                    },
                },
                nominal_bayar: {
                    title: 'Nominal Bayar',
                    render: (item, data) => {
                        return `${window.formatRupiah(data.nominal_bayar)}`;
                    },
                },
                bukti_bayar:{
                    title: 'Bukti Bayar',
                    render: (item, data) => {
                        if (data.bukti_bayar) {
                            return `<a href="storage/${data.bukti_bayar}" download="storage/${data.bukti_bayar}" target="_blank" class="badge badge-sm badge-outline">
                        Download <i class="ki-filled ki-cloud-download"></i>
                    </a>`;
                        } else {
                            return '-';
                        }
                    },
                },
                approve_bayar_by: {
                    title: 'Status Approve',
                    render: (item, data) => {
                        if(data.permohonan){
                            var permohonan = data.permohonan;
                        } else if(data.penawaran){
                            var permohonan = data.penawaran.permohonan;
                        }

                        if(permohonan) {
                            if(permohonan.approve_bayar) {
                                return `${permohonan.approve_bayar.name}`;
                            }
                            return "";
                        }

                        return "Menunggu Approval";
                    },
                },
                approve_keterangan_bayar: {
                    title: 'Status Approve',
                    render: (item, data) => {
                        if(data.permohonan){
                            var permohonan = data.permohonan;
                        } else if(data.penawaran){
                            var permohonan = data.penawaran.permohonan;
                        }

                        if(permohonan) {
                            return `${permohonan.approve_keterangan_bayar}` || '-';
                        }

                        return "-";
                    },
                },
                actions: {
                    title: 'Status',
                    render: (item, data) => {
                        if(data.permohonan){
                            var permohonan = data.permohonan;
                        } else if(data.penawaran){
                            var permohonan = data.penawaran.permohonan;
                        }

                        var iconPembayaranOtorisator = '';
                        if(permohonan) {
                            if (!permohonan.approve_bayar_by) {
                                iconPembayaranOtorisator = `<a class="btn btn-sm btn-icon btn-clear btn-primary " onclick="handlePembayaran(${permohonan.id},'otorisator')">
                                <i class="ki-filled ki-double-check"></i>
                            </a>
                            <a class="btn btn-sm btn-icon btn-clear btn-primary " onclick="handlePembayaran(${permohonan.id}, 'revisi')">
                                <i class="ki-filled ki-arrow-circle-left"></i>
                            </a>

                            `;
                            }

                            return `<div class="flex flex-nowrap justify-center">` + iconPembayaranOtorisator + `</div>`;
                        }

                        return "";

                    },
                }
            },
        };

        let dataTable = new KTDataTable(element, dataTableOptions);
        // Custom search functionality
        searchInput.addEventListener('input', function () {
            const searchValue = this.value.trim();
            dataTable.search(searchValue, true);

        });
    </script>
@endpush

