@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('laporan-external') }}
@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="laporan-external-table" data-api-url="{{ route('laporan-external.datatables') }}">
            <div class="card-header bg-agi-50 py-5 flex-wrap">
                <h3 class="card-title">
                    Daftar Laporan External
                </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 Laporan External" id="search" type="text" value="">
                        </label>
                    </div>
                    <div class="flex flex-wrap gap-2.5 hidden">
                        <div class="h-[24px] border border-r-gray-200"></div>
                        <a class="btn btn-sm btn-light" href="{{ route('laporan-external.export') }}"> Export to Excel </a>
                        <a class="btn btn-sm btn-primary" href="{{ route('laporan-external.create') }}"> Tambah Laporan External </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_laporan">
                                <span class="sort"> <span class="sort-label"> Nomor Laporan </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="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_jaminan">
                                <span class="sort"> <span class="sort-label"> Jenis Jaminan </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="nilai_pasar">
                                <span class="sort"> <span class="sort-label"> Nilai Pasar </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="indikasi_nilai_likuidasi">
                                <span class="sort"> <span class="sort-label"> Indikasi Nilai Likuidasi </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>

                            <th class="min-w-[150px]" data-datatable-column="file_resume">
                                <span class="sort"> <span class="sort-label"> File Resume </span>
                                    <span class="sort-icon"> </span> </span>
                            </th>
                            <th class="min-w-[150px]" data-datatable-column="file_laporan">
                                <span class="sort"> <span class="sort-label"> File Laporan </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 deleteData(data) {
            Swal.fire({
                title: 'Are you sure?',
                text: "You won't be able to revert this!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, delete it!'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': '{{ csrf_token()  }}'
                        }
                    });

                    $.ajax(`laporan-external/${data}`, {
                        type: 'DELETE'
                    }).then((response) => {
                        swal.fire('Deleted!', 'Laporan External has been deleted.', 'success').then(() => {
                            window.location.reload();
                        });
                    }).catch((error) => {
                        console.error('Error:', error);
                        Swal.fire('Error!', 'An error occurred while deleting the laporan.', 'error');
                    });
                }
            })
        }
    </script>
    <script type="module">
        const element = document.querySelector('#laporan-external-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_laporan: {
                    title: 'Nomor Laporan',
                },
                nama_debitur: {
                    title: 'Nama Debitur',
                    render: (item, data) => {
                        return `${data.permohonan.debiture.name}`;
                    },
                },
                tujuan_penilaian: {
                    title: 'Tujuan Penilaian',
                    render: (item, data) => {
                        return `${data.permohonan.penawaran?.tujuan_penilaian_kjpp?.name}` ?? '';
                    }
                },
                jenis_jaminan: {
                    title: 'Jenis Jaminan',
                    render: (item, data) => {
                        return `${data.permohonan.dokumenjaminan[0]?.jenisjaminan?.name}` ?? '';
                    }
                },
                nilai_pasar: {
                    title: 'Nilai Pasar',
                },
                indikasi_nilai_likuidasi: {
                    title: 'Indikasi Nilai Likuidasi',
                },
                file_resume: {
                    title: 'File Resume',
                    render: (item, data) => {
                        return data.file_resume ? `<a href="storage/${data.file_resume}" download="${data.file_resume}" target="_blank" class="badge badge-sm badge-outline">
                        Download <i class="ki-filled ki-cloud-download"></i>
                    </a>` : 'N/A';
                    },
                },
                file_laporan: {
                    title: 'File Laporan',
                    render: (item, data) => {
                        return data.file_laporan ? `<a href="storage/${data.file_laporan}" download="${data.file_laporan}" target="_blank" class="badge badge-sm badge-outline">
                        Download <i class="ki-filled ki-cloud-download"></i>
                    </a>` : 'N/A';

                    },
                },
                actions: {
                    title: 'Action',
                    render: (item, data) => {
                        return `<div class="flex flex-nowrap justify-center">
                            <a class="btn btn-sm btn-icon btn-clear btn-info" href="laporan-external/${data.id}/edit">
                                <i class="ki-outline ki-notepad-edit"></i>
                            </a>
                        </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
