<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Hasil Inspeksi</title>
    <style>
        * {

            font-size: 12px;
        }

        body {
            font-family: Arial, sans-serif;
        }

        /** Define the header rules **/
        @page {
            margin-top: 100px;
            margin-bottom: 50px;

        }

        #header {
            position: fixed;
            top: -80px;
            left: 0;
            right: 0;
            height: 50px;
            text-align: center;
            padding-bottom: 10px;
        }

        .header-logo {
            position: absolute;
            left: 20px;
            top: 10px;
            max-height: 80px;
            max-width: 150px;
        }

        .header-title {
            margin-top: 20px;
        }

        #footer {
            position: fixed;
            bottom: -50px;
            left: 0;
            right: 0;
            height: 50px;
            text-align: center;
            border-top: 1px solid #ddd;
            padding-top: 10px;
            font-size: 9px;
        }

        .page-number:after {
            content: counter(page) " dari " counter(pages);
        }

        .content {
            margin-top: 10px;
        }

        .page-break {
            page-break-after: always;
        }

        #footer {
            position: fixed;
            bottom: -50px;
            left: 0;
            right: 0;
            height: 50px;
            text-align: left;
            border-top: 1px solid #ddd;
            padding-top: 10px;
            font-size: 12px;
        }

        body {
            margin-top: 3cm;
            margin-bottom: 120px;
            position: relative;
            z-index: 1;
        }

        .border {
            border: 1px solid #000;
        }

        .no-break {
            page-break-inside: avoid;
            page-break-before: auto;
            page-break-after: auto;
        }

        .custom-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        .custom-table td {
            vertical-align: top;
        }

        .custom-table td:first-child {
            font-weight: bold;
            width: 30%;
            /* Sesuaikan lebar kolom keterangan */
        }

        table.checkbox-list {
            width: 100%;
            border-collapse: collapse;
        }

        td.checkbox-item {
            width: 33.33%;
            vertical-align: top;
            text-align: left;
        }

        label.checkbox-label {
            display: flex;
            align-items: center;
            font-size: 12px;
        }

        label.checkbox-label input[type="checkbox"] {
            margin-right: 6px;
            transform: scale(1.3);
            /* Memperkecil ukuran checkbox */
        }

        @media print {
        table {
            border-collapse: collapse;
        }
        tr {
            page-break-inside: avoid;
        }
        td, th {
            border: 1px solid #000;
        }
    }

    </style>
</head>

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

    $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;

                    if ($team_users) {
                        foreach ($team_users as $team_user) {
                            $user = $team_user->user;

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

    $surveyorUser = getUser($surveyor->userPenilaiTeam->id);
@endphp

<body style="width: 90%; margin: auto">
    <header id="header">
        <table style="width: 100%; border: none;">
            <tr>
                <td style="width: 20%; text-align: left;" class="header-left">
                    @include('lpj::component.logo-bag', ['width' => 150, 'height' => 40])
                </td>
                <td style="text-align: right;">
                    <div style="">
                        <p style="margin: 0; padding:0; font-size:10px;">
                            Tanggal: {{ \Carbon\Carbon::parse($permohonan->penilaian->updated_at)->format('d-m-Y') }}
                        </p>
                        <p style="margin: 0; padding:0; font-size:10px;">
                            Waktu: {{ \Carbon\Carbon::parse($permohonan->penilaian->updated_at)->format('H:i') }}
                        </p>
                        <p style="margin: 0; padding:0;  font-size:10px;">User: {{ $surveyorUser->name }}</p>
                    </div>
                </td>
            </tr>
        </table>
    </header>

    <div style="text-align: center; text-transform: uppercase; font-weight: bold;">
        <h1 style="font-size: 14px;">
            FORM INSPEKSI {{ $data ?? '' }}
        </h1>
    </div>

    <div>
        <table style="width: 100%; border: 1px solid #000; border-collapse: collapse;">
            <tr>
                <td style="text-align: center; margin-top: 5px; ">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        Tujuan Penilaian
                    </h2>
                </td>
            </tr>
        </table>
        <table style="width: 100%;">
            <tr>
                <td style="vertical-align: top;">
                    <table>
                        <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="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->debiture->branch->name ?? '' }} / {{ $permohonan->user->name ?? '' }}
                            </td>
                        </tr>

                        <tr>
                            <td style="width: 25%; padding: 2px; vertical-align: top;">Debitur/Perwakilan</td>
                            <td style="width: 1%;  padding: 2px; vertical-align: top;">:</td>
                            <td style="vertical-align: top;  padding: 2px;">
                                @php
                                    $debitur = $forminspeksi['asset']['debitur_perwakilan'] ?? null;
                                @endphp
                                @if ($debitur)
                                    @foreach (is_array($debitur) ? $debitur : [$debitur] as $item)
                                        <p style="margin:0;padding:0;">{{ $item }}</p>
                                    @endforeach
                                @endif
                            </td>
                        </tr>
                    </table>
                </td>

                <td style="vertical-align: top;">
                    <table>
                        <tr>

                            <td style="width: 25%; padding: 2px;">
                                Tanggal Survey</td>
                            <td style="width: 1%; padding: 2px;">:</td>
                            <td style="vertical-align: top;  padding: 2px;">
                                {{ formatTanggalIndonesia($permohonan->penilaian->updated_at) }}</td>
                        </tr>
                        <tr>
                            <td style="width: 25%; padding: 2px; vertical-align: top;">Pihak Bank</td>
                            <td style="width: 1%; vertical-align: top;">:</td>
                            <td style="vertical-align: top;  vertical-align: top;">
                                {{ $forminspeksi['asset']['pihak_bank'] ?? '' }}
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

        <table
            style="width: 100%; border: 1px solid #000; border-collapse: collapse; ">

            <tr>
                <td style="text-align: center; margin-top: 5px; ">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        Nama Cadeb/Debitur
                    </h2>
                </td>
            </tr>
        </table>
        <table style="width: 100%">
            <tr>
                <td style="width: 25%; padding: 2px; vertical-align: top;">Nama</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td style="vertical-align: top; width: 79%;">{{ $permohonan->debiture->name ?? '' }}</td>
            </tr>
        </table>

        <table
            style="width: 100%;  border: 1px solid #000; border-collapse: collapse;">

            <tr>
                <td style="text-align: center; margin-top: 5px; ">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        Deskripsi Aset jaminan
                    </h2>
                </td>
            </tr>
        </table>
        <table style="width: 100%">
            <tr>ß
                @php
                    $jenisAsset   = $forminspeksi['asset']['jenis_asset'] ?? [];
                    $selectedData = $jenisAsset['sesuai'] ?? $jenisAsset['tidak sesuai'] ?? null;
                    $lainnya      = $jenisAsset['Lainnya'] ?? null;
                @endphp

                <td width="25%" style="padding: 2px; vertical-align: top;">Jenis Aset</td>
                <td width="1%" style=" vertical-align: top;">:</td>
                <td style="vertical-align: top; width: %;">
                    {{ $selectedData ?? '' }} {{ $lainnya ?? '' }}
                </td>
            </tr>

            <tr>
                <td style="width: 25%; padding: 2px; vertical-align: top;">Alamat</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td style="vertical-align: top;">
                    @if ($permohonan->is_mig == 1 && $dokumen)
                        <table style="margin: 0">
                            @foreach ($dokumen->detail as $detail)
                                @if ($detail->name && $detail->details && $detail->dokumen_jaminan)
                                    <tr><td width="25%"><strong>{{ $detail->name }}</strong></td></tr>
                                @endif
                                @if ($detail->name == 'LOKASI JAMINAN' && $detail->details)
                                    @include('lpj::component.detail-lokasi', ['details' => json_decode($detail->details, true)])
                                @endif
                            @endforeach
                        </table>
                    @else
                        <table style="margin: 0">
                            <tr><td width="25%">Terletak di</td><td>{{ $alamat['address'] ?? '' }}</td></tr>
                            <tr><td>Kelurahan</td><td>{{ $alamat['village_code'] ?? '' }}</td></tr>
                            <tr><td>Kecamatan</td><td>{{ $alamat['district_code'] ?? '' }}</td></tr>
                            <tr><td>Kotamadya</td><td>{{ $alamat['city_code'] ?? '' }}</td></tr>
                            <tr><td>Propinsi</td><td>{{ $alamat['province_code'] ?? '' }}</td></tr>
                        </table>
                    @endif
                </td>
            </tr>
        </table>

        <table style="width: 100%;  border: 1px solid #000; border-collapse: collapse; ">
            <tr>
                <td style="text-align: center; margin-top: 5px; ">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        Titik Koordinat Google Maps
                    </h2>
                </td>
            </tr>
        </table>

        <table style="width: 100%">
            <tr>
                <td style="width: 25%; padding: 2px; vertical-align: top;">Titik Kordinat</td>
                <td style="width: 1%; vertical-align: top;">:</td>
                <td style="vertical-align: top; width: 79%;">
                    {{ isset($forminspeksi['asset']['kordinat_lat']) && !empty($forminspeksi['asset']['kordinat_lat']) ? $forminspeksi['asset']['kordinat_lat'] : '-' }}
                    ,
                    {{ isset($forminspeksi['asset']['kordinat_lng']) && !empty($forminspeksi['asset']['kordinat_lng']) ? $forminspeksi['asset']['kordinat_lng'] : '-' }}
                </td>
            </tr>
        </table>

        <div class="no-break" >
            <table style="width: 100%;  border: 1px solid #000; border-collapse: collapse; ">
                <tr>
                    <td style="text-align: center; margin-top: 5px; ">
                        <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                            STATUS KEPEMILIKAN, HUBUNGAN DAN PENGHUNI
                        </h2>
                    </td>
                </tr>
            </table>

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

                <tr>
                    @php
                        // Ambil nilai sesuai dan tidak sesuai
                        $hubCadeb            = $forminspeksi['asset']['hub_cadeb'] ?? [];
                        $hubCadebSesuai      = $hubCadeb['sesuai'] ?? null;
                        $hubCadebTidakSesuai = $hubCadeb['tidak sesuai'] ?? null;
                    @endphp
                    <td style="vertical-align: top;">Hubungan Pemilik Jaminan dengan Debitur</td>
                    <td style="width:1%; padding: 2px; vertical-align: top;">:</td>
                    <td>
                        <div style="margin-bottom: 10px;">
                            <label>
                                <input type="radio" name="hubCadebStatus" value="sesuai"
                                    {{ $hubCadebSesuai ? 'checked' : '' }}> Sesuai
                            </label>
                            <label style="margin-left: 20px;">
                                <input type="radio" name="hubCadebStatus" value="tidak sesuai"
                                    {{ $hubCadebTidakSesuai ? 'checked' : '' }}> Tidak Sesuai
                            </label>
                        </div>

                        @if (!empty($basicData['hubCadeb']))
                            <table>
                                <tr>
                                    @foreach ($basicData['hubCadeb'] as $item)
                                        <td>
                                            <label>
                                                <input type="checkbox"
                                                    {{ isset($item->name) && ($hubCadebSesuai === $item->name || $hubCadebTidakSesuai === $item->name) ? 'checked' : '' }}>
                                                {{ $item->name ?? '' }}
                                            </label>
                                        </td>
                                        @if ($loop->iteration % 3 === 0)
                                            </tr>
                                            <tr>
                                        @endif
                                    @endforeach
                                </tr>
                            </table>
                        @endif
                    </td>
                </tr>

                <tr>
                    @php
                        $selectedData = $forminspeksi['asset']['hub_cadeb_penghuni']['sesuai']
                                     ?? $forminspeksi['asset']['hub_cadeb_penghuni']['tidak sesuai']
                                     ?? null;
                    @endphp

                    <td style="vertical-align: top; width: 20%; padding: 2px;">Hubungan Penghuni Jaminan dengan Debitur</td>
                    <td style="width:1%; padding: 2px; vertical-align: top;">:</td>
                    <td style="vertical-align: top;">
                        {{ $selectedData ?? '' }}
                    </td>
                </tr>
            </table>
        </div>



        @if (strtolower($permohonan->tujuanPenilaian->name) == 'rap')
            @include('lpj::surveyor.components.print-out.rap')
        @else
            @foreach ($permohonan->documents as $dokumen)
                @if ($dokumen->jenisJaminan)
                    @php
                        $formKategori = json_decode($dokumen->jenisJaminan->form_kategori, true) ?: [];
                        $kategoriUnik = array_unique(is_array($formKategori) ? $formKategori : [$formKategori]);
                    @endphp

                    @foreach ($kategoriUnik as $kategori)
                        @includeIf('lpj::surveyor.components.print-out.' . $kategori, ['dokumen' => $dokumen])
                    @endforeach
                @endif
            @endforeach
        @endif
    </div>

    <div class="no-break">
        <table style="width: 100%; border-collapse: collapse;">
            <tr>
                <td>
                    <table>
                        <tr>
                            <td>
                                Jakarta {{ formatTanggalIndonesia($permohonan->penilaian->waktu_penilaian) }}
                            </td>
                        </tr>

                        <tr>
                            <td>
                                TTD
                            </td>
                        </tr>
                    </table>
                    <table style="width: 100%; border-collapse: collapse; text-align: center;">
                        <tr>
                            @if (isset($forminspeksi['signature']['penilai']['image']) && !empty($forminspeksi['signature']['penilai']['image']))
                                @php
                                    $imagePath = $forminspeksi['signature']['penilai']['image'];

                                    $isBase64 = str_starts_with($imagePath, 'data:image');

                                    if (!$isBase64) {
                                        $relativePath = str_replace(url('storage') . '/', '', $imagePath);
                                        $absolutePath = storage_path('app/public/' . $relativePath);
                                    } else {
                                        $absolutePath = $imagePath;
                                    }

                                @endphp
                                <td style="width: 100px;">
                                    <img style="width: 100px;" src="{{ $absolutePath }}" alt="">
                                </td>
                            @endif

                            @if (isset($forminspeksi['signature']['cabang']))
                                @php
                                    $imagePath = $forminspeksi['signature']['cabang']['image'];

                                    $isBase64 = str_starts_with($imagePath, 'data:image');

                                    if (!$isBase64) {
                                        $relativePath = str_replace(url('storage') . '/', '', $imagePath);
                                        $absolutePath = storage_path('app/public/' . $relativePath);
                                    } else {
                                        $absolutePath = $imagePath;
                                    }

                                @endphp
                                <td style="width: 100px;">
                                    <img style="width: 100px;" src="{{ $absolutePath }}" alt="">
                                </td>
                            @endif


                            @if (isset($forminspeksi['signature']['debitur']))
                                <td style="width: 100px;">
                                    <img style="width: 100px;"
                                        src="{{ $forminspeksi['signature']['debitur']['image'] }}"
                                        alt="Debitur/Perwakilan">
                                </td>
                            @endif

                            <!-- K.J.P.P -->
                            @if (isset($forminspeksi['signature']['kjpp']))
                                <td style="width: 100px;">
                                    <img style="width: 100px;"
                                        src="{{ $forminspeksi['signature']['kjpp']['image'] }}" alt="KJPP">
                                </td>
                            @endif
                        </tr>


                        <tr>
                            @if (isset($forminspeksi['signature']['penilai']['name']))
                                <td style=" text-align: center">
                                    <span>{{ ucwords(strtolower($forminspeksi['signature']['penilai']['name'])) }}</span>
                                    <br />
                                    <span>{{ ucwords(strtolower('PENILAI')) }}</span>
                                </td>
                            @endif
                            @if (isset($forminspeksi['signature']['cabang']['name']))
                                <td style=" text-align: center">
                                    <span>{{ ucwords(strtolower($forminspeksi['signature']['cabang']['name'])) }}</span>
                                    <br />
                                    <span>{{ ucwords(strtolower('CABANG')) }}</span>
                                </td>
                            @endif
                            @if (isset($forminspeksi['signature']['debitur']['name']))
                                <td style=" text-align: center">
                                    <span>{{ ucwords(strtolower($forminspeksi['signature']['debitur']['name'])) }}</span>
                                    <br />
                                    <span>{{ ucwords(strtolower('DEBITUR/PERWAKILAN')) }}</span>
                                </td>
                            @endif
                            @if (isset($forminspeksi['signature']['kjpp']['name']))
                                <td style=" text-align: center">
                                    <span>{{ ucwords(strtolower($forminspeksi['signature']['kjpp']['name'])) }}</span>
                                    <br />
                                    <span>{{ strtoupper('K.J.P.P') }}</span>
                                </td>
                            @endif
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>


    <footer id="footer">
        {{-- Cabang : {{ $permohonan->debiture->branch->name ?? '' }} --}}
    </footer>
    <script type="text/php">
        if (isset($pdf)) {
            $pdf->page_script('
                $font = $fontMetrics->get_font("Arial, Helvetica, sans-serif", "normal");
                $size = 10;
                $pageText = $PAGE_NUM;
                $y = 800;
                $x = 550;
                $pdf->text($x, $y, $pageText, $font, $size);
            ');
        }
    </script>
</body>

</html>
