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

        $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;
                        }
                    }
                }
            }
        }
    }
    $data = rtrim($data, ', ');
@endphp


<main class="content">
    <table style="width: 100%">
        <tr>
            <td style="text-align: center; margin-top: 5px;">
                <h2 style="text-decoration: underline; text-transform: uppercase; text-align: center; margin: 0;">
                    MEMO ANTAR KANTOR</h2>
                    <p style="font-size: 12px;  margin: 0;">NO: {{ $nomorLaporan ?? '' }}</p>
            </td>
        </tr>
    </table>
    <hr/>
    <table style="width: 100%">
        <tr>
            <td style="width: 25%; padding: 2px;">Kepada</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $memo['kepada'] ?? '' }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Dari</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $memo['dari'] ?? '' }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Tanggal</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ formatTanggalIndonesia($memo['tanggal']) }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Perihal</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $memo['perihal'] ?? '' }}</td>
        </tr>
    </table>

    <table>
        <tr>
            <td>
                Menindak lanjuti permintan penilaian jaminan dari {{ $permohonan->user->name }} AO Cabang
                {{ $permohonan->debiture->branch->name ?? '' }}
                tanggal {{ formatTanggalIndonesia($permohonan->created_at) ?? '' }}, dapat di sampaikan sebagai berikut:
            </td>
        </tr>
    </table>

    <table style="margin: 5px 0; border-collapse: collapse; width: 100%;">
        <tr>
            <td style="">
                <h6 style="text-transform: uppercase; margin: 0; ">Data Debitur dan Jaminan</h2>
            </td>
        </tr>
    </table>
    <table style="width: 100%">

        <tr>
            <td style="width: 25%; padding: 2px;">Nama Calon Debitur</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $permohonan->debiture->name ?? '' }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Jenis Aset</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $data ?? '' }}</td>
        </tr>

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

        <tr>
            <td style="width: 25%; padding: 2px;">Atas Nama</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $permohonan->debiture->name ?? '' }}</td>
        </tr>

        @if (!empty($memo['lokasi']['address']))
            @php
                $alamatFields = [
                    'Terletak di' => 'address',
                    'Desa/Kelurahan' => 'village_code',
                    'Kecamatan' => 'district_code',
                    'Kabupaten/Kota' => 'city_code',
                    'Provinsi' => 'province_code'
                ];
            @endphp

            @foreach ($alamatFields as $label => $field)
                @if (!empty($alamat[$field]))
                    <tr>
                        <td width="25%">{{ $label }}</td>
                        <td style="width: 1%; vertical-align: top;">:</td>
                        <td>{{ $alamat[$field] }}</td>
                    </tr>
                @endif
            @endforeach
        @endif
    </table>



    <table style="margin: 5px 0; border-collapse: collapse; width: 100%;">
        <tr>
            <td style="">
                <h6 style="text-transform: uppercase; margin: 0; ">HASIL Kunjungan</h6>
            </td>
        </tr>
    </table>

    <table style="width: 100%">
        <tr>
            <td style="width: 25%; padding: 2px;">Tanggal Kunjungan</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">
                {{ formatTanggalIndonesia($permohonan->penilaian->updated_at) }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Surveyor</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $surveyor->userPenilaiTeam->name ?? '' }}</td>
        </tr>
        <tr>
            <td style="width: 25%; padding: 2px;">Pihak Pemohon</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $permohonan->user->name ?? '' }} -
                {{ $permohonan->debiture->branch->name ?? '' }}</td>
        </tr>

        <tr>
            <td style="width: 25%; padding: 2px;">Pihak Cadeb/Debitur</td>
            <td style="width: 1%;  padding: 2px;">:</td>
            <td style="width: 79%;  padding: 2px;">{{ $permohonan->debiture->name ?? '' }}</td>
        </tr>

    </table>
    <table>
        <tr>
            @isset($memo['hasil_survey'])
                @foreach ($memo['hasil_survey'] as $key => $item)
            <tr>
                <td>{{ $item }}</td>
            </tr>
            @endforeach
        @endisset
        </tr>
    </table>
    <table style="margin: 5px 0; border-collapse: collapse; width: 100%;">
        <tr>
            <td style="">
                <h6 style="text-transform: uppercase; margin: 0; ">KESIMPULAN DAN SARAN</h6>
            </td>
        </tr>
        <tr>
            @isset($memo['kesimpulan_saran'])
                @foreach ($memo['kesimpulan_saran'] as $key => $item)
            <tr>
                <td> {{ $item }}</td>
            </tr>
            @endforeach
        @endisset
        </tr>
    </table>

    <table style="width: 100%">
        <tr>
            <td>Demikian Kami Sampaikan, atas perhatiannya kami ucapkan terimakasih</td>
        </tr>
        <tr>
            @include('lpj::penilai.components.signature-approval')
        </tr>
    </table>
</main>
<div class="page-break"></div>
@if (isset($memo['foto']))

<table width="100%" border="0" style="align-content: center; text-align: center; margin-bottom: 20px">
    @foreach ($memo['foto'] as $item)
    @php
         $cleanedPath = str_replace('/storage/', '', $item);
            $imagePath = storage_path('app/public/' . $cleanedPath);
        @endphp
        <tr>
            <td style="width: 100%; padding: 10px;" class="photo-item border">
                <img src="{{ $imagePath }}" alt="{{ $item }}" class="photo-image">
            </td>
        </tr>
    @endforeach
</table>
@endif

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