<div class="card-header bg-agi-50">
    <h3 class="card-title">Data Pembanding</h3>
    <div class="flex gap-2 items-center">
        @if (Request::is('penilai/show-data-pembanding/*/edit') && request()->has('documentId') && request()->has('jenis_jaminan'))
            <a href="{{ url()->previous() }}" class="btn btn-xs btn-info">
                <i class="ki-filled ki-exit-left"></i> Back
            </a>
        @else
            <a href="{{ route('surveyor.show', ['id' => $permohonan->id]) }}" class="btn btn-xs btn-info">
                <i class="ki-filled ki-exit-left"></i> Back
            </a>
        @endif

    </div>
</div>

<div class="flex flex-wrap justify-end py-5 card-header bg-agi-50">
    <button type="button" id="addColumnBtn" class="btn btn-primary btn-sm">
        <i class="ki-filled ki-plus"></i> Tambah Pembanding
    </button>
    <button type="button" id="removeColumnBtn" class="ml-2 btn btn-danger btn-sm" style="display: none;">
        <i class="ki-filled ki-minus"></i> Hapus Pembanding
    </button>
</div>

{{-- @php
        print_r($inspectionData);
    @endphp --}}

<div class="card-body">
    <div class="scrollable-x-auto">
        <table id="dataTable" class="table text-sm font-medium text-gray-700 align-middle table-auto table-border">
            <thead class="sticky top-0 bg-gray-50">
                <tr>
                    <th class="px-4 py-3 min-w-[200px]">Parameter</th>
                    <th class="px-4 py-3 min-w-[250px]">Objek Penilaian</th>
                    <th class="px-4 py-3 min-w-[250px]">Data Pembanding 1</th>
                </tr>
            </thead>
            <tbody>
                <!-- Informasi Dasar -->
                <tr class="bg-gray-100">
                    <td colspan="3" class="px-4 py-2 font-semibold">Informasi Dasar</td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Foto</td>
                    <td class="px-4 py-2">
                        <div class="flex flex-col gap-2">

                            @php
                                $photos = $fotoForm['upload_foto'] ?? [];
                                if (!is_array($photos)) {
                                    $photos = [];
                                }

                                $groupedPhotos = collect($photos)->groupBy('category');
                                $mainPhoto = $groupedPhotos->get('Tampak Depan', collect())->first();

                                // echo $mainPhoto;

                            @endphp

                            @if ($mainPhoto)
                                <img id="uploadedImage1" src="{{ asset('storage/' . $mainPhoto['path']) }}"
                                    class="max-w-[200px]" alt="Uploaded Image">
                            @endif
                            <input type="hidden" name="name_foto_objek" class="input"
                                value="{{ $mainPhoto['path'] ?? '' }}">
                            <input type="file" name="foto_objek" class="file-input" accept=".jpg,.jpeg,.png"
                                onchange="previewImage(this, 'uploadedImage1')">

                        </div>
                    </td>

                    <td class="px-4 py-2">
                        <div class="flex flex-col gap-2">
                            <img id="uploadedImage2" class="max-w-[200px] hidden" alt="Pembanding Image">
                            <input type="file" name="foto_objek_pembanding[]" class="file-input" accept=".jpg,.jpeg,.png"
                                onchange="previewImage(this, 'uploadedImage2')">
                        </div>
                    </td>
                </tr>



                <!-- Data Properti -->
                <tr class="bg-gray-100">
                    <td colspan="3" class="px-4 py-2 font-semibold">Data Properti</td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Jenis Aset</td>
                    <td class="px-4 py-2">
                        <select name="jenis_aset" class="select">
                            <option value="">Pilih Jenis Aset</option>
                            @php
                                $selectedKey =
                                    old('jenis_aset') ??
                                    ($inspectionData['asset']['jenis_asset']['sesuai'] ??
                                        ($inspectionData['asset']['jenis_asset']['tidak sesuai'] ?? ''));
                            @endphp

                            @foreach ($data['jenisJaminan'] as $item)
                                <option value="{{ $item->name }}" {{ $selectedKey == $item->name ? 'selected' : '' }}>
                                    {{ $item->name }}
                                </option>
                            @endforeach


                        </select>
                    </td>
                    <td class="px-4 py-2">
                        <select name="jenis_aset_pembanding[]" class="select">
                            <option value="">Pilih Jenis Aset</option>
                            @foreach ($data['jenisJaminan'] as $item)
                                <option value="{{ $item->name }}"
                                    {{ ($inspectionData['jenis_aset'] ?? '') == $item->name ? 'selected' : '' }}>
                                    {{ $item->name }}</option>
                            @endforeach
                        </select>
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Luas Tanah (m²)</td>
                    @php
                        $cekLuas =
                            isset($inspectionData['tanah']['luas_tanah']) == 'sesuai' ? 'sesuai' : 'tidak sesuai';

                    @endphp
                    <td class="px-4 py-2">
                        <input type="text" name="luas_tanah" class="input"
                            value="{{ $inspectionData['tanah']['luas_tanah']['sesuai'] ?? ($inspectionData['tanah']['luas_tanah']['tidak sesuai'] ?? '') }}">
                    </td>
                    <td class="px-4 py-2">

                        <input type="text" name="luas_tanah_pembanding[]" class="input">
                    </td>
                </tr>

                @php
                    $luasTanahData = [];
                    $nilai = 0;

                    // Memeriksa apakah key 'bangunan' ada di dalam $inspectionData
                    if (isset($inspectionData['bangunan']['luas_tanah_bagunan'])) {
                        $luasTanahData = $inspectionData['bangunan']['luas_tanah_bagunan'];

                        if (!empty($luasTanahData['sesuai'])) {
                            $nilai = $luasTanahData['sesuai'];
                        } elseif (!empty($luasTanahData['tidak sesuai'])) {
                            $nilai = $luasTanahData['tidak sesuai'];
                        }
                    }
                @endphp

                <tr>
                    <td class="px-4 py-2">Luas Bangunan (m²)</td>
                    <td class="px-4 py-2">
                        <input type="text" name="luas_tanah_bagunan" class="input"
                            value="{{ $inspectionData['bangunan']['luas_tanah_bagunan']['sesuai'] ?? ($inspectionData['bangunan']['luas_tanah_bagunan']['tidak sesuai'] ?? '') }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="luas_bangunan_pembanding[]" class="input">
                    </td>
                </tr>



                <!-- Informasi Harga -->
                <tr class="bg-gray-100">
                    <td colspan="3" class="px-4 py-2 font-semibold">Informasi</td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Hak atas properti yang dialihkan</td>
                    <td class="px-4 py-2">
                        <input type="text" name="hak_properti" class="input"
                            value="{{ $inspectionData['asset']['hak_properti'] ?? '' }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="hak_properti_pembanding[]" class="input">
                    </td>
                </tr>
                {{-- <tr> --}}
                {{-- <td class="px-4 py-2">Penawaran/ Transaksi</td>
                    <td class="px-4 py-2">
                        <input type="text" name="penawaran" class="input"
                            value="{{ $inspectionData['asset']['penawaran'] ?? '' }}"> --}}
                {{-- <select class="w-full input" name="penawaran">
                            <option value="">Pilih</option>
                            <option value="Penawaran" {{ (isset($inspectionData['asset']['penawaran']) && $inspectionData['asset']['penawaran'] == 'Penawaran') ? 'selected' : '' }}>Penawaran</option>
                            <option value="Transaksi" {{ (isset($inspectionData['asset']['penawaran']) && $inspectionData['asset']['penawaran'] == 'Transaksi') ? 'selected' : '' }}>Transaksi</option>
                        </select> --}}
                {{-- </td>
                    <td class="px-4 py-2">
                        <input type="text" name="penawaran_pembanding[]" class="input"> --}}
                {{-- <select class="w-full input" name="penawaran_pembanding[]">
                            <option value="">Pilih</option>
                            <option value="Penawaran" {{ (isset($inspectionData['penawaran']) && $inspectionData['penawaran'] == 'Penawaran') ? 'selected' : '' }}>Penawaran</option>
                            <option value="Transaksi" {{ (isset($inspectionData['penawaran']) && $inspectionData['penawaran'] == 'Transaksi') ? 'selected' : '' }}>Transaksi</option>
                        </select> --}}

                {{-- </td>
                </tr> --}}

                {{-- <tr>
                    <td class="px-4 py-2">Harga Penawaran/ Transaksi(Rp)</td>
                    <td class="px-4 py-2">
                        <input type="text" name="harga_penawaran" class="input currency-format"
                            value="{{ $inspectionData['asset']['harga_penawaran'] ?? '' }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="harga_penawaran_pembanding[]" class="input currency-format">
                    </td>
                </tr> --}}


                <tr>
                    <td class="px-4 py-2">Telepon Contact Person</td>
                    <td class="px-4 py-2">
                        <input type="text" name="telepon" class="input"
                            value="{{ $inspectionData['asset']['telepon'] ?? ($permohonan->debiture->phone ?? '') }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="telepon_pembanding[]" class="input">
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Status Narasumber</td>
                    <td class="px-4 py-2">
                        <input type="text" name="status_nara_sumber" class="input"
                            value="{{ $inspectionData['asset']['status_nara_sumber'] ?? ($dokumen->pemilik->hubungan_pemilik->name ?? '') }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="status_nara_sumber_pembanding[]" class="input">
                    </td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Nama Narasumber</td>
                    <td class="px-4 py-2">
                        <input type="text" name="nama_nara_sumber" class="input"
                            value="{{ $inspectionData['asset']['nama_nara_sumber'] ?? ($permohonan->debiture->name ?? '') }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="nama_nara_sumber_pembanding[]" class="input">
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Tanggal Data</td>
                    <td class="px-4 py-2">
                        <input type="date" name="tanggal" class="input"
                            value="{{ $inspectionData['asset']['tanggal'] ?? '' }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="date" name="tanggal_pembanding[]" class="input">
                    </td>
                </tr>

                <!-- Lokasi -->
                <tr class="bg-gray-100">
                    <td colspan="3" class="px-4 py-2 font-semibold">Lokasi</td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Koordinat</td>
                    <td class="px-4 py-2">
                        <div class="grid grid-cols-2 gap-2">
                            <input type="text" name="kordinat_lat" class="input" placeholder="Latitude"
                                value="{{ $inspectionData['asset']['kordinat_lat'] ?? '' }}" pattern="^-?([1-8]?\d(\.\d+)?|90(\.0+)?)$">
                            <input type="text" name="kordinat_lng" class="input" placeholder="Longitude"
                                value="{{ $inspectionData['asset']['kordinat_lng'] ?? '' }}" pattern="^-?(1[0-7]?\d(\.\d+)?|180(\.0+)?|[1-9]?\d(\.\d+)?)$">
                        </div>
                    </td>
                    <td class="px-4 py-2">
                        <div class="grid grid-cols-2 gap-2">
                            <input type="text" name="kordinat_lat_pembanding[]" class="input"
                                placeholder="Latitude" pattern="^-?([1-8]?\d(\.\d+)?|90(\.0+)?)$">
                            <input type="text" name="kordinat_lng_pembanding[]" class="input"
                                placeholder="Longitude" pattern="^-?(1[0-7]?\d(\.\d+)?|180(\.0+)?|[1-9]?\d(\.\d+)?)$">
                        </div>
                    </td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Alamat</td>
                    <td class="px-4 py-2">
                        @php
                            $statusAlamat = isset($inspectionData['asset']['alamat']['sesuai'])
                                ? 'sesuai'
                                : 'tidak sesuai';
                            $address = $inspectionData['asset']['alamat'][$statusAlamat]['address'] ?? null;
                        @endphp
                        <textarea name="address" class="py-2 input" rows="2">{{ $address }}</textarea>
                    </td>
                    <td class="px-4 py-2">
                        <textarea name="address_pembanding[]" class="py-2 input" rows="2"></textarea>
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Provinsi</td>
                    <td class="px-4 py-2">
                        <select id="province_code" name="province_code" class="w-full input">
                            @php
                                $statusKey = isset($inspectionData['asset']['alamat']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $address = $inspectionData['asset']['alamat'][$statusKey]['province_code'] ?? null;
                            @endphp
                            <option value="">Select Province</option>
                            @foreach ($provinces as $province)
                                @php
                                    $statusKey = isset($inspectionData['asset']['alamat']['sesuai'])
                                        ? 'sesuai'
                                        : 'tidak sesuai';
                                    $selectedProvince =
                                        $inspectionData['asset']['alamat'][$statusKey]['province_code'] ?? null;
                                @endphp

                                <option value="{{ $province->code }}"
                                    {{ $selectedProvince == $province->code ? 'selected' : '' }}>
                                    {{ $province->name }}
                                </option>
                            @endforeach
                        </select>
                    </td>
                    <td class="px-4 py-2">
                        <select id="province_code_pembanding" name="province_code_pembanding[]"
                            onchange="handleProvinceChange(this)" class="w-full input">
                            <option value="">Pilih Provinsi</option>
                            @foreach ($provinces as $province)
                                <option value="{{ $province->code }}">{{ $province->name }}
                                </option>
                            @endforeach
                        </select>
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Kabupaten/Kota</td>
                    <td class="px-4 py-2">
                        <select id="city_code" name="city_code" class="w-full select">
                            @php

                                $statusKey = isset($inspectionData['asset']['alamat']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $selectedCity = $inspectionData['asset']['alamat'][$statusKey]['city_code'] ?? null;
                            @endphp
                            <option value="">Pilih Kota/Kabupaten</option>
                            @if (isset($selectedCity))
                                @foreach ($cities as $city)
                                    <option value="{{ $city->code }}"
                                        {{ $selectedCity == $city->code ? 'selected' : '' }}>
                                        {{ $city->name }}
                                    </option>
                                @endforeach
                            @endif
                        </select>
                    </td>
                    <td class="px-4 py-2">
                        <select id="city_code_pembanding" name="city_code_pembanding[]"
                            onchange="handleCityChange(this)" class="w-full input">
                            <option value="">Pilih Kota/Kabupaten</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Kecamatan</td>
                    <td class="px-4 py-2">
                        <select id="district_code" name="district_code" class="w-full select">
                            @php

                                $statusKey = isset($inspectionData['asset']['alamat']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $selectedDisrict =
                                    $inspectionData['asset']['alamat'][$statusKey]['district_code'] ?? null;
                            @endphp
                            <option value="">Pilih Kecamatan</option>
                            @if (isset($selectedDisrict))
                                @foreach ($districts as $district)
                                    <option value="{{ $district->code }}"
                                        {{ $selectedDisrict == $district->code ? 'selected' : '' }}>
                                        {{ $district->name }}
                                    </option>
                                @endforeach
                            @endif
                        </select>
                    </td>
                    <td class="px-4 py-2">

                        <select id="district_code_pembanding" name="district_code_pembanding[]"
                            onchange="handleDistrictChange(this)" class="w-full input">
                            <option value="">Pilih Kecamatan</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Desa/Kelurahan</td>
                    <td class="px-4 py-2">
                        <select id="village_code" name="village_code" class="w-full select">
                            @php
                                $statusKey = isset($inspectionData['asset']['alamat']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $selectedDesa = $inspectionData['asset']['alamat'][$statusKey]['village_code'] ?? null;
                            @endphp
                            <option value="">Pilih Kecamatan</option>
                            @if (isset($selectedDesa))
                                @foreach ($villages as $village)
                                    <option value="{{ $village->code }}"
                                        {{ $selectedDesa == $village->code ? 'selected' : '' }}>
                                        {{ $village->name }}
                                    </option>
                                @endforeach
                            @endif
                        </select>
                    </td>
                    <td class="px-4 py-2">
                        <select id="village_code_pembanding" name="village_code_pembanding[]" class="w-full input">
                            <option value="">Pilih Desa/Kelurahan</option>
                        </select>
                    </td>
                </tr>

                <tr class="bg-gray-100">
                    <td colspan="3" class="px-4 py-2 font-semibold">Harga</td>
                </tr>

                <tr>
                    <td class="px-4 py-2">Harga Penawaran/ Transaksi(Rp)</td>
                    <td class="px-4 py-2">
                        <input type="text" name="harga" class="input currency-format"
                            value="{{ $inspectionData['asset']['harga'] ?? '' }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="harga_pembanding[]" class="input currency-format">
                    </td>
                </tr>
                <tr>
                    <td class="px-4 py-2">Diskon</td>
                    <td class="px-4 py-2">
                        <div class="input">
                            <input type="text" name="diskon" class="currency"
                                value="{{ $inspectionData['asset']['diskon'] ?? '' }}">
                            <span class="btn btn-icon">
                                <i class="ki-outline ki-percentage"></i>
                            </span>
                        </div>

                    </td>
                    <td class="px-4 py-2">
                        <div class="input">
                            <input type="text" name="diskon_pembanding[]" class="currency">
                            <span class="btn btn-icon">
                                <i class="ki-outline ki-percentage"></i>
                                </i>
                            </span>
                        </div>

                    </td>
                </tr>
                {{-- <tr style="">
                    <td class="px-4 py-2">Harga Setelah Diskon</td>
                    <td class="px-4 py-2">
                        <input type="text" name="total" class="input currency"
                            value="{{ $inspectionData['asset']['total'] ?? '' }}" readonly>
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="total_pembanding[]" class="input currency-format" readonly>
                    </td>
                </tr> --}}
                <tr>
                    <td class="px-4 py-2">Harga Setelah Diskon</td>
                    <td class="px-4 py-2">
                        <input type="text" name="total" class="input currency-format" readonly
                            value="{{ $inspectionData['asset']['total'] ?? '' }}">
                    </td>
                    <td class="px-4 py-2">
                        <input type="text" name="total_pembanding[]" readonly class="input currency-format">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
