<!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">
    <title>Surat Tender | {{ formatTanggalIndonesia(now()) }}</title>
    <style>
        body {
            font-family: Calibri;
            margin: 20px;
            color: #333;
        }

        .container {
            max-width: 800px;
            margin: auto;
            line-height: 1.6;
        }

        .content {
            margin-top: 20px;
            white-space: pre-line;
            /* To handle line breaks in text */
        }

        .content-max {
            margin-top: 20px;
            max-width: 800px;
        }

        .flex-wrap {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }

        .important {
            font-weight: bold;
            color: black;
        }

        .signature {
            margin-top: 40px;
            font-family: 'Brush Script MT', cursive;
            font-size: 1.2em;
        }

        .footer {
            margin-top: 20px;
            font-size: 0.9em;
            color: #555;
        }
    </style>
</head>

<body>
    <div class="container">
        Dear <span class="important">
            @php
                $allPeople = [];

                foreach ($penawaran->penawaranKjpp as $kjpp) {
                    if ($kjpp->kjpp->nama_pic_admin) {
                        $allPeople[] = ucwords($kjpp->kjpp->nama_pic_admin);
                    }

                    if ($kjpp->kjpp->detail_nama_pic_admin) {
                        try {
                            $decoded = json_decode($kjpp->kjpp->detail_nama_pic_admin);
                            if ($decoded) {
                                foreach ($decoded as $admin) {
                                    if (isset($admin->nama_pic_admin)) {
                                        $allPeople[] = ucwords($admin->nama_pic_admin);
                                    }
                                }
                            }
                        } catch (Exception $e) {
                            // Handle invalid JSON silently
                        }
                    }
                }

                $allPeople = array_filter($allPeople);
                $totalPeople = count($allPeople);
            @endphp
            @if ($totalPeople > 0)
                @foreach ($allPeople as $index => $person)
                    {{ $person }}{{ $index === $totalPeople - 2 ? ' dan ' : ($index < $totalPeople - 2 ? ' , ' : '') }}
                @endforeach
            @else
                Tidak Ada
            @endif
        </span>

        <div class="content">
            Mohon untuk dibuatkan proposal jasa appraisal atas nama <span
                class="important">{{ $permohonan->debiture->name }}</span>, tujuan penilaian untuk <span
                class="important">{{ $penawaran->tujuanPenilaianKJPP->name }}</span>, laporan dalam bentuk <span
                class="important">{{ $penawaran->jenisLaporan->name }}</span>, dengan data-data sebagai berikut:
        </div>

        <div class="content-max">
            Aset Jaminan: @foreach ($permohonan->documents as $document)
                {{ $document->jenisJaminan->name }}
            @endforeach
            <span class="flex-wrap">Lokasi Jaminan: @foreach ($permohonan->documents as $document)
                    {{ $document->address }}, Kel. @foreach ($villages as $village)
                        {{ $village->name }}
                        @endforeach, Kec. @foreach ($districts as $district)
                            {{ $district->name }}
                            @endforeach,@foreach ($cities as $city)
                                {{ ucwords(strtolower($city->name)) }}
                                @endforeach,@foreach ($provinces as $province)
                                    {{ $province->name }}
                                @endforeach
                            @endforeach
            </span>

            <br> Dokumen Jaminan: <br>
            @php
                $n = 1;
            @endphp

            @foreach ($permohonan->documents as $document)
                @foreach ($document->detail as $index => $detail)
                    @if($detail->details)
                        @php $luastanah = $luasbangunan = 0; @endphp
                        @foreach (json_decode($detail->details) as $key => $value)
                            @foreach($value as $k => $v)
                                @if($k=='luas_tanah')
                                    @php
                                        $luastanah += (int) preg_replace('/[^0-9.]/', '', str_replace('m2', '', $v));
                                    @endphp
                                @endif
                                @if($k=='luas_bangunan')
                                    @php
                                        $luasbangunan += (int) preg_replace('/[^0-9.]/', '', str_replace('m2', '', $v));
                                    @endphp
                                @endif
                            @endforeach
                        @endforeach

                        @if (isset($luastanah) && isset($luasbangunan))
                            {{ $n }}. {{ $detail->name }}<br>
                            Luas Tanah / Luas Bangunan: {{ $luastanah }} m<sup>2</sup> / {{ $luasbangunan }} m<sup>2</sup>
                            <br>
                            @php $n++; @endphp
                        @elseif (isset($luastanah))
                            {{ $n }}. {{ $detail->name }}<br>
                            Luas Tanah : {{ $luastanah }} m<sup>2</sup>
                            <br>
                            @php $n++; @endphp
                        @elseif (isset($luasbangunan))
                            {{ $n }}. {{ $detail->name }}<br>
                            Luas Bangunan: {{ $luasbangunan }} m<sup>2</sup>
                            <br>
                            @php $n++; @endphp
                        @endif

                    @endif
                @endforeach
            @endforeach
        </div>

        <div class="content">
            Harap proposal dibuat dengan harga yang minimal sehingga tidak perlu tawar menawar lagi. <br />
            Mohon proposal dapat saya terima segera, sebelum <span
                class="important">{{ formatTanggalIndonesia($penawaran->end_date, true) }}</span>
        </div>

        <div class="signature">
            Best Regards,<br />
            <img src="{{ asset('storage/signatures/' . $user->id . '/' . $user->sign) }}" alt="{{ $user->name }}"
                width="200">
            <p>
                {{ $user->name }}
            </p>
        </div>

        <div class="footer">
            PT. Bank Artha Graha Internasional, Tbk.<br>
            Gedung Bank Artha Graha, Lantai 3<br>
            Jl. Kwitang Raya No 24-26, Jakarta Pusat - 10420.<br>
            Telp. 021 - 3903040 (H)
        </div>
    </div>
</body>

</html>
