@include('lpj::penilai.components.header')
@php
    $data = '';
    foreach ($permohonan->documents as $dokumen) {
        $data .= $dokumen->jenisJaminan->name . ', ';
        $jenisAset = $dokumen->jenisJaminan->name;
    }
    $data = rtrim($data, ', ');

    $permohonan_migrasi = json_decode($permohonan->mig_permohonan);

@endphp

<main class="content">
    <div style="text-align: center; margin-top: 5px;">
        <h4 style="text-transform: uppercase; font-size: 16px; margin: 0;">LAPORAN PENILAIAN ATAS
            {{ $data }}
        </h4>
        <p style="font-size: 12px; margin: 5px 0;">NO: {{ $nomorLaporan }}</p>
    </div>
    <hr />
    <table style="width: 100%">
        <tr>
            <td style="width: 50%; vertical-align: top;">
                <table style="width: 100%; border-collapse: collapse;">
                    <tr>
                        <td style=" padding: 2px; vertical-align: top;">Nama Debitur</td>
                        <td style=" padding: 2px; vertical-align: top;">:</td>
                        <td style=" padding: 2px; vertical-align: top;">{{ $permohonan->debiture->name ?? '' }}</td>
                    </tr>
                    <tr>
                        <td>Alamat dan Telpon</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">
                            {{ $permohonan->debiture->address ?? '' }} {{ $permohonan->debiture->phone ?? '' }}
                        </td>
                    </tr>

                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Permintaan Cabang</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $permohonan->debiture->branch->name ?? '' }}
                        </td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Permintaan (AO)</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $permohonan->user->name ?? $permohonan_migrasi->mig_mst_jaminan_nama_ao ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">No Order</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $permohonan->nomor_registrasi ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Tanggal Order</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">
                            {{ formatTanggalIndonesia($permohonan->tanggal_permohonan) }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Tanggal Survey</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">
                            {{ formatTanggalIndonesia($permohonan->penilaian->tanggal_kunjungan) }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">User</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $permohonan->user->name ?? '' }}</td>
                    </tr>


                    @php
                        $senior_officer = null;
                        if ($permohonan->debiture && $permohonan->documents) {
                            foreach ($permohonan->documents as $dokumen) {
                                $penilai = $permohonan->penilaian->userPenilai->where('role', 'penilai')->first();
                                $surveyor = $permohonan->penilaian->userPenilai->where('role', 'surveyor')->first();
                                $teams = $permohonan->region->teams;

                                if ($teams) {
                                    foreach ($teams as $team) {
                                        $team_users = $team->teamsUsers;
                                        // print_r($team_users);
                                        if ($team_users) {
                                            foreach ($team_users as $team_user) {
                                                $user = $team_user->user;

                                                if ($user && $user->hasRole('senior-officer')) {
                                                    $senior_officer = $user;
                                                    break 3;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    @endphp
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">User</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $penilai->user->name ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Pemeriksa</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $senior_officer->name ?? $permohonan->approveSo->name ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Menyetujui</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $senior_officer->name ?? $permohonan->approveSo->name ?? '' }}</td>
                    </tr>
                     @if($permohonan->is_mig)
                     <tr>
                        <td style="padding: 2px; vertical-align: top;">No Laporan</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">
                           {{  $permohonan_migrasi->mig_mst_lpj_nomor_lpj ?? '-' }}
                        </td>
                    </tr>
                    @endif
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Tanggal Laporan</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">
                            {{ formatTanggalIndonesia($tanggalLaporan) ?? '' }}
                        </td>
                    </tr>
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">Waktu Penyelesaian</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td>
                            @if($permohonan->is_mig)
                            {{ hitungHariKerja($permohonan->penilaian->tanggal_kunjungan, \Carbon\Carbon::createFromFormat('d/m/Y H:i:s', $permohonan_migrasi->mig_mst_jaminan_tgl_oto)->format('Y-m-d H:i:s'))-1 }}
                            @else
                            {{ hitungHariKerja($permohonan->penilaian->tanggal_kunjungan, $tanggalLaporan) }}
                            @endif
                            </td>
                    </tr>
                </table>
            </td>
            <td style="width: 50%; vertical-align: top">
                <table style="width: 100%; border-collapse: collapse;">
                    <tr>
                        <td style=" padding: 2px; vertical-align: top;">Fasilitas Kredit</td>
                        <td style="  padding: 2px; vertical-align: top;">:</td>
                        <td style="  padding: 2px; vertical-align: top;">
                            {{ $permohonan->jenisFasilitasKredit->name ?? '' }}</td>
                    </tr>
                    <tr>

                        <td style=" padding: 2px; vertical-align: top;">Jenis Jaminan</td>
                        <td style=" padding: 2px; vertical-align: top;">:</td>
                        <td style=" padding: 2px; vertical-align: top;">{{ $data ?? '' }}</td>
                    </tr>
                    <tr>
                        <td style=" padding: 2px; vertical-align: top;">Tujuan Penilaian</td>
                        <td style=" padding: 2px; vertical-align: top;">:</td>
                        <td style=" padding: 2px; vertical-align: top;">{{ $permohonan->tujuanPenilaian->name ?? '' }}
                        </td>
                    </tr>

                    @if ($permohonan->is_mig)
                        @php
                            $data = json_decode($inspeksi->mig_detail_data_jaminan, true);
                        @endphp
                        @foreach ($data as $key => $item)
                            @if ($key == 'address')
                                @php $key = 'Terletak di'; @endphp
                            @endif
                            @php
                                // Remove suffix _2, _3, _4, _5 from key
                                $key = preg_replace('/_([2-5])$/', '', $key);
                            @endphp
                            <tr>
                                <td style=" padding: 2px; vertical-align: top;">{{ $key }}</td>
                                <td style=" padding: 2px; vertical-align: top;">:</td>
                                <td style=" padding: 2px; vertical-align: top;">{{ $item }}</td>
                            </tr>
                        @endforeach

                    @else
                        @if (@isset($dokumen))
                            @foreach ($dokumen->detail as $detail)
                                @if (!empty($detail->name) && isset($detail->details) && !empty($detail->dokumen_jaminan))
                                    <tr>
                                        <td width="25%">{{ $detail->name ?? '' }}</td>
                                        <td style=" padding: 2px; vertical-align: top;">:</td>
                                        <td style=" padding: 2px; vertical-align: top;">{{ json_decode($detail->dokumen_nomor, true)[0] ?? '' }}</td>
                                    </tr>
                                @endif

                                @if (isset($detail->details) && $detail->name == 'LOKASI JAMINAN')
                                    @php
                                        $details = json_decode($detail->details, true);
                                    @endphp
                                    @include('lpj::component.detail-lokasi', ['details' => $details])
                                @endif
                            @endforeach
                        @endif

                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Terletak di</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $alamat['address'] ?? '' }}</td>
                        </tr>
                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Desa/Kelurahan</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $alamat['village_code'] ?? '' }}</td>
                        </tr>
                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Kecamatan</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $alamat['district_code'] ?? '' }}</td>
                        </tr>
                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Kabupaten/Kota</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $alamat['city_code'] ?? '' }}</td>
                        </tr>
                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Provinsi</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $alamat['province_code'] ?? '' }}</td>
                        </tr>


                        @include('lpj::component.print-out-dokument')

                        @if (isset($forminspeksi['asset']['nomor_nib']))
                            <tr>
                                <td style=" padding: 2px; vertical-align: top;">:</td>
                                <td style=" padding: 2px; vertical-align: top;">
                                    {{ $forminspeksi['asset']['nomor_nib'] ?? '' }}</td>
                            </tr>
                        @endif
                        <tr>
                            <td style=" padding: 2px; vertical-align: top;">Atas Nama</td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px;">{{ $permohonan->debiture->name ?? '' }}</td>
                        </tr>
                    @endif

                </table>
            </td>
        </tr>
    </table>

    <div class="no-break">
        @php $mig_detail_legalitas_jaminan = json_decode($inspeksi->mig_detail_legalitas_jaminan, true); @endphp
        <table width="100%">
            <tr>
                <td style="width: 50%; border: 1px solid #000;">
                    STATUS KEPEMILIKAN, HUBUNGAN DAN PENGHUNI
                </td>

                <td style="width: 50%; border: 1px solid #000;">
                    SARANA PELENGKAP DAN LINGKUNGAN --
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">
                    <table style="vertical-align: top;">
                        <tr>
                            @php
                                $cekHubDebitur =
                                    isset($forminspeksi['asset']['hub_cadeb']) &&
                                    $forminspeksi['asset']['hub_cadeb'] === 'sesuai'
                                        ? 'sesuai'
                                        : 'tidak sesuai';

                                $hubCadebDebitur = isset($forminspeksi['asset']['hub_cadeb'][$cekHubDebitur])
                                    ? $forminspeksi['asset']['hub_cadeb']['tidak sesuai']
                                    : (isset($forminspeksi['asset']['hub_cadeb']['sesuai']) ? $forminspeksi['asset']['hub_cadeb']['sesuai'] : '');

                            @endphp
                            <td style=" width:49%; padding: 2px; vertical-align: top;">Hubungan Pemilik Jaminan dengan
                                Debitur</td>
                            <td style=" width:1%; padding: 2px; vertical-align: top;">:</td>
                            <td style=" width:50%; padding: 2px; vertical-align: top;">{{ $cekHubDebitur =='tidak sesuai' ? 'Lain-lain, ' : '' }} {{ $hubCadebDebitur ?? '' }}
                            </td>
                        </tr>
                        <tr>
                            @php

                                $cekHub =
                                    isset($forminspeksi['asset']['hub_cadeb_penghuni']) &&
                                    $forminspeksi['asset']['hub_cadeb_penghuni'] === 'sesuai'
                                        ? 'sesuai'
                                        : 'tidak sesuai';

                                $hubCadeb = isset($forminspeksi['asset']['hub_cadeb_penghuni'][$cekHub])
                                    ? $forminspeksi['asset']['hub_cadeb_penghuni']['tidak sesuai']
                                    : (isset($forminspeksi['asset']['hub_cadeb_penghuni']['sesuai']) ? $forminspeksi['asset']['hub_cadeb_penghuni']['sesuai'] : '');
                            @endphp
                            <td style=" padding: 2px; vertical-align: top;">Hubungan Penghuni Jaminan dengan Debitur
                            </td>
                            <td style=" padding: 2px; vertical-align: top;">:</td>
                            <td style=" padding: 2px; vertical-align: top;">{{ $hubCadeb ?? '' }}</td>
                        </tr>
                    </table>
                </td>

                <td style="width: 50%; vertical-align: top;">
                    @if ($permohonan->is_mig)
                    @if(isset($mig_detail_legalitas_jaminan['SARANA PELENGKAP DAN LINGKUNGAN']))
                        <table>
                                @foreach ($mig_detail_legalitas_jaminan['SARANA PELENGKAP DAN LINGKUNGAN'] as $item)
                                    <tr>
                                        <td style="padding: 2px; vertical-align: top;">{{ $item['label'] }}</td>
                                        <td style="padding: 2px; vertical-align: top;">:</td>
                                        <td style="padding: 2px; vertical-align: top;">{{ ltrim($item['value'], ',') }}</td>
                                    </tr>
                                @endforeach

                        </table>
                        @endif
                    @else
                        <table>
                            @php
                                $lingkungan = $forminspeksi['lingkungan'];
                                $fasilitas = $lingkungan['fasilitas_dekat_object'] ?? [];
                                $fasilitasInput = $lingkungan['fasilitas_dekat_object_input'] ?? [];
                            @endphp

                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Jarak Jalan Utama</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['jarak_jalan_utama'] ?? '-' }} m
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Jalan Lingkungan</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['jalan_linkungan'] ?? '-' }}
                                    m
                                </td>

                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Jarak CBD</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['jarak_cbd_point'] ?? '-' }}
                                    m
                                    ({{ $lingkungan['nama_cbd_point'] ?? '-' }})</td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Lebar Perkerasan Jalan</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['lebar_perkerasan_jalan'] ?? '-' }} m</td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Perkerasan Jalan</td>
                                <td style="padding: 2px; vertical-align: top;">:
                                </td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ implode(', ', $lingkungan['perkerasan_jalan']['perkerasan_jalan'] ?? ($lingkungan['perkerasan_jalan'] ?? ['-'])) }}
                                </td>
                            </tr>


                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Lalu Lintas</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['lalu_lintas']
                                        ? (is_array($lingkungan['lalu_lintas'])
                                            ? implode(', ', $lingkungan['lalu_lintas'])
                                            : $lingkungan['lalu_lintas'])
                                        : '-' }}
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Golongan Masyarakat Sekitar</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ implode(', ', $lingkungan['gol_mas_sekitar'] ?? ($lingkungan['gol_mas_sekitar'] ?? ['-'])) }}
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Tingkat Keramaian</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ implode(', ', $lingkungan['tingkat_keramaian']['tingkat_keramaian'] ?? ($lingkungan['tingkat_keramaian'] ?? ['-'])) }}
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Terletak di Area</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ implode(', ', $lingkungan['terletak_diarea']['terletak_diarea'] ?? ($lingkungan['terletak_diarea'] ?? ['-'])) }}
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Kondisi Bangunan Sekitar</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['kondisi_bagunan_disekitar_lokasi'] ?? '-' }}
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Sifat Bangunan Sekitar</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ $lingkungan['sifat_bagunan_disekitar_lokasi'] ?? '-' }}</td>
                            </tr>

                            @if ($lingkungan['dekat_makam'] == 'ya')
                                <tr>
                                    <td style="padding: 2px; vertical-align: top;">Dekat Makam</td>
                                    <td style="padding: 2px; vertical-align: top;">:</td>
                                    <td style="padding: 2px; vertical-align: top;">Ya, Jarak
                                        {{ $lingkungan['jarak_makam'] ?? '-' }} m
                                        ({{ $lingkungan['nama_makam'] ?? '-' }})
                                    </td>
                                </tr>
                            @endif

                            @if ($lingkungan['dekat_tps'] == 'ya')
                                <tr>
                                    <td style="padding: 2px; vertical-align: top;">Dekat TPS</td>
                                    <td style="padding: 2px; vertical-align: top;">:</td>
                                    <td style="padding: 2px; vertical-align: top;">Ya, Jarak
                                        {{ $lingkungan['jarak_tps'] ?? '-' }} m
                                        ({{ $lingkungan['nama_tps'] ?? '-' }})</td>
                                </tr>
                            @endif

                            <tr>
                                <td style="padding: 2px; vertical-align: top;">Daerah</td>
                                <td style="padding: 2px; vertical-align: top;">:</td>
                                <td style="padding: 2px; vertical-align: top;">
                                    {{ implode(', ', $lingkungan['merupakan_daerah'] ?? ['-']) }}
                                </td>
                            </tr>

                            @if (!empty($fasilitas))
                                <tr>
                                    <td style="padding: 2px; vertical-align: top;">Fasilitas Dekat Objek</td>
                                    <td style="padding: 2px; vertical-align: top;">:</td>
                                    <td style="padding: 2px; vertical-align: top;">
                                        @foreach ($fasilitas as $fas)
                                            {{ $fas }}
                                            @if (isset($fasilitasInput[$fas]))
                                                ({{ $fasilitasInput[$fas] }})
                                            @endif
                                            @if (!$loop->last)
                                                ,
                                            @endif
                                        @endforeach
                                    </td>
                                </tr>
                            @endif
                        </table>
                    @endif
                </td>
            </tr>
        </table>
    </div>

    @if ($permohonan->is_mig)
        @if(isset($mig_detail_legalitas_jaminan['ANALISA TANAH DAN BANGUNAN']))
        <table width="100%">
            <tr>
                <td colspan="3" style="border: 1px solid #000;">
                    ANALISA TANAH DAN BANGUNAN
                </td>
            </tr>
                @foreach ($mig_detail_legalitas_jaminan['ANALISA TANAH DAN BANGUNAN'] as $item)
                    <tr>
                        <td style="padding: 2px; vertical-align: top;">{{ $item['label'] }}</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ ltrim($item['value'], ',') }}</td>
                    </tr>
                @endforeach
        </table>
         @endif
    @endif
        @foreach ($permohonan->documents as $dokumen)
            @if ($dokumen->jenisJaminan)
                @php
                    $formKategori = json_decode($dokumen->jenisJaminan->form_kategori, true);
                @endphp
                @if (isset($formKategori) && $formKategori)
                    @php
                        $kategoriArray = is_array($formKategori) ? $formKategori : [$formKategori];
                        $kategoriUnik = array_unique($kategoriArray);
                    @endphp

                    @if(!$permohonan->is_mig)
                        @foreach ($kategoriUnik as $kategori)
                            @include(
                                'lpj::penilai.components.analisa.' . str_replace('-', '-', $kategori),
                                    [
                                        'dokumen' => $dokumen,
                                    ]
                                )
                        @endforeach
                    @endif
                @endif
            @endif
        @endforeach

    <div class="no-break">
        <table style="width: 100%">
            <tr>
                <td style="  border: 1px solid #000; text-align: center;">
                    INFORMASI HARGA
                </td>
            </tr>
        </table>
        @if(isset($dataPembanding))
            @include('lpj::penilai.components.print-pembanding')
        @endif
    </div>
    <div class="no-break">
        <table style="width: 100%">
            <tr>
                <td style="border: 1px solid #000; text-align: center;"> INFORMASI NILAI
                </td>
            </tr>
        </table>
        <table style="width: 100%;">
            <tr>
                <td style="padding: 8px; width: 100%;">
                    <table style="width: 100%; border-collapse: collapse;">
                        <tr>
                            <td colspan="6" style="padding: 8px; text-align: left;">
                                <strong>a. Total Nilai Pasar Wajar</strong>
                            </td>
                        </tr>
                        @php
                            $totalNilaiPasarWajar = 0;
                            $labelNilai = [
                                'bangunan' => 'Luas Bangunan',
                                'tanah' => 'Luas Tanah',
                                'apartemen-kantor' => 'Luas Unit',
                                'alat-berat' => 'Luas Alat Berat',
                                'mesin' => 'Luas Mesin',
                                'kendaraan' => 'Luas Kendaraan',
                                'pesawat' => 'Luas Pesawat',
                                'kapal' => 'Luas Kapal',
                            ];
                            if (strcasecmp($jenisAset, 'RUKO/RUKAN') === 0) {
                                $labelNilai['bangunan'] = 'Luas Unit';
                                unset($labelNilai['tanah']);
                            }
                        @endphp

                        {{-- Fallback untuk data tanpa npw_tambahan --}}
                        @if(!$permohonan->is_mig)
                            @foreach ($labelNilai as $key => $label)
                                @if (isset($lpjData['luas_' . $key]))
                                    <tr>
                                        <td width="30%" style="padding: 3px; text-align: right;">{{ $label }}</td>
                                        <td width="10%" style="padding: 3px; text-align: right;">{{ $lpjData['luas_' . $key] }} m<sup>2</sup></td>
                                        <td width="5%" style="padding: 3px; text-align: center;">X</td>
                                        <td width="25%" style="padding: 3px; text-align:right">{{ formatRupiah($lpjData['nilai_' . $key . '_1'] ?? 0, 0, false) }}</td>
                                        <td width="5%" style="padding: 3px; text-align: center;">=</td>
                                        <td width="25%" style="padding: 3px; text-align: right;">{{ formatRupiah($lpjData['nilai_' . $key . '_2'] ?? 0, 0, false) }}</td>
                                    </tr>
                                    @php $totalNilaiPasarWajar += str_replace(['Rp', '.'], '', $lpjData['nilai_' . $key . '_2']); @endphp
                                @endif
                            @endforeach
                        @endif
                        @if (isset($lpjData['npw_tambahan']))
                            @foreach ($lpjData['npw_tambahan'] as $npw)
                                <tr>
                                    <td width="30%" style="padding: 3px; text-align: right;">
                                        {{ $npw['name'] }}</sup>
                                    </td>
                                    <td width="10%" style="padding: 3px; text-align: right;">
                                        {{ $npw['luas'] }} m<sup>2</sup>
                                    </td>
                                    <td width="5%" style="padding: 3px; text-align: center;">X</td>
                                    <td width="25%"  style="padding: 3px; text-align:right">
                                        {{ number_format((float) str_replace(['Rp', '.', ','], '', $npw['nilai_1'] ?? 0), 0, ',', '.') }}
                                    </td>
                                    <td width="5"  style="padding: 3px; text-align: center;">=</td>
                                    <td  width="25%"  style="padding: 3px; text-align: left; text-align: right;">
                                        {{ number_format((float) str_replace(['Rp', '.', ','], '', $npw['nilai_2'] ?? 0), 0, ',', '.') }}
                                    </td>
                                </tr>
                                @php $totalNilaiPasarWajar += str_replace(['Rp', '.', ','], '', $npw['nilai_2'] ?? 0); @endphp
                            @endforeach
                        @endif
                        <tr>
                            <td style="padding: 3px; text-align: right; font-weight: bold; width:10%;" colspan="4">Total Nilai Pasar Wajar</td>
                            <td style="padding: 3px; text-align: center; width:5%;font-weight: bold;" >=</td>
                            <td style="padding: 3px; text-align: right; font-weight: bold; width: 40%;">
                                <div style="display: inline-block; border-top: 1px solid black; padding-top: 3px;">
                                    {{ number_format((float) str_replace(['Rp', '.', ','], '', $lpjData['total_nilai_pasar_wajar'] ?? $totalNilaiPasarWajar ?? 0), 0, ',', '.') }}
                                </div>
                            </td>
                        </tr>

                            <tr>
                                <td colspan="6" style="padding: 8px; text-align: left; font-weight: bold;">
                                    b. Total Nilai Likuidasi
                                </td>
                            </tr>
                            <tr>
                                <td style="padding: 3px; text-align: right;" colspan="4">
                                    {{ $lpjData['likuidasi'] ?? '' }}% <span style="padding-left:20px; padding-right: 20px">X</span> Total Nilai Pasar
                                    Wajar
                                </td>
                                <td style="padding: 3px; text-align: center; font-weight: bold;">=</td>
                                @php
                                    $percentStr = str_replace(['%',' '], '', $lpjData['likuidasi'] ?? '0');
                                    $percentVal = (float) str_replace(',', '.', $percentStr);
                                    $totalNpwRaw = $lpjData['total_nilai_pasar_wajar'] ?? ($totalNilaiPasarWajar ?? 0);
                                    $totalNpwVal = (float) str_replace(['Rp', '.', ','], '', $totalNpwRaw);
                                    $likuidasiCalc = ($percentVal / 100) * $totalNpwVal;
                                    $likuidasiFinal = isset($permohonan_migrasi->mig_mst_lpj_tot_nilai_likuidasi) && $permohonan_migrasi->mig_mst_lpj_tot_nilai_likuidasi !== null
                                        ? $permohonan_migrasi->mig_mst_lpj_tot_nilai_likuidasi
                                        : $likuidasiCalc;
                                @endphp
                                <td style="padding: 3px; text-align: right;font-weight: bold;">{{ number_format($likuidasiFinal, 0, ',', '.') }}</td>
                                </td>
                            </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td>
                    <table>
                        <tr>
                            <td width="25%" style="vertical-align: top;">Catatan yang Perlu
                                Diperhatikan</td>
                            <td width="1%" style="vertical-align: top;">:</td>
                            <td style="vertical-align: top;">
                                @php
                                    $keterangan = $lpjData['keterangan_penilai'] ?? '';
                                @endphp

                                @if (!empty($keterangan))
                                    @if (is_array($keterangan))
                                        <div style="margin: 0; padding: 0;">
                                            @foreach ($keterangan as $index => $item)
                                                @php
                                                    $cleanItem = trim($item);
                                                    $isNumbered = preg_match('/^\d+\./', $cleanItem);
                                                    $isBullet = preg_match('/^[-•]/', $cleanItem);
                                                @endphp
                                                <div style="margin-bottom: 8px; line-height: 1.4;">
                                                                                                        @if ($isNumbered || $isBullet)
                                                        {{ $cleanItem }}
                                                    @else
                                                        @if ($index > 0 && !empty($cleanItem))- @endif{{ $cleanItem }}
                                                    @endif
                                                </div>
                                            @endforeach
                                        </div>
                                    @else
                                        @php
                                            // Handle string format - normalize line endings first
                                            $keterangan = str_replace(["\r\n", "\r"], "\n", $keterangan);
                                            $items = [];

                                            if (strpos($keterangan, '"') !== false) {
                                                // Handle quoted strings (array format)
                                                preg_match_all('/"([^"]*)"/', $keterangan, $matches);
                                                $items = $matches[1];
                                            } else {
                                                // Handle line-separated items
                                                $rawItems = explode("\n", $keterangan);
                                                foreach ($rawItems as $item) {
                                                    $cleanItem = trim($item);
                                                    // Skip empty items
                                                    if (!empty($cleanItem)) {
                                                        $items[] = $cleanItem;
                                                    }
                                                }
                                            }
                                        @endphp
                                        <div style="margin: 0; padding: 0;">
                                            @foreach ($items as $index => $item)
                                                @php
                                                    $cleanItem = trim($item);
                                                    if (!empty($cleanItem))
                                                @endphp
                                                <div style="margin-bottom: 8px; line-height: 1.4;">
                                                    @if (preg_match('/^\d+\./', $cleanItem))
                                                        {{ $cleanItem }}
                                                    @elseif (preg_match('/^[-•]/', $cleanItem))
                                                        {{ $cleanItem }}
                                                    @else
                                                        - {{ $cleanItem }}
                                                    @endif
                                                </div>
                                            @endforeach
                                        </div>
                                    @endif
                                @else
                                    <div style="color: #666; font-style: italic;">Tidak ada catatan khusus</div>
                                @endif
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    @if($permohonan->is_mig)
        @include(
            'lpj::penilai.components.analisa.informasi',
            [
                'dokumen' => $dokumen,
            ]
        )
    @endif
    <div class="no-break">
        <table style="width: 100%;">
            <tr>
                <td>
                    <table>
                        <div style="margin-top: 20px;">
                            <label style="font-weight: bold;">DISCLAIMER</label>
                            <div>
                                <ol style="padding-left: 20px; list-style-type: decimal; margin-top: 0;">

                                    <li style="margin-bottom: 5px;">PENILAIAN INI DIBUAT BERDASARKAN ATURAN YANG
                                        BERLAKU DI SUBDIT APPRAISAL</li>
                                    <li style="margin-bottom: 5px;">LAPORAN INI DIBUAT BERDASARKAN DATA FOTOCOPY
                                        DOKUMEN YANG DITERIMA PENILAI DENGAN ASUMSI BAHWA DATA TERSEBUT SESUAI DENGAN
                                        DOKUMEN ASLINYA</li>
                                    <li style="margin-bottom: 5px;">PENILAI TIDAK MELAKUKAN PEMBUKTIAN LEBIH RINCI ATAU
                                        PENGAKUAN TERTULIS DARI PIHAK YANG DITEMUI SAAT PENILAIAN, ATAS INFORMASI YANG
                                        DIBERIKAN SECARA LISAN SEHUBUNGAN DENGAN IDENTITAS DIRI DAN HUBUNGAN DI ANTARA
                                        PIHAK TERKAIT SAAT MELAKUKAN INSPEKSI OBJEK YANG DINILAI</li>
                                    <li style="margin-bottom: 5px;">LAPORAN INI DIGUNAKAN HANYA UNTUK KEPENTINGAN
                                        INTERNAL DAN DILARANG MENYEBARKAN KEPADA PIHAK KETIGA</li>
                                </ol>
                            </div>
                        </div>

                    </table>
                </td>
            </tr>

            <tr>
                <td>Demikian laporan penilai jaminan ini di buat secara objektif, tanpa adanya pengaruh baik intern
                    maupun ekstern</td>
            </tr>
            <tr>
                @include('lpj::penilai.components.signature-approval',['laporan' => 'sederhana','npw' => $totalNilaiPasarWajar])
            </tr>
        </table>
    </div>
</main>
@include('lpj::penilai.components.footer')
