@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('laporan-penilaian-jaminan') }}
@endsection

@section('content')
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
        <!-- Filter Card -->
        <div class="card border border-agi-100">
            <div class="card-header bg-agi-50 py-5">
                <h3 class="card-title">Filter Laporan</h3>
            </div>
            <div class="card-body grid gap-4">
                <!-- Search field at the top, full width -->
                <div class="flex flex-col w-full">
                    <label class="text-sm font-medium mb-1">Pencarian</label>
                    <label class="input input-sm">
                        <i class="ki-filled ki-magnifier"></i>
                        <input placeholder="Search Laporan Penilaian Jaminan" id="search" type="text" value="">
                    </label>
                </div>

                <!-- Other filter fields in grid layout -->
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
                    <div class="flex flex-col">
                        <label class="text-base font-medium mb-1">Tanggal Awal</label>
                        <label class="input">
                            <input placeholder="Tanggal Awal" id="start_date" type="date">
                        </label>
                    </div>
                    <div class="flex flex-col">
                        <label class="text-base font-medium mb-1">Tanggal Akhir</label>
                        <label class="input">
                            <input placeholder="Tanggal Akhir" id="end_date" type="date">
                        </label>
                    </div>
                    <div class="flex flex-col">
                        <label class="text-base font-medium mb-1">Cabang</label>
                        <select class="select tomselect" id="branch_filter">
                            <option value="">Semua Cabang</option>
                            @foreach(\Modules\Basicdata\Models\Branch::where('status', 1)->get() as $branch)
                                <option value="{{ $branch->id }}">{{ $branch->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="flex flex-col">
                        <label class="text-base font-medium mb-1">Penilai</label>
                        <select class="select tomselect" id="penilai_filter">
                            <option value="">Semua Penilai</option>
                            @foreach(\MOdules\Usermanagement\Models\User::role(['penilai','surveyor'])->get() as $penilai)
                                <option value="{{ $penilai->id }}">{{ $penilai->name }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <!-- Buttons row at the bottom -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-2">
                    <button class="btn btn-sm btn-primary" id="filter_tanggal">
                        <i class="ki-outline ki-filter fs-2 me-1"></i>
                        Terapkan Filter
                    </button>
                    <a class="btn btn-sm btn-light" href="{{ route('laporan-hasil-penilaian-jaminan-internal-external.export') }}" id="export-btn">
                        <i class="ki-outline ki-file-down fs-2 me-1"></i>
                        Export to Excel
                    </a>
                </div>
            </div>
        </div>

        <!-- Data Table Card -->
        <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="laporan-penilaian-jaminan-table" data-api-url="{{ route('laporan-penilaian-jaminan.data') }}">
            <div class="card-header bg-agi-50 py-5 flex-wrap">
                <h3 class="card-title">
                    Laporan Penilaian Jaminan
                </h3>
            </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="branch">
                                <span class="sort"> <span class="sort-label"> Cabang </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="pemohon">
                                <span class="sort"> <span class="sort-label"> Pemohon </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="name">
                                <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="tujuan_penilaian">
                                <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_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="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="luas_tanah">
                                <span class="sort"> <span class="sort-label"> Luas Tanah </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="nilai_tanah">
                                <span class="sort"> <span class="sort-label"> Nilai Tanah </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="luas_bangunan">
                                <span class="sort"> <span class="sort-label"> Luas Bangunan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="nilai_bangunan">
                                <span class="sort"> <span class="sort-label"> Nilai Bangunan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="tanggal_laporan">
                                <span class="sort"> <span class="sort-label"> Tanggal Laporan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="tanggal_review">
                                <span class="sort"> <span class="sort-label"> Tanggal Review </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>
                        </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="module">
        const element = document.querySelector('#laporan-penilaian-jaminan-table');
        const searchInput = document.getElementById('search');
        const startDateInput = document.getElementById('start_date');
        const endDateInput = document.getElementById('end_date');
        const branchFilter = document.getElementById('branch_filter');
        const filterTanggalButton = document.getElementById('filter_tanggal');
        const penilaiFilter = document.getElementById('penilai_filter');
        const exportBtn = document.getElementById('export-btn');

        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',
                },
                tanggal_permohonan: {
                    title: 'Tanggal Permohonan',
                    render: (item, data) => {
                        return data.tanggal_permohonan ? window.formatTanggalIndonesia(data.tanggal_permohonan) : '-';
                    },
                },
                branch: {
                    title: 'Cabang',
                },
                name: {
                    title: 'Nama Debitur',
                },
                pemohon: {
                    title: 'Pemohon',
                },
                tujuan_penilaian: {
                    title: 'Tujuan Penilaian',
                },
                jenis_agunan: {
                    title: 'Jenis Agunan',
                },
                alamat_agunan: {
                    title: 'Alamat Agunan',
                },
                luas_tanah: {
                    title: 'Luas Tanah',
                },
                nilai_tanah: {
                    title: 'Nilai Tanah',
                },
                luas_bangunan: {
                    title: 'Luas Bangunan',
                },
                nilai_bangunan: {
                    title: 'Nilai Bangunan',
                },
                tanggal_laporan: {
                    title: 'Tanggal Laporan',
                    render: (item, data) => {
                        return data.tanggal_laporan ? window.formatTanggalIndonesia(data.tanggal_laporan) : '-';
                    },
                },
                tanggal_review: {
                    title: 'Tanggal Review',
                    render: (item, data) => {
                        return data.tanggal_review ? window.formatTanggalIndonesia(data.tanggal_review) : '-';
                    },
                },
                nilai_pasar_wajar: {
                    title: 'Nilai Pasar Wajar',
                    render: (item, data) => {
                        return data.nilai_pasar_wajar;
                    },
                },
                nilai_likuidasi: {
                    title: 'Nilai Likuidasi',
                },
                nama_penilai: {
                    title: 'Nama Penilai',
                }
            }
        };

        let dataTable = new KTDataTable(element, dataTableOptions);

        // Function to apply all filters
        function applyFilters() {
            let filters = {};
            const startDate = startDateInput.value;
            const endDate = endDateInput.value;
            const branch = branchFilter.value;
            const penilai = penilaiFilter.value;

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

            if (startDate) {
                filters.start_date = startDate;
            }

            if (endDate) {
                filters.end_date = endDate;
            }

            if (branch) {
                filters.branch_id = branch;
            }

            if (penilai) {
                filters.penilai_id = penilai;
            }

            dataTable.search(filters);
        }

        // Update export URL with filters
        function updateExportUrl() {
            let url = new URL(exportBtn.href);

            if (startDateInput.value) {
                url.searchParams.set('start_date', startDateInput.value);
            } else {
                url.searchParams.delete('start_date');
            }

            if (endDateInput.value) {
                url.searchParams.set('end_date', endDateInput.value);
            } else {
                url.searchParams.delete('end_date');
            }

            if (branchFilter.value) {
                url.searchParams.set('branch_id', branchFilter.value);
            } else {
                url.searchParams.delete('branch_id');
            }

            if (searchInput.value) {
                url.searchParams.set('search', searchInput.value);
            } else {
                url.searchParams.delete('search');
            }

            if (penilaiFilter.value) {
                url.searchParams.set('penilai_id', penilaiFilter.value);
            } else {
                url.searchParams.delete('penilai_id');
            }

            exportBtn.href = url.toString();
        }

        // Add event listeners for all inputs
        searchInput.addEventListener('input', () => {
            applyFilters();
            updateExportUrl();
        });

        filterTanggalButton.addEventListener('click', () => {
            applyFilters();
            updateExportUrl();
        });

        // Initial update of export URL
        updateExportUrl();
    </script>
@endpush
