@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render(request()->route()->getName()) }}
@endsection

@section('content')
    @include('lpj::assetsku.includenya')
    <div class="grid gap-5 mx-auto w-full lg:gap-7.5">
        <div class="pb-2.5 border card border-agi-100">
            <div class="card-header light:light:bg-agi-50" id="basic_settings">
                <h3 class="card-title">
                    Detail Penilaian
                </h3>
                <div class="flex gap-2 items-center">
                    <a href="{{ route('penilai.index') }}" class="btn btn-xs btn-info"><i class="ki-filled ki-exit-left"></i>
                        Back</a>
                </div>
            </div>
            <div class="grid gap-5 card-body">
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">
                        Nomor Registrasi
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->nomor_registrasi) ? $permohonan->nomor_registrasi : '' }}</p>
                    </div>
                    <label class="form-label max-w-56">
                        Nama Debitur
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->debiture->name) ? $permohonan->debiture->name : '' }}</p>
                    </div>
                </div>
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">
                        Cabang
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->branch->name) ? $permohonan->branch->name : '' }}</p>
                    </div>
                    <label class="form-label max-w-56">
                        Alamat Jaminan
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ formatAlamat($permohonan->dokumenjaminan[0]) }}
                        </p>
                    </div>
                </div>
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">
                        Nama AO/Pemohon
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->user->name) ? $permohonan->user->name : '' }}</p>
                    </div>
                    <label class="form-label max-w-56">
                        Fasilitas Kredit
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->jenisFasilitasKredit->name) ? $permohonan->jenisFasilitasKredit->name : '' }}
                        </p>
                    </div>
                </div>
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">
                        Tanggal Permohonan
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ formatTanggalIndonesia($permohonan->created_at) }}</p>
                    </div>
                    <label class="form-label max-w-56">
                        CIF
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ isset($permohonan->debiture->cif) ? $permohonan->debiture->cif : '' }}</p>
                    </div>


                </div>
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">
                        Tanggal Konfirmasi Kunjungan
                    </label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ formatTanggalIndonesia($permohonan->penilaian->waktu_penilaian) ?? '' }}</p>
                    </div>
                    @if (!in_array(strtolower($permohonan->tujuanPenilaian->name), ['penilaian ulang', 'asuransi', 'lelang']))
                        <label class="form-label max-w-56">
                            Status Bayar
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <span
                                class="text-md font-bold {{ $permohonan->status_bayar === 'belum_bayar' ? 'text-red-600' : 'text-green-600' }} uppercase">
                                {{ str_replace('_', ' ', $permohonan->status_bayar) }}
                            </span>
                        </div>
                    @endif
                </div>
            </div>
        </div>


        @foreach ($permohonan->documents as $dokumen)
            <div class="border card border-agi-100">
                <div class="grid gap-5 card-body">
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        @php
                            $surveyor = $permohonan->penilaian->userPenilai->where('role', 'surveyor')->first();
                            $penilai = $permohonan->penilaian->userPenilai->where('role', 'penilai')->first();
                        @endphp
                        <label class="form-label max-w-56">
                            Surveyor
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">

                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ $surveyor->userPenilaiTeam->name }}
                            </p>

                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ checkRegionUserName($surveyor->userPenilaiTeam->id) }}
                            </p>
                        </div>
                        <label class="form-label max-w-56">
                            Penilai
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">

                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ $penilai->userPenilaiTeam->name }}
                            </p>

                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ checkRegionUserName($penilai->userPenilaiTeam->id) }}
                            </p>
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Tujuan Penilaian
                        </label>
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            {{ $permohonan->tujuanPenilaian->name }}</p>
                        <label class="form-label max-w-56">
                            Jenis Jaminan
                        </label>
                        <p class="flex w-full text-sm font-medium text-gray-600">
                            @foreach ($permohonan->documents as $document)
                                {{ $document->jenisjaminan->name }}
                            @endforeach
                        </p>
                    </div>
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Tanggal Survei
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ formatTanggalIndonesia($permohonan->penilaian->updated_at) }}</p>
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Tanggal Laporan
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <p class="flex w-full text-sm font-medium text-gray-600">

                                @php
                                    $tglLaporan = getNomorLaporan($permohonan->id, $dokumen->id, 'tanggal_laporan');
                                @endphp

                                {{ isset($tglLaporan) ? formatTanggalIndonesia($tglLaporan) : '-' }}</p>
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            No. Laporan
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <p class="flex w-full text-sm font-medium text-gray-600">
                                {{ getNomorLaporan($permohonan->id, $dokumen->id) ?? '-' }}
                            </p>
                        </div>
                    </div>

                    @if ($permohonan->status == 'revisi-laporan')
                        <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                            <label class="form-label max-w-56">
                                Catatan Revisi
                            </label>
                            <div class="flex flex-wrap items-baseline w-full">
                                <p class="flex w-full text-sm font-medium text-gray-600">
                                    {{ $permohonan->keterangan ?? '' }}
                                </p>
                            </div>
                        </div>
                    @endif

                </div>
                @php

                    $jaminanId = $dokumen->id;
                    $currentInspeksi = $inspeksiData[$jaminanId] ?? null;
                    $tanahBangunanTypes = ['KAPAL', 'PESAWAT', 'ALAT BERAT'];
                @endphp

                <div class="flex flex-wrap gap-2.5 items-baseline mt-2 w-full lg:flex-nowrap card-footer">
                    <div class="flex gap-5">
                        <a class="btn btn-primary"
                            href="{{ route('penilai.lampiran') }}?permohonanId={{ $permohonan->id }}&documentId={{ $dokumen->id }}&jaminanId={{ $dokumen->jenis_jaminan_id }}&header={{ request('header') }}">
                            Lampiran Foto dan Dokumen
                        </a>
                        @if ($permohonan->status != 'freeze')
                            @if (strtolower($permohonan->tujuanPenilaian->name) != 'rap')
                                <a class="btn btn-primary" data-modal-toggle="#modal_2">
                                    Kertas Kerja
                                </a>
                            @endif
                            <a class="btn btn-primary"
                                onclick="paparan({{ $permohonan->id }}, {{ $dokumen->id }}, {{ $dokumen->jenis_jaminan_id }})">
                                Paparan
                            </a>

                            <div class="dropdown" data-dropdown="true" data-dropdown-trigger="click">
                                <button class="dropdown-toggle btn btn-primary">
                                    Pembuatan Laporan
                                </button>

                                {{-- kios tanah bangunan kalo dalam mall ruko --}}
                                <div class="py-2 w-full dropdown-content max-w-56">
                                    <div class="flex flex-col w-full menu menu-default">
                                        @if (strtolower($permohonan->tujuanPenilaian->name) == 'rap')
                                            <div class="menu-item">
                                                <a class="menu-link"
                                                    onclick="rap('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }})">
                                                    <span class="menu-icon">
                                                        <i class="ki-outline ki-message-programming">
                                                        </i>
                                                    </span>
                                                    <span class="menu-title">
                                                        RAP
                                                    </span>
                                                </a>
                                            </div>
                                        @else
                                            {{-- pendapingan kjjp leleang --}}
                                            {{-- memo pendapingan lelang --}}
                                            {{-- existing di  --}}
                                            <div class="menu-item">
                                                <a class="menu-link"
                                                    onclick="memo('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }})">
                                                    <span class="menu-icon">
                                                        <i class="ki-outline ki-badge">
                                                        </i>
                                                    </span>
                                                    <span class="menu-title">
                                                        MEMO
                                                    </span>
                                                </a>
                                            </div>
                                            @if (!in_array(strtoupper($dokumen->jenisJaminan->name), $tanahBangunanTypes))
                                                @if ($permohonan->status_bayar === 'belum_bayar')
                                                    <div class="menu-item">
                                                        <a class="menu-link"
                                                            onclick="resume('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }})">
                                                            <span class="menu-icon">
                                                                <i class="ki-outline ki-profile-circle">
                                                                </i>
                                                            </span>
                                                            <span class="menu-title">
                                                                RESUME
                                                            </span>
                                                        </a>
                                                    </div>
                                                @endif

                                                {{-- untuk laporan standart itu non kerjasama tapi sederhanan in kerjasama --}}

                                                <div class="menu-item">
                                                    <a class="menu-link"
                                                        onclick="seletSederhanaStandart('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }}, '{{ $permohonan->jenisFasilitasKredit->name }}', '{{ $permohonan->status_bayar }}')">
                                                        <span class="menu-icon">
                                                            <i class="ki-outline ki-setting-2"></i>
                                                        </span>
                                                        <span class="menu-title">LPJ</span>
                                                    </a>
                                                </div>
                                            @endif
                                            <div class="menu-item">
                                                <a class="menu-link"
                                                    onclick="callReport('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }})">
                                                    <span class="menu-icon">
                                                        <i class="ki-outline ki-profile-circle">
                                                        </i>
                                                    </span>
                                                    <span class="menu-title">
                                                        CALL REPORT
                                                    </span>
                                                </a>
                                            </div>
                                        @endif

                                        {{-- @if ($permohonan->penilaian->jenis_penilaian_id == 2 && $permohonan->tujuanPenilaian->id == 4) --}}

                                        {{-- @endif --}}

                                    </div>
                                </div>
                            </div>

                            @if ($permohonan->jenisPenilaian->name == 'Eksternal')
                                @if (isset($permohonan->laporanExternal->file_resume) || isset($permohonan->laporanExternal->file_laporan))
                                    <div class="dropdown" data-dropdown="true" data-dropdown-trigger="click">
                                        <button class="dropdown-toggle btn btn-primary">
                                            Laporan Eksternal
                                        </button>
                                        <div class="dropdown-content w-full max-w-56 py-2 !ml-[200px] !mt-[-20px]">
                                            <div class="flex flex-col w-full menu menu-default">
                                                @if (isset($permohonan->laporanExternal->file_resume) && !empty($permohonan->laporanExternal->file_resume))
                                                    <div class="menu-item">
                                                        <a class="menu-link"
                                                            onclick="viewPDF('{{ Storage::url($permohonan->laporanExternal->file_resume) }}')">
                                                            <span class="menu-icon">
                                                                <i class="ki-outline ki-badge"></i>
                                                            </span>
                                                            <span class="menu-title">
                                                                Resume
                                                            </span>
                                                        </a>
                                                    </div>
                                                @endif
                                                @if (isset($permohonan->laporanExternal->file_laporan) && !empty($permohonan->laporanExternal->file_laporan))
                                                    <div class="menu-item">
                                                        <a class="menu-link"
                                                            onclick="viewPDF('{{ Storage::url($permohonan->laporanExternal->file_laporan) }}')">
                                                            <span class="menu-icon">
                                                                <i class="ki-outline ki-setting-2"></i>
                                                            </span>
                                                            <span class="menu-title">
                                                                Laporan
                                                            </span>
                                                        </a>
                                                    </div>
                                                @endif
                                            </div>
                                        </div>
                                    </div>
                                @endif
                            @endif
                        @endif
                    </div>

                    <div class="flex gap-5 justify-end">

                        <a href="{{ route('surveyor.print_out_inspeksi', ['permohonan_id' => $permohonan->id, 'dokument_id' => $dokumen->id, 'jenis_jaminan_id' => $dokumen->jenis_jaminan_id]) }}"
                            class="btn btn-light">
                            <i class="ki-filled ki-printer"></i> Cetak Hasil Inspeksi
                        </a>

                        <a class="btn btn-outline btn-info"
                            onclick="checkLaporan('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }}, 1 )">
                            <i class="ki-filled ki-eye"></i>
                            Lihat Laporan
                        </a>

                        <a class="btn btn-success"
                            onclick="checkLaporan('{{ $permohonan->id }}', '{{ $dokumen->id }}', {{ $dokumen->jenis_jaminan_id }}, 0 )">
                            <i class="ki-filled ki-printer"></i>
                            Cetak Laporan
                        </a>
                    </div>
                </div>
            </div>
        @endforeach
        @if ($permohonan->status != 'freeze')
            <div class="flex gap-5 justify-start">
                <a class="btn btn-success" onclick="savePenilai()">
                    <i class="ki-filled ki-paper-plane"></i>
                    REPORT
                </a>

                <a class="btn btn-warning"
                    {{ $permohonan->status == 'proses-paparan' || $permohonan->status == 'proses-laporan' ? 'disabled' : '' }}
                    onclick="revisiSurveyor('{{ $permohonan->id }}', '{{ $permohonan->debiture->name }}', '{{ $permohonan->nomor_registrasi }}')">
                    <i class="ki-filled ki-arrow-circle-right"></i>
                    REVISI
                </a>
            </div>
        @endif


    </div>




    <div class="modal" data-modal="true" id="modal_2">
        <div class="modal-content modal-center-y max-w-[600px]">
            <div class="modal-header">
                <h3 class="modal-title">
                    Kertas Kerja
                </h3>
                <button class="btn btn-xs btn-icon btn-light" data-modal-dismiss="true">
                    <i class="ki-outline ki-cross">
                    </i>
                </button>
            </div>
            <div class="grid gap-5 modal-body">
                @php
                    $jenisJaminanId = null;

                    $documentId = null;
                    $firstDocument = $permohonan->documents->first(); // Ambil dokumen pertama

                    if ($firstDocument) {
                        $documentId = $firstDocument->id;

                        // Ambil ID inspeksi pertama (jika ada)

                        // Ambil jenis jaminan jika ada
                        if ($firstDocument->jenisjaminan) {
                            $jenisJaminanId = $firstDocument->jenisjaminan->id;
                        }
                    } else {
                        $documentId = 'Tidak ada dokumen';

                        $jenisJaminanId = 'Tidak ada jenis jaminan';
                    }

                @endphp

                <div class="flex items-baseline w-full">
                    <label for="" class="form-label max-w-56">Import Kertas Kerja</label>
                    <div class="flex gap-2 justify-center items-center">
                        <input type="file" id="kertas_kerja" class="file-input">
                        <button type="button" class="btn btn-outline btn-success"
                            onclick="uploadKertasKerja({{ $permohonan->id }}, '{{ $documentId }}', '{{ $jenisJaminanId }}')">Upload
                        </button>
                    </div>
                </div>
                <div class="flex justify-between items-center">
                    @if (isset($permohonan->penilai) && isset($permohonan->penilai->kertas_kerja) && $permohonan->penilai->kertas_kerja)
                        <span data-modal-dismiss="true" class="btn btn-warning btn-outline"
                            onclick="viewPDF('{{ Storage::url($permohonan->penilai->kertas_kerja) }}')"><i
                                class="mr-2 ki-filled ki-eye"></i>Lihat Kertas Kerja</span>
                    @endif
                </div>

            </div>
            <div class="modal-footer">

            </div>
        </div>
    </div>
@endsection

@include('lpj::component.pdfviewer')

<script>
    function seletSederhanaStandart(permohonanId, documentId, jaminanId, fasilitasKredit, statusBayar) {
        if (statusBayar === 'belum_bayar') {
            Swal.fire({
                title: 'Jenis LPJ Ditentukan',
                text: 'Satus Pembayaran belum bayar. LPJ akan diarahkan ke resum.',
                icon: 'info',
                confirmButtonText: 'Lanjutkan',
                confirmButtonColor: '#3085d6',
            }).then(() => {
                resume(permohonanId, documentId, jaminanId)
            });

        } else {
            fetch(
                    `{{ url('/penilai/check-status-lpj') }}?permohonanId=${permohonanId}&documentId=${documentId}`
                )
                .then(response => response.json())
                .then(data => {
                    if (data.status) {
                        // Jika status LPJ sudah ada, arahkan langsung ke halaman
                        if (data.status === 'standar') {
                            showLoadingSwal('Tunggu ...');
                            window.location.href =
                                `{{ route('penilai.standar', $permohonan->id) }}?documentId=${documentId}&jaminanId=${jaminanId}`;
                        } else if (data.status === 'sederhana') {
                            showLoadingSwal('Tunggu ...');
                            window.location.href =
                                `{{ route('penilai.sederhana', $permohonan->id) }}?documentId=${documentId}&jaminanId=${jaminanId}`;
                        }
                    } else {
                        if (fasilitasKredit.toUpperCase() === 'KPR KERJASAMA') {
                            Swal.fire({
                                title: 'Jenis LPJ Ditentukan',
                                text: 'Jenis fasilitas kredit adalah KPR Kerjasama. LPJ akan diarahkan ke Sederhana.',
                                icon: 'info',
                                confirmButtonText: 'Lanjutkan',
                                confirmButtonColor: '#3085d6',
                            }).then(() => {
                                saveStatusLpj(permohonanId, documentId, 'sederhana', jaminanId);
                            });
                        } else {
                            Swal.fire({
                                title: 'Pilih Jenis LPJ',
                                text: 'Apakah kamu akan memilih LPJ Standard?',
                                icon: 'question',
                                showCloseButton: true,
                                showDenyButton: true,
                                confirmButtonText: 'LPJ Standard',
                                denyButtonText: 'LPJ Sederhana',
                                confirmButtonColor: '#3085d6',
                                denyButtonColor: '#d33',
                            }).then((result) => {
                                if (result.isConfirmed) {
                                    saveStatusLpj(permohonanId, documentId, 'standar',
                                        jaminanId);
                                } else if (result.isDenied) {
                                    saveStatusLpj(permohonanId, documentId, 'sederhana',
                                        jaminanId);
                                }
                            });
                        }
                    }
                    console.log(data);

                });
        }

    }

    function saveStatusLpj(permohonanId, documentId, type, jaminanId) {
        fetch(`{{ url('/penilai/save-status-lpj') }}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                },
                body: JSON.stringify({
                    permohonan_id: permohonanId,
                    document_id: documentId,
                    type: type,
                }),
            }).then(response => response.json())
            .then(data => {
                if (data.success) {
                    if (type === 'standar') {
                        window.location.href =
                            `{{ route('penilai.standar', $permohonan->id) }}?documentId=${documentId}&jaminanId=${jaminanId}`;
                    } else if (type === 'sederhana') {
                        window.location.href =
                            `{{ route('penilai.sederhana', $permohonan->id) }}?documentId=${documentId}&jaminanId=${jaminanId}`;
                    }
                }
            });
    }


    function uploadKertasKerja(permohonanId, documentId, jaminanId) {
        const kertasKerjaInput = document.getElementById('kertas_kerja');
        if (!kertasKerjaInput.files.length) {
            Swal.fire({
                title: 'Error!',
                text: 'Silakan pilih file sebelum mengupload.',
                icon: 'error',
                confirmButtonText: 'OK'
            });
            return;
        }

        const kertasKerja = kertasKerjaInput.files[0];
        const formData = new FormData();
        formData.append('permohonan_id', permohonanId);
        formData.append('document_id', documentId);
        formData.append('jenis_jaminan_id', jaminanId);
        formData.append('kertas_kerja', kertasKerja);

        fetch(`{{ url('/penilai/import/kertas-kerja') }}`, {
                method: 'POST',
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}',
                },
                body: formData,
            })
            .then(response => response.json()) // Pastikan respons diurai menjadi JSON
            .then(data => {
                if (data.success) {
                    Swal.fire({
                        title: 'Berhasil!',
                        text: data.message || 'Kertas kerja berhasil diunggah.',
                        icon: 'success',
                        confirmButtonText: 'OK'
                    }).then(result => {
                        if (result.isConfirmed) {
                            window.location.reload();
                        }
                    });
                } else {
                    Swal.fire({
                        title: 'Error!',
                        text: data.message || 'Terjadi kesalahan.',
                        icon: 'error',
                        confirmButtonText: 'OK'
                    });
                }
            })
            .catch(error => {
                Swal.fire({
                    title: 'Error!',
                    text: 'Terjadi kesalahan saat mengunggah file.',
                    icon: 'error',
                    confirmButtonText: 'OK'
                });
                console.error('Error:', error); // Debug error di konsol
            });
    }

    function resume(permohonanId, documentId, jaminanId) {
        showLoadingSwal('Tunggu ...');
        window.location.href =
            `{{ route('penilai.resume') }}?permohonanId=${permohonanId}&documentId=${documentId}&jaminanId=${jaminanId}`;
    }

    function memo(permohonanId, documentId, jaminanId) {
        showLoadingSwal('Tunggu ...');
        window.location.href =
            `{{ route('penilai.memo') }}?permohonanId=${permohonanId}&documentId=${documentId}&jaminanId=${jaminanId}`;
    }

    function rap(permohonanId, documentId, jaminanId) {
        showLoadingSwal('Tunggu ...');
        window.location.href =
            `{{ route('penilai.rap') }}?permohonanId=${permohonanId}&documentId=${documentId}&jaminanId=${jaminanId}`;
    }


    function paparan(permohonanId, documentId, jaminanId) {
        Swal.fire({
            title: 'Apakah Kamu yakin ingin melakukan paparan',
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes'
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href =
                    `/penilai/${permohonanId}/edit?document_id=${documentId}&jaminanId=${jaminanId}`;
            }
        });
    }

    function callReport(permohonanId, documentId, jaminanId) {
        showLoadingSwal('Tunggu ...');
        window.location.href =
            `{{ route('penilai.call-report') }}?permohonanId=${permohonanId}&documentId=${documentId}&jaminanId=${jaminanId}`;
    }


    function savePenilai() {
        showLoadingSwal('Mengirim data keserver ...');
        $.ajax({
            url: "{{ route('penilai.proses.laporan', ['id' => $permohonan->id]) }}",
            type: "POST",
            headers: {
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            },
            success: function(response) {
                if (response.success) {
                    // window.location.reload();
                    hideLoadingSwal();
                    toastrSuccessBuild(response.message);
                } else {
                    // hideLoadingSwal();
                    Swal.fire('Perhatian!', response.message, 'warning');
                }
            },
            error: function(xhr, status, error) {
                console.log('Error checking button status:', error, status, xhr);
                if (xhr.responseJSON.message) {
                    // window.location.reload();
                    toastrErrorBuild(xhr.responseJSON.message);
                } else {
                    // window.location.reload();
                    toastrErrorBuild('Terjadi kesalahan');
                }
                hideLoadingSwal();

            }
        });

    }

    function revisiSurveyor(dataId, debitur, noreg) {

        Swal.fire({
            title: 'Apakah Anda yakin?',
            text: `Untuk melakukan Revisi nomor registrasi ${noreg}  atas nama debiture ${debitur} !`,
            icon: 'warning',
            input: 'textarea',
            inputLabel: 'Keterangan',
            inputPlaceholder: 'Masukkan keterangan...',
            inputAttributes: {
                'aria-label': 'Masukkan keterangan'
            },
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Ya, Lanjutkan!',
            cancelButtonText: 'Batal',
        }).then((result) => {
            if (result.isConfirmed) {
                const userMessage = result.value || '';
                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}'
                    },
                });
                $.ajax({
                    url: `/penilai/revisi-surveyor/${dataId}`,
                    type: 'PUT',
                    data: {
                        keterangan: userMessage
                    },
                    success: (response) => {
                        Swal.fire('Berhasil!', response.message, 'success').then(
                            () => {
                                window.location.href =
                                    '{{ route('penilai.index') }}';
                            });
                        console.log(response);
                    },
                    error: (error) => {
                        console.error('Error:', error);
                        Swal.fire('Gagal!', 'Terjadi kesalahan saat melakukan Revisi.',
                            'error');
                    }
                });
            }
        });
    }
</script>

@include('lpj::surveyor.js.utils')
