@include('lpj::penilai.components.header')
@php
    $data = '';
    foreach ($permohonan->documents as $dokumen) {
        $data .= $dokumen->jenisJaminan->name . ', ';
        $jenisAset = $dokumen->jenisJaminan->name;
    }
    $data = rtrim($data, ', ');
@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;">{{ u $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 }}</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->updated_at) }}</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;">Pemeriksa</td>
                        <td style="padding: 2px; vertical-align: top;">:</td>
                        <td style="padding: 2px; vertical-align: top;">{{ $senior_officer->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 ?? '' }}</td>
                    </tr>
                    <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 style="padding: 2px; vertical-align: top;">
                            {{ hitungHariKerja($permohonan->penilaian->updated_at, $tanggalLaporan) }}</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>
                    <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>

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

    <div class="no-break">

    <table>
        <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

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

                                $hubCadeb = $cekHub === 'sesuai'
        ? ($forminspeksi['asset']['hub_cadeb']['sesuai'] ?? null)
        : ($forminspeksi['asset']['hub_cadeb']['tidak sesuai'] ?? null);
                    @endphp
                        <td style=" padding: 2px; vertical-align: top;">Hubungan Pemilik Jaminan dengan Debitur</td>
                        <td style=" padding: 2px; vertical-align: top;">:</td>
                        <td style=" padding: 2px; vertical-align: top;">{{ $hubCadeb ?? '' }}</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']
                                : $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;">
                <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'] ?? ['-']) }}
                        </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;">
                            {{ implode(', ', $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'] ?? ['-']) }}
                        </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'] ?? ['-']) }}
                        </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'] ?? ['-']) }}
                        </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>
            </td>
        </tr>
    </table>
</div>

        @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

                    @foreach ($kategoriUnik as $kategori)

                        @if ($kategori !== 'lingkungan')
                            @include('lpj::penilai.components.analisa.' . str_replace('-', '-', $kategori), [
                            'dokumen' => $dokumen,
                        ])
                        @endif

                    @endforeach
                @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>
        @include('lpj::penilai.components.print-pembanding')
    </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="3" style="padding: 8px; text-align: left;">
                                <strong>a. Total Nilai Pasar Wajar</strong>
                            </td>
                        </tr>
                        @php
                            $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
                        @foreach ($labelNilai as $key => $label)
                            @if (isset($lpjData['luas_' . $key]))
                                <tr>
                                    <td style="padding: 3px; text-align: right; width:30%; ">
                                        {{ $label }} {{ $lpjData['luas_' . $key] ?? '' }} m<sup>2</sup>
                                    </td>
                                    <td style="padding: 3px; width:30%;">
                                        <span style="padding-left:20px; padding-right: 20px">X</span> Rp
                                        {{ $lpjData['nilai_' . $key . '_1'] ?? '' }}
                                    </td>
                                    <td style="padding: 3px; text-align: left; width:40%;">
                                        <span style="padding-left:20px; padding-right: 20px"> = </span> Rp
                                        {{ $lpjData['nilai_' . $key . '_2'] ?? '' }}
                                    </td>
                                </tr>
                            @endif
                        @endforeach
                        @if (isset($lpjData['npw_tambahan']))
                            @foreach ($lpjData['npw_tambahan'] as $npw)
                                <tr>
                                    <td style="padding: 3px; text-align: right;">
                                        {{ $npw['name'] }} {{ $npw['luas'] }} m<sup>2</sup>
                                    </td>
                                    <td style="padding: 3px;">
                                        <span style="padding-left:20px; padding-right: 20px">X</span> Rp
                                        {{ $npw['nilai_1'] }}
                                    </td>
                                    <td style="padding: 3px; text-align: left; width:40%">
                                        <span style="padding-left:20px; padding-right: 20px"> = </span> Rp
                                        {{ $npw['nilai_2'] }}
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                        <tr>
                            <td style="padding: 3px; font-weight: bold; width:30%;">Total Nilai Pasar Wajar</td>
                            <td style="padding: 3px; text-align: right; font-weight: bold; width:30%"></td>
                            <td style="padding: 3px; text-align: left; font-weight: bold; width: 40%;">
                                <span style="padding-left: 20px; padding-right: 20px;">=</span>
                                <div style="display: inline-block; border-top: 1px solid black; padding-top: 3px;">
                                    Rp {{ $lpjData['total_nilai_pasar_wajar'] ?? '' }}
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="3" style="padding: 8px; text-align: left; font-weight: bold;">
                                b. Total Nilai Likuidasi
                            </td>
                        </tr>
                        <tr>
                            <td style="padding: 3px; text-align: right; width:20%">
                                {{ $lpjData['likuidasi'] ?? '' }}%
                            </td>
                            <td style="padding: 3px; text-align: left; font-weight: bold; width:40%">
                                <span style="padding-left:20px; padding-right: 20px">X</span> Total Nilai Pasar Wajar
                            </td>
                            <td style="padding: 3px; text-align: left; width:40%;">
                                <span style="padding-left:20px; padding-right: 20px"> = </span> Rp
                                {{ $lpjData['likuidasi_nilai_2'] ?? '' }}
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
</main>
@include('lpj::penilai.components.footer')
