@include('lpj::penilai.components.header')

@php
    $data = '';
    $dokument = null;
    foreach ($permohonan->documents as $dokumen) {
        $data      .= $dokumen->jenisJaminan->name . ', ';
        $jenisAset  = $dokumen->jenisJaminan->name;
        $dokument   = $dokumen;
    }
    $data = rtrim($data, ', ');

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

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

                if ($teams) {
                    foreach ($teams as $team) {
                        $team_users = $team->teamsUsers;
                        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

    <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="vertical-align: top;">
                <table>
                    <tr>
                        <td style="width: 25%; padding: 2px; vertical-align: top;">Cabang/Pemohon</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td style="vertical-align: top;  vertical-align: top;">
                            {{ $permohonan->branch->name ?? '' }}
                        </td>
                    </tr>
                    <tr>
                        <td style="width: 25%; padding: 2px;">Tujuan Penilai</td>
                        <td style="width: 1%; padding: 2px;">:</td>
                        <td style="vertical-align: top;  padding: 2px;">
                            {{ $permohonan->tujuanPenilaian->name ?? '' }}</td>
                    </tr>
                    <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="width: 25%; padding: 2px;">Ao</td>
                        <td style="width: 1%;  padding: 2px;">:</td>
                        <td style="vertical-align: top;  padding: 2px;">{{ $permohonan->user->name ??  $permohonan->mig_nama_ao ?? '' }}</td>
                    </tr>
                </table>
            </td>
            <td style="vertical-align: top;">
                <table>
                    <tr>
                        <td style="width: 25%; padding: 2px;">Tanggal Order</td>
                        <td style="width: 1%;  padding: 2px;">:</td>
                        <td style="vertical-align: top;  padding: 2px;">
                            {{ formatTanggalIndonesia($permohonan->tanggal_permohonan) }}</td>
                    </tr>
                    <tr>
                        <td style="width: 25%; padding: 2px;">Tanggal Survey</td>
                        <td style="vertical-align: top; width: 1%;  padding: 2px;">:</td>
                        <td style="vertical-align: top; width: 79%;">
                            {{ formatTanggalIndonesia($permohonan->penilaian->waktu_penilaian) }}
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
    <div class="no-break">
        <table style="width: 100%; ">
            <tr>
                <td style="width: 100%; border: 1px solid #000; text-align: center;">
                    IDENTITAS CADEB/DEBITUR
                </td>
            </tr>
        </table>

        <table style="width: 100%; ">
            <tr>
                <td style="width: 5%; padding: 2px; vertical-align: top;">Nama Debitur</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td style="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="width: 25%; padding: 2px; vertical-align: top;">Debitur / Wakil Debitur</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td>
                    {{ is_array($forminspeksi['asset']['debitur_perwakilan'] ?? null)
                        ? implode('<br>', array_map(fn($v) => $v ?? $permohonan->debiture->name ?? '', $forminspeksi['asset']['debitur_perwakilan']))
                        : ($forminspeksi['asset']['debitur_perwakilan'] ?? $permohonan->debiture->name ?? '') }}
                </td>
            </tr>
            <tr>
                <td style="width: 25%; padding: 2px; vertical-align: top;">Pihak Bank selain Appraisal</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td style="vertical-align: top;">{{ $forminspeksi['asset']['pihak_bank'] ?? '' }}
                </td>
            </tr>
        </table>
    </div>

    <div class="no-break">
        <table style="width: 100%; ">
            <tr>
                <td style="width: 100%; border: 1px solid #000; text-align: center;">
                    DESKRIPSI DAN ALAMAT ASET
                </td>
            </tr>
        </table>

        @if ($permohonan->is_mig)
                @if ($dokument)
                    <table style="width: 100%;">
                        <tr>
                            <td width="25%">Jenis Jaminan</td>
                            <td width="1%">:</td>
                            <td>{{ $dokument->jenisJaminan?->name }}</td>
                        </tr>

                        @foreach ($dokument->detail as $detail)
                            @if ($detail->name === 'LOKASI JAMINAN' && $detail->details)
                                @include('lpj::component.detail-lokasi', ['details' => json_decode($detail->details, true)])
                            @endif
                        @endforeach
                    </table>
                @endif
        @else
            <table style="width: 100%; ">
                <tr>
                    <td width="25%">Terletak di</td>
                    <td style="width: 1%; vertical-align: top;">:</td>
                    <td> {{ $alamat['address'] ?? '' }}</td>
                </tr>
                <tr>
                    <td>Desa/Kelurahan</td>
                    <td style="width: 1%; vertical-align: top;">:</td>
                    <td>{{ $alamat['village_code'] ?? '' }}</td>
                </tr>
                <tr>
                    <td>Kecamatan</td>
                    <td style="width: 1%; vertical-align: top;">:</td>
                    <td>{{ $alamat['district_code'] ?? '' }}</td>
                </tr>
                <tr>
                    <td>Kabupaten/Kota</td>
                    <td style="width: 1%; vertical-align: top;">:</td>
                    <td> {{ $alamat['city_code'] ?? '' }}</td>
                </tr>
                <tr>
                    <td>Provinsi</td>
                    <td style="width: 1%; vertical-align: top;">:</td>
                    <td>{{ $alamat['province_code'] ?? '' }}</td>
                </tr>
            </table>
        @endif
    </div>

    <table style="width: 100%; ">
        <tr>
            <td style="width: 100%; border: 1px solid #000; text-align: center;">
                DOKUMEN
            </td>
        </tr>
    </table>

    <table style="width: 100%; ">
        @include('lpj::component.print-out-dokument')
    </table>

    @if (!$permohonan->is_mig)
        <div class="no-break">
            <table style="width: 100%; ">
                <tr>
                    <td style="width: 100%; border: 1px solid #000; text-align: center;">
                        HUBUNGAN CADEB/DEBITUR DENGAN PEMILIK DAN PENGHUNI
                    </td>
                </tr>
            </table>
            <table style="width:100%">
                <tr>
                    @php
                        $hubCadebRaw = $forminspeksi['asset']['hub_cadeb'] ?? null;
                        $cekHubDebitur = null;
                        $hubCadebDebitur = '';

                        if (is_array($hubCadebRaw)) {
                            if (array_key_exists('sesuai', $hubCadebRaw)) {
                                $cekHubDebitur = 'sesuai';
                            } elseif (array_key_exists('tidak sesuai', $hubCadebRaw)) {
                                $cekHubDebitur = 'tidak sesuai';
                            }
                            $hubCadebDebitur = $cekHubDebitur ? ($hubCadebRaw[$cekHubDebitur] ?? '') : '';
                        } else {
                            if (in_array($hubCadebRaw, ['sesuai', 'tidak sesuai'], true)) {
                                $cekHubDebitur = $hubCadebRaw;
                                $hubCadebDebitur = $hubCadebRaw;
                            }
                        }
                    @endphp
                    <td style="width:25%; padding: 2px; vertical-align: top;">Hubungan Pemilik Jaminan dengan Debitur</td>
                    <td style="width:1%; padding: 2px; vertical-align: top;">:</td>
                    <td style=" padding: 2px; vertical-align: top;">{{ $hubCadebDebitur ?? '' }}</td>
                </tr>
                <tr>
                    @php
                        $valPenghuni = $forminspeksi['asset']['hub_cadeb_penghuni'] ?? null;
                        if (is_array($valPenghuni)) {
                            $valPenghuni = implode(', ', array_map(
                                fn($k, $v) => $k . ($v ? ', ' . $v : ''),
                                array_keys($valPenghuni),
                                array_values($valPenghuni)
                            ));
                        }
                        $hubCadeb = $valPenghuni;
                    @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>
        </div>

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

                    @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="width: 100%; border: 1px solid #000; text-align: center;">
                    INFORMASI DATA PEMBANDING
                </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="width: 100%; border: 1px solid #000; text-align: center;">
                    TOTAL NILAI PASAR WAJAR
                </td>
            </tr>
        </table>
        <table style="width: 100%;">
            <tr>
                <td colspan="6" style=" padding: 8px; text-align: left;width:100%">1. Total Nilai Pasar Wajar
                </td>
            </tr>
            @php
                $totalNilaiPasarWajar = 0;
                $labelNilai = [
                    'bangunan'         => 'Luas Bangunan',
                    'tanah'            => 'Luas Tanah',
                    'apartemen-kantor' => 'Luas Unit',
                    'apartemen'        => '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']);
                }

                if (strcasecmp($jenisAset, 'Apartemen') === 0) {
                    $labelNilai['tanah'] = 'Luas Unit';
                }
            @endphp

            @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>
            <!-- Total Nilai Likuidasi -->
            @if (isset($permohonan_migrasi->mig_mst_lpj_tot_nilai_likuidasi) || isset($lpjData['likuidasi']) && $lpjData['likuidasi_nilai_2'] > 0)
                <tr>
                    <td colspan="6" style="padding: 8px; text-align: left">
                        2. 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>
                </tr>
            @endif
        </table>
    </div>
    @php
        $keterangan = $lpjData['keterangan_penilai'] ?? '';
        if (is_array($keterangan)) {
            $keterangan = implode(', ', array_filter($keterangan));
        }
    @endphp
    @if($permohonan->is_mig)
        @if (!empty($forminspeksi['fakta']['fakta_positif']))
            <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; border: 1px solid #000; text-align: center;">
                            FAKTOR POSITIF
                        </td>
                    </tr>
                </table>
            @foreach ($forminspeksi['fakta']['fakta_positif'] as $key => $item)
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; text-align: left;">
                            @if (is_array($item))
                                @foreach ($item as $data_)
                                    {!! nl2br(e($data_)) !!}<br>
                                @endforeach
                            @else
                                {!! nl2br(e($item)) !!}
                            @endif
                        </td>
                    </tr>
                </table>
            @endforeach
        @endif
        @if (!empty($forminspeksi['fakta']['fakta_negatif']))
            <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; border: 1px solid #000; text-align: center;">
                            FAKTOR NEGATIF
                        </td>
                    </tr>
                </table>
            @foreach ($forminspeksi['fakta']['fakta_negatif'] as $key => $item)
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; text-align: left;">
                            @if (is_array($item))
                                @foreach ($item as $data_)
                                    {!! nl2br(e($data_)) !!}<br>
                                @endforeach
                            @else
                                {!! nl2br(e($item)) !!}
                            @endif
                        </td>
                    </tr>
                </table>
            @endforeach
        @endif
        @if (!empty($forminspeksi['fakta']['informasi_dokument']))
            <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; border: 1px solid #000; text-align: center;">
                            LAIN - LAIN
                        </td>
                    </tr>
                </table>
            @foreach ($forminspeksi['fakta']['informasi_dokument'] as $key => $item)
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; text-align: left;">
                            @if (is_array($item))
                                @foreach ($item as $data_)
                                    {!! nl2br(e($data_)) !!}<br>
                                @endforeach
                            @else
                                {!! nl2br(e($item)) !!}
                            @endif
                        </td>
                    </tr>
                </table>
            @endforeach

        @endif

        @if (!empty($forminspeksi['fakta']['keterangan']))
            @foreach ($forminspeksi['fakta']['keterangan'] as $key => $item)
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; border: 1px solid #000; text-align: center;">
                            {{ $key }}
                        </td>
                    </tr>
                </table>
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; text-align: left;">
                            @if (is_array($item))
                                @foreach ($item as $data_)
                                    {!! nl2br(e($data_)) !!}<br>
                                @endforeach
                            @else
                                {!! nl2br(e($item)) !!}
                            @endif
                        </td>
                    </tr>
                </table>
            @endforeach
        @endif

        @if (!empty($forminspeksi['fakta']['saran']))
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; border: 1px solid #000; text-align: center;">
                            SARAN
                        </td>
                    </tr>
                </table>
            @foreach ($forminspeksi['fakta']['saran'] as $key => $item)
                <table class="judul"  style="width: 100%; ">
                    <tr>
                        <td style="width: 100%; text-align: left;">
                            <ul style="margin: 0px;padding-left: 10px;">
                                <li>
                                    @if (is_array($item))
                                        @foreach ($item as $data_)
                                            {!! nl2br(e($data_)) !!}<br>
                                        @endforeach
                                    @else
                                        {!! nl2br(e($item)) !!}
                                    @endif
                                </li>
                            </ul>
                        </td>
                    </tr>
                </table>
            @endforeach
        @endif
    @endif

    @if (!empty($keterangan))
        <table style="width: 100%  margin-top: 20px">
            <tr>
                <td style="width: 25%; vertical-align: top;">Catatan yang Perlu Diperhatikan</td>
                <td style="width:
                    1%; vertical-align: top">:</td>
                <td style="vertical-align: top">

                    {!! nl2br(e($keterangan)) !!}

                </td>
            </tr>
        </table>
    @endif
    <div class="no-break" style="margin-top:20px;">
        <table style="width: 100%">
            <tr>
                <td>Demikian laporan penilai jaminan ini di buat secara objektif, tanpa adanya pengaruh baik intern
                    maupun ekstern</td>
            </tr>
        </table>

        @include('lpj::penilai.components.signature-approval',['laporan' => 'standar','npw' => $totalNilaiPasarWajar])
    </div>
    <hr />
    <table style="width: 100%; ">
        <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>
</main>
@include('lpj::penilai.components.footer')
