@include('lpj::surveyor.components.print-out.header')

<div style="border: 1px solid #000;">
    <table
        style="width: 100%; border-top: none; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">

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

            <td style="vertical-align: top;">
                <table>
                    <tr>
                        <td style="width: 25%; padding: 2px; vertical-align: top;">Tanggal Dibuat</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td style="vertical-align: top;  vertical-align: top;">
                            {{ formatTanggalIndonesia($permohonan->created_at) }}
                        </td>
                    </tr>
                    <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->tanggal_kunjungan) }}</td>
                    </tr>

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

    <table
        style="width: 100%; border-top: 1px solid #000; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">

        <tr>
            <td style="text-align: center; margin-top: 5px; ">
                <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                    Identitas Debitur
                </h2>
            </td>
        </tr>
    </table>
    <table style="width: 100%">
        <tr>
            <td style="width: 20%; 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>
        <tr>
            <td style="width: 20%; padding: 2px;">Nama Wakil</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="vertical-align: top;  padding: 2px;">
                @if (isset($forminspeksi['asset']['debitur_perwakilan']) && !empty($forminspeksi['asset']['debitur_perwakilan']))
                    @foreach ($forminspeksi['asset']['debitur_perwakilan'] as $key => $item)
                        {{ $item }}
                    @endforeach
                @endif
            </td>
        </tr>
        <tr>
            <td style="width: 20%; padding: 2px;">Hubungan Calon debitur</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="vertical-align: top;  padding: 2px;">
                @if (isset($forminspeksi['asset']['debitur_perwakilan']) && !empty($forminspeksi['asset']['debitur_perwakilan']))
                    @foreach ($forminspeksi['asset']['debitur_perwakilan'] as $key => $item)
                        {{ $item }}
                    @endforeach
                @endif
            </td>
        </tr>
    </table>

    <table
        style="width: 100%; border-top: 1px solid #000; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">

        <tr>
            <td style="text-align: center; margin-top: 5px; ">
                <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                    Lokasi Jaminan
                </h2>
            </td>
        </tr>
    </table>
    <table style="width: 100%">
      
           
                    <tr>
                        <td width="20%">Terletak di</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td> {{ $alamat['address'] ?? '' }}</td>
                    </tr>
                    <tr>
                        <td>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>Kotamadya</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td> {{ $alamat['city_code'] ?? '' }}</td>
                    </tr>
                    <tr>
                        <td>Propinsi</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td>{{ $alamat['province_code'] ?? '' }}</td>
                    </tr>
               
    </table>

    <div class="no-break" style="border-bottom: 1px solid #000;">
        <table
            style="width: 100%; border-top: 1px solid #000; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">

            <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
                    $hubCadebSesuai = $forminspeksi['asset']['hub_cadeb']['sesuai'] ?? null;
                    $hubCadebTidakSesuai = $forminspeksi['asset']['hub_cadeb']['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>
                                @php $count = 0; @endphp
                                @foreach ($basicData['hubCadeb'] as $item)
                                    <td>
                                        <label>
                                            <input type="checkbox"
                                                {{ isset($item->name) && ($hubCadebSesuai === $item->name || $hubCadebTidakSesuai === $item->name) ? 'checked' : '' }}>
                                            {{ $item->name ?? '' }}
                                        </label>
                                    </td>
                                    @php $count++; @endphp

                                    @if ($count % 3 === 0)
                            </tr>
                            <tr>
                    @endif
                    @endforeach

                    @if ($count % 3 !== 0)
                        @for ($i = 0; $i < 3 - ($count % 3); $i++)
                <td></td>
                @endfor
                @endif
            </tr>
        </table>
        @endif
        </td>
        </tr>


        <tr>

            @php
                $statusKey = isset($forminspeksi['asset']['hub_cadeb_penghuni']['sesuai']) ? 'sesuai' : 'tidak sesuai';
                $selectedData = $forminspeksi['asset']['hub_cadeb_penghuni'][$statusKey] ?? 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;">
                @if (isset($selectedData))
                    {{ $selectedData ?? '' }}
            </td>
            @endif
            </td>
        </tr>
        </table>
    </div>

    <div>
        <table
            style="width: 100%; border-top: 1px solid #000; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">
            <tr>
                <td style="text-align: center; margin-top: 5px; ">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        Kondisi Fisik Objek
                    </h2>
                </td>
            </tr>
        </table>

        <table style="width: 100%;">
            <tr>
                <td style="vertical-align: top; width: 25%;">Kendaraan</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table style="width: 100%;">
                        @if (isset($basicData['jenisKendaraan']))
                            @foreach ($basicData['jenisKendaraan'] as $item)
                                <tr>
                                    <td width="40%">
                                        <label class="form-label max-w-56 gap-2.5">
                                        <input class="checkbox" name="kendaraan[]" type="checkbox"
                                            value="{{ $item->name }}"
                                            {{ in_array($item->name, old('kendaraan', $forminspeksi['kendaraan'] ?? [])) ? 'checked' : '' }} />
                                        {{ $item->name }}
                                    </label>
                                    </td>   
                                    <td width="1%"></td>
                                    <td width="100%">
                                        <p>{{ old('kendaraan_input.' . $loop->index, $forminspeksi['kendaraan_input'][$loop->index] ?? '') }}</p>
                                    </td>
                                </tr>
                            @endforeach
                        @endif
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Kondisi</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;"></td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Nomor Polisi</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Merek</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Warana</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Nomor Rangka</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Nomor Mesin</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top;">Posisi Kilometer</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    <table>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="sesuai" >
                                    Sesuai 
                                </label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label>
                                    <input type="radio" name="luas_bangunan_radio" value="tidak sesuai" >
                                    Tidak Sesuai 
                                </label>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            @php
                $inputDataJaminan = [
                    [
                        'label' => 'Transmisi',
                        'value' => ['Otomatis', 'Manual'],
                        'name' => 'transmisi',
                        'index' => 0,
                    ],
                    [
                        'label' => 'Mesin dan panel Instrument',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'mesin_panel_instrument',
                        'index' => 1,
                    ],
                    [
                        'label' => 'Fungsi Mesin dan panel Instument',
                        'value' => ['Berfungsi', 'Rusak'],
                        'name' => 'fungsi_mesin_panel_instrument',
                        'index' => 2,
                    ],
                    [
                        'label' => 'Interior',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'interior',
                        'index' => 3,
                    ],
                    [
                        'label' => 'Jumlah pintu',
                        'value' => ['Dua Pintu', 'Empat Pintu', 'Lain-Lain'],
                        'name' => 'jumlah_pintu',
                        'index' => 4,
                    ],
                    [
                        'label' => 'Rangka dan karoseri',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'rangka_karoseri',
                        'index' => 5,
                    ],
                    [
                        'label' => 'Ban',
                        'value' => ['Standard', 'Tidak Standard'],
                        'name' => 'ban',
                        'index' => 6,
                    ],
                    [
                        'label' => 'Velg',
                        'value' => ['Standard', 'Tidak Standard'],
                        'name' => 'velg',
                        'index' => 7,
                    ],
                    [
                        'label' => 'Bamper Depan',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'bamper_depan',
                        'index' => 8,
                    ],
                    [
                        'label' => 'Bamper Belakang',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'bamper_belakang',
                        'index' => 9,
                    ],
                    [
                        'label' => 'Lampu depan',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'lampu_depan',
                        'index' => 10,
                    ],
                    [
                        'label' => 'Lampu belakang',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'lampu_belakang',
                        'index' => 11,
                    ],
                    [
                        'label' => 'Kaca kendaraan',
                        'value' => ['Terawat', 'Cukup Terawat', 'Tidak Terawat'],
                        'name' => 'kaca_kendaraan',
                        'index' => 12,
                    ],
                    [
                        'label' => 'Air Conditioner',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'air_conditioner',
                        'index' => 13,
                    ],
                    [
                        'label' => 'Tape/Radio/CD dan perlengkapan (Equalizer, Booster, dll)',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'tape_radio_cd',
                        'index' => 14,
                    ],
                    [
                        'label' => 'Sensor parkir',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'sensor_parkir',
                        'index' => 15,
                    ],
                    [
                        'label' => 'Sensor Camera (360), Recorder',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'sensor_camera_recorder',
                        'index' => 16,
                    ],
                    [
                        'label' => 'Lcd',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'lcd',
                        'index' => 17,
                    ],
                    [
                        'label' => 'Sabuk Keselamatan',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'sabuk_keselamatan',
                        'index' => 18,
                    ],
                    [
                        'label' => 'Airbag',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'airbag',
                        'index' => 19,
                    ],
                    [
                        'label' => 'Asuransi',
                        'value' => ['Ada', 'Tidak Ada'],
                        'name' => 'asuransi',
                        'index' => 20,
                    ],
                ];
            @endphp


            @foreach ($inputDataJaminan as $item)
                <tr>
                    <td style="vertical-align: top; width: 25%;">{{ $item['label'] }}</td>
                    <td width="1%" style="vertical-align: top;">:</td>
                    <td style="vertical-align: top;">
                        <table style="width: 100%; border-collapse: collapse;">
                            @foreach ($item['value'] as $value)
                                <tr>
                                    <td>
                                        <label class="form-label max-w-56 gap-2.5" style="width: 500px">
                                            <input class="checkbox" name="{{ $item['name'] }}[]" type="checkbox"
                                                value="{{ $value }}"
                                                {{ in_array($value, old($item['name'], $forminspeksi[$item['name']] ?? [])) ? 'checked' : '' }} />
                                            {{ $value }}
                                            @if ($forminspeksi[$item['name'] . '_input'][$loop->index] != null)
                                                -
                                                {{ old($item['name'] . '_input' . $loop->index, $forminspeksi[$item['name'] . '_input'][$loop->index] ?? '') }}
                                            @endif
                                        </label>
                                    </td>

                                </tr>
                            @endforeach
                        </table>
                    </td>
                </tr>
            @endforeach

            <tr>
                <td style="vertical-align: top;">Perusahaan Asuransi</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">{{ $forminspeksi['perusahaan_asuransi'] ?? '' }}</td>
            </tr>

            <tr>
                <td style="vertical-align: top;">Tahun Berakhir</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    {{ isset($forminspeksi['tahun_berakhir']) ? formatTanggalIndonesia($forminspeksi['tahun_berakhir']) : '' }}
                </td>
            </tr>
        </table>
    </div>


    <div>
        <table
            style="width: 100%; border-top: 1px solid #000; border-bottom: 1px solid #000; border-collapse: collapse; border-left: none; border-right: none;">


            <tr>
                <td style="text-align: center; margin-top: 5px;">
                    <h2 style=" text-transform: uppercase; text-align: center; margin: 0;">
                        ANalisa Fakta
                    </h2>

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

        <table style="width: 100%">
            <tr>
                <td width="25%" style="vertical-align: top;">Faktor Positif</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    @isset($forminspeksi['fakta_positif'])
                        <table style="width: 100%; border-collapse: collapse;">
                            @foreach ($forminspeksi['fakta_positif'] as $key => $item)
                                <tr>
                                    <td>{!! nl2br(e($item)) !!}</td>
                                </tr>
                            @endforeach
                        </table>
                    @endisset
                </td>
            </tr>
            <tr>
                <td width="25%" style="vertical-align: top;">Faktor Negatif</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    @php
                        $faktaNegatif = $forminspeksi['fakta_negatif'] ?? [];
                        $faktaNegatifFiltered = is_array($faktaNegatif)
                            ? array_filter($faktaNegatif, function ($item) {
                                return !empty(trim($item));
                            })
                            : [];
                    @endphp

                    @if (!empty($faktaNegatifFiltered))
                        <table style="width: 100%; border-collapse: collapse;">
                            @foreach ($faktaNegatifFiltered as $key => $item)
                                <tr>
                                    <td>{!! nl2br(e($item)) !!}</td>
                                </tr>
                            @endforeach
                        </table>
                    @else
                        <p>Tidak ada faktor negatif yang teridentifikasi.</p>
                    @endif
                </td>
            </tr>

            <tr>
                <td width="25%" style="vertical-align: top;">Analisa Makro</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    @php
                        $faktaNegatif = $forminspeksi['analisa_makro'] ?? [];
                        $faktaNegatifFiltered = is_array($faktaNegatif)
                            ? array_filter($faktaNegatif, function ($item) {
                                return !empty(trim($item));
                            })
                            : [];
                    @endphp

                    @if (!empty($faktaNegatifFiltered))
                        <table style="width: 100%; border-collapse: collapse;">
                            @foreach ($faktaNegatifFiltered as $key => $item)
                                <tr>
                                    <td>{!! nl2br(e($item)) !!}</td>
                                </tr>
                            @endforeach
                        </table>
                    @else
                        <p>Tidak ada faktor negatif yang teridentifikasi.</p>
                    @endif
                </td>
            </tr>

            <tr>
                <td width="20%" style="vertical-align: top;">Kesimpulan</td>
                <td width="1%" style="vertical-align: top;">:</td>
                <td width="100%" style="vertical-align: top;">
                    @php
                        $faktaNegatif = $forminspeksi['catatan'] ?? [];
                        $faktaNegatifFiltered = is_array($faktaNegatif)
                            ? array_filter($faktaNegatif, function ($item) {
                                return !empty(trim($item));
                            })
                            : [];
                    @endphp

                    @if (!empty($faktaNegatifFiltered))
                        <table style="width: 100%; border-collapse: collapse;">
                            @foreach ($faktaNegatifFiltered as $key => $item)
                                <tr>
                                    <td>{!! nl2br(e($item)) !!}</td>
                                </tr>
                            @endforeach
                        </table>
                    @else
                        <p>Tidak ada faktor negatif yang teridentifikasi.</p>
                    @endif
                </td>
            </tr>
        </table>
    </div>



</div>


@include('lpj::surveyor.components.print-out.footer')
