@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render(request()->route()->getName(), request()->route('noreg')) }}
@endsection

@section('content')
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
        <div class="card border border-agi-100 pb-2.5">
            <div class="card-header bg-agi-50" id="basic_settings">
                <h3 class="card-title">
                    Surat Tender
                </h3>
                <div class="flex items-center gap-2">
                    <a href="{{ route('tender.penawaran.downloadSuratTender', $noreg) }}" class="btn btn-xs btn-light">
                        <img src="{{ asset('img/pdf.png') }}" width="25" alt="pdf" class="pdf"></img>Download
                        </span>
                        @if (isset($penawaran->nomor_registrasi))
                            <a href="{{ route('tender.penawaran.editPenawaran', $noreg) }}" class="btn btn-xs btn-info"><i
                                    class="ki-filled ki-exit-left"></i> Back</a>
                        @else
                            <a href="{{ route('tender.penawaran.createPenawaran', $noreg) }}" class="btn btn-xs btn-info"><i
                                    class="ki-filled ki-exit-left"></i> Back</a>
                        @endif
                </div>
            </div>
            <div class="card-body grid gap-5 text-gray-900">
                <p>Dear
                    <span class="font-bold">
                        @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>
                </p>
                <p>Mohon untuk dibuatkan proposal jasa appraisal atas nama <span
                        class="font-bold">{{ ucwords($permohonan->debiture->name) }}</span>, tujuan penilaian
                    untuk <span class="font-bold">
                        {{ $penawaran->tujuanPenilaianKJPP->name }}
                    </span>, laporan dalam bentuk <span class="font-bold">{{ $penawaran->jenisLaporan->name }}</span>,
                    dengan data-data sebagai berikut :</p>
                <ul>
                    <li>Aset Jaminan:
                        <span class="font-bold">
                            @foreach ($permohonan->documents as $document)
                                {{ $document->jenisJaminan->name }}
                            @endforeach
                        </span>
                    </li>
                    <li>Lokasi Jaminan:
                        <span class="font-bold">
                            @foreach ($permohonan->documents as $document)
                                {{ $document->address }}
                                @if (isset($document->jenisJaminan))
                                    , 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
                                @endif
                            @endforeach
                        </span>
                    </li>
                    <li>Dokumen Jaminan: <br>
                        <span class="font-bold">
                            @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
                        </span>
                    </li>
                </ul>
                <p>Harap proposal dibuat dengan harga yang minimal sehingga tidak perlu tawar menawar lagi.</p>
                <p>Mohon proposal dapat saya terima segera, sebelum
                    <span class="font-bold">{{ formatTanggalIndonesia($penawaran->end_date, true) }}</span>
                </p>
                <p>Best Regards,
                <div class="font-bold">
                    <img src="{{ asset('storage/signatures/' . auth()->user()->id . '/' . auth()->user()->sign) }}"
                        alt="{{ auth()->user()->name }}" width="200" class="signature">
                    <p>
                        {{ auth()->user()->name }}
                    </p>
                </div>
                Sub Direktorat Appraisal
                </p>
                <p>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)</p>
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <style>
        .dark img.signature {
            filter: invert(1) brightness(2);
        }

        .dark img.pdf {
            filter: invert(1) brightness(1);
            mix-blend-mode: screen;
        }
    </style>
@endpush
