@extends('layouts.main')

{{-- @section('breadcrumbs') --}}
{{-- {{ Breadcrumbs::render('laporan.index') }} --}}
{{-- @endsection --}}


@section('content')
    <div class="grid gap-5 mx-auto w-full lg:gap-7.5">
        <div class="min-w-full border card border-agi-100 card-grid" data-datatable="false" data-datatable-page-size="10"
            data-datatable-state-save="false" id="laporan-table" data-api-url="{{ route('laporan.datatables') }}">
            <div class="flex-wrap py-5 card-header bg-agi-50">
                <h3 class="card-title">
                    Laporan
                </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 Penilai" 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 text-sm font-medium text-gray-700 align-middle table-auto table-border"
                        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="debitur_id">
                                    <span class="sort"> <span class="sort-label"> Debitur </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"> Pemohon(Cabang/Direktorat) </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"> AO </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="tujuan_penilaian_id">
                                    <span class="sort"> <span class="sort-label"> Tujuan Penilaian </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="jenis_fasilitas_kredit_id">
                                    <span class="sort"> <span class="sort-label"> Fasilitas Kredit </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="tanggal_survei">
                                    <span class="sort"> <span class="sort-label"> Tanggal Survei </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="due_date_sla">
                                    <span class="sort"> <span class="sort-label"> Due Date SLA </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[50px] text-center" data-datatable-column="status">Status</th>
                                <th class="min-w-[50px] text-center" data-datatable-column="actions">Actions</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <div
                    class="flex-col gap-3 justify-center font-medium text-gray-600 card-footer md:justify-between md:flex-row text-2sm">
                    <div class="flex gap-2 items-center">
                        Show
                        <select class="w-16 select select-sm" data-datatable-size="true" name="perpage"> </select> per
                        page
                    </div>
                    <div class="flex gap-4 items-center">
                        <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 formatDate(date) {
            const day = date.getDate().toString().padStart(2, '0');
            const month = (date.getMonth() + 1).toString().padStart(2, '0');
            // Months are 0-indexed
            const year = date.getFullYear();

            return `${day} ${getIndonesianMonth(month)} ${year}`;
        }

        function getIndonesianMonth(month) {
            const months = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
                'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
            ];
            return months[month -
                1];
        }
    </script>
    <script type="module">
        const element = document.querySelector('#laporan-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',
                },
                debitur_id: {
                    title: 'Debitur',
                    render: (item, data) => {
                        if (data.debiture) {
                            return `${data.debiture.name.toUpperCase()}`;
                        }
                        return "-";
                    },
                },
                branch_id: {
                    title: 'Cabang Pemohon',
                    render: (item, data) => {
                        return `${data.branch.name}`;
                    },
                },
                user_id: {
                    title: 'User Pemohon',
                    render: (item, data) => {
                        return `${data.user?.name}`;
                    },
                },
                tujuan_penilaian_id: {
                    title: 'Tujuan Penilaian',
                    render: (item, data) => {
                        switch (data.tujuan_penilaian.code) {
                            case "TP0001":
                                return `<span class="badge badge-sm badge-primary">${data.tujuan_penilaian.name}</span>`;
                            case "TP0002":
                                return `<span class="badge badge-sm badge-info">${data.tujuan_penilaian.name}</span>`;
                            case "TP0003":
                                return `<span class="badge badge-sm badge-success">${data.tujuan_penilaian.name}</span>`;
                            case "TP0004":
                                return `<span class="badge badge-sm badge-danger">${data.tujuan_penilaian.name}</span>`;
                            case "TP0005":
                                return `<span class="badge badge-sm badge-warning">${data.tujuan_penilaian.name}</span>`;
                            case "TP0006":
                                return `<span class="badge badge-sm badge-dark">${data.tujuan_penilaian.name}</span>`;
                            case "TP0007":
                                return `<span class="badge badge-sm badge-outline badge-info">${data.tujuan_penilaian.name}</span>`;
                            default:
                                return `<span class="badge badge-sm badge-outline badge-default">${data.tujuan_penilaian.name}</span>`;
                        }
                    },
                },
                jenis_fasilitas_kredit_id: {
                    title: 'Fasilitas Kredit',
                    render: (item, data) => {
                        return `${data.jenis_fasilitas_kredit?.name}`;
                    }
                },
                tanggal_survei: {
                    title: 'Tanggal Survei',
                    render: (item, data) => {
                        if (data.penilaian?.waktu_penilaian) {
                            return `${formatDate(new Date(data.penilaian?.waktu_penilaian))}`;
                        }
                        return `${formatDate(new Date(data.penilaian?.created_at))}`;
                    },
                },
                due_date_sla: {
                    title: 'Due Date SLA',
                    render: (item, data) => {
                        const tujuan_penilaian = data.tujuan_penilaian.name;
                        const tipe_laporan = data.penilai?.type;
                        const nilai_plafond = data.penilaian?.nilaiPlafond?.name;
                        let waktu_penilaian = new Date(data.penilaian?.created_at);
                        if (data.penilaian?.waktu_penilaian) {
                            waktu_penilaian = new Date(data.penilaian?.waktu_penilaian);
                        }

                        if (tujuan_penilaian.name === "RAP") {
                            waktu_penilaian.setDate(waktu_penilaian.getDate() + 3);
                        } else {
                            if (tipe_laporan === "sederhana") {
                                waktu_penilaian.setDate(waktu_penilaian.getDate() + 2);
                            } else if (tipe_laporan === "standar") {
                                if (nilai_plafond === "2 M - 5 M") {
                                    waktu_penilaian.setDate(waktu_penilaian.getDate() + 3);
                                } else if (nilai_plafond === "< 2M") {
                                    waktu_penilaian.setDate(waktu_penilaian.getDate() + 3);
                                } else {
                                    waktu_penilaian.setDate(waktu_penilaian.getDate() + 5);
                                }
                            }
                        }
                        return formatDate(waktu_penilaian);
                    },
                },
                status: {
                    title: 'Status',
                    render: (item, data) => {
                        return `<span class="flex justify-center uppercase badge badge-sm badge-default">${data.status}</span>`;
                    }
                },
                actions: {
                    title: 'Actions',
                    render: (item, data) => {
                        const inspeksiId = data.documents[0]?.inspeksi[0]?.id || '-';
                        const dokumenID = data.documents[0]?.id || '-';
                        const jenisJaminanID = data.documents[0]?.jenis_jaminan_id || '-';
                        const typePenilaian = data.penilai?.type_penilai || '';
                        const type = data.penilai?.type || '';
                        let laporanButton = '';
                        let resumeButton = '';

                        if (data.penilai?.resume) {
                            resumeButton = `
                            <a href="penilai/print-out-laporan/${data.id}/${dokumenID}/${jenisJaminanID}" class="btn btn-sm btn-success">
                                Resume
                            </a>`;
                        }

                        if (data.status_bayar == "sudah_bayar" || data.status_bayar == "tidak_bayar") {
                            laporanButton = `
                            <a href="penilai/print-out-laporan/${data.id}/${dokumenID}/${jenisJaminanID}" class="btn btn-sm btn-primary">
                                Laporan
                            </a>`;
                        }

                        return `<div class="flex flex-wrap gap-1.5 justify-end"> ${resumeButton} ${laporanButton} </div>`;
                    },
                }
            },
        };

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

        });
    </script>
@endpush
