@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('laporan-admin-kredit') }}
@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-admin-kredit-table"
            data-api-url="{{ route('admin-kredit.laporan.datatables') }}">
            <div class="flex-wrap py-5 card-header bg-agi-50">
                <h3 class="card-title">
                    Daftar Laporan Admin Kredit
                </h3>
                <div class="flex flex-wrap gap-2 lg:gap-5">
                    <div class="flex">
                        <label class="input input-sm">
                            <input placeholder="Tanggal Awal" id="tanggal_awal" type="date">
                        </label>
                    </div>
                    <div class="flex">
                        <label class="input input-sm">
                            <input placeholder="Tanggal Akhir" id="tanggal_akhir" type="date">
                        </label>
                    </div>
                    <div class="flex">
                        <button class="btn btn-sm btn-primary" id="filter_tanggal">Filter</button>
                    </div>
                    <div class="flex">
                        <label class="input input-sm"> <i class="ki-filled ki-magnifier"> </i>
                            <input placeholder="Search Laporan" 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="{{ route('admin-kredit.laporan.export') }}"> 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="cif">
                                    <span class="sort"> <span class="sort-label"> CIF </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="kode_register_t24">
                                    <span class="sort"> <span class="sort-label"> Kode Register T24 </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nama_debitur">
                                    <span class="sort"> <span class="sort-label"> Nama Debitur </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="cabang">
                                    <span class="sort"> <span class="sort-label"> Cabang </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="jenis_agunan">
                                    <span class="sort"> <span class="sort-label"> Jenis Agunan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="bukti_kepemilikan">
                                    <span class="sort"> <span class="sort-label"> Bukti Kepemilikan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="alamat_agunan">
                                    <span class="sort"> <span class="sort-label"> Alamat Agunan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nama_pemilik">
                                    <span class="sort"> <span class="sort-label"> Nama Pemilik </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="tanggal_kunjungan">
                                    <span class="sort"> <span class="sort-label"> Tanggal Kunjungan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nilai_pasar_wajar">
                                    <span class="sort"> <span class="sort-label"> Nilai Pasar Wajar </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nilai_likuidasi">
                                    <span class="sort"> <span class="sort-label"> Nilai Likuidasi </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="nama_penilai">
                                    <span class="sort"> <span class="sort-label"> Nama Penilai </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[150px]" data-datatable-column="kolektibilitas">
                                    <span class="sort"> <span class="sort-label"> Kolektibilitas </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[200px]" data-datatable-column="keterangan">
                                    <span class="sort"> <span class="sort-label"> Keterangan </span>
                                        <span class="sort-icon"> </span> </span>
                                </th>
                                <th class="min-w-[100px]" data-datatable-column="actions">
                                    <span class="sort"> <span class="sort-label"> Actions </span>
                                        <span class="sort-icon"> </span> </span>
                                </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="module">
        const element = document.querySelector('#laporan-admin-kredit-table');
        const searchInput = document.getElementById('search');
        const tanggalAwalInput = document.getElementById('tanggal_awal');
        const tanggalAkhirInput = document.getElementById('tanggal_akhir');
        const filterTanggalButton = document.getElementById('filter_tanggal');


        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();
                    },
                },
                cif: {
                    title: 'CIF',
                    render: (item, data) => {
                        return data.debiture ? data.debiture.cif : '';
                    },
                },
                kode_register_t24: {
                    title: 'Kode Register T24',
                },
                nama_debitur: {
                    title: 'Nama Debitur',
                    render: (item, data) => {
                        return data.debiture ? data.debiture.name : '';
                    },
                },
                cabang: {
                    title: 'Cabang',
                    render: (item, data) => {
                        return data.debiture ? data.debiture.branch.name : '';
                    },
                },
                jenis_agunan: {
                    title: 'Jenis Agunan',
                },
                bukti_kepemilikan: {
                    title: 'Bukti Kepemilikan',
                },
                alamat_agunan: {
                    title: 'Alamat Agunan',
                },
                nama_pemilik: {
                    title: 'Nama Pemilik',
                },
                tanggal_kunjungan: {
                    title: 'Tanggal Kunjungan',
                    render: (item, data) => {
                        return window.formatTanggalIndonesia(data.tanggal_kunjungan);
                    },
                },
                nilai_pasar_wajar: {
                    title: 'Nilai Pasar Wajar',
                    render: (item, data) => {
                        return window.formatRupiah(data.nilai_pasar_wajar);
                    },
                },
                nilai_likuidasi: {
                    title: 'Nilai Likuidasi',
                    render: (item, data) => {
                        return window.formatRupiah(data.nilai_likuidasi);
                    },
                },
                nama_penilai: {
                    title: 'Nama Penilai',
                },
                kolektibilitas: {
                    title: 'Kolektibilitas',
                },
                keterangan: {
                    title: 'Keterangan',
                    render: (item, data) => {
                        // Truncate keterangan if too long
                        if (data.keterangan && data.keterangan.length > 50) {
                            return data.keterangan.substring(0, 50) + '...';
                        }
                        return data.keterangan || '-';
                    },
                },
                actions: {
                    title: 'Action',
                    render: (item, data) => {
                        return `<div class="flex flex-nowrap justify-center">
            <a href="admin-kredit/laporan/${data.id}/edit" class="btn btn-sm btn-outline btn-info">
                <i class="ki-filled ki-pencil"></i>
            </a>
        </div>`;
                    },
                }
            }
        };

        let dataTable = new KTDataTable(element, dataTableOptions);

        // Function to apply all filters
        function applyFilters() {
            let filters = {};
            const tanggalAwal = tanggalAwalInput.value;
            const tanggalAkhir = tanggalAkhirInput.value;

            console.table(tanggalAwal, tanggalAkhir);

            if (searchInput.value) {
                filters.search = searchInput.value;
            }

            if (tanggalAwal) {
                filters.tanggal_awal = tanggalAwal
            }

            if (tanggalAkhir) {
                filters.tanggal_akhir = tanggalAkhir
            }

            dataTable.search(filters);
        }

        // Add event listeners for all inputs
        searchInput.addEventListener('input', applyFilters);
        filterTanggalButton.addEventListener('click', applyFilters);
    </script>
@endpush
