<div class="w-full rounded-lg shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">

        <h3 class="uppercase card-title">
            {{ $title ?? 'Form Inspeksi' }}
        </h3>

        <div class="flex gap-2 items-center">
            @if (isset($id) && isset($backLink))
                <a href="{{ route($backLink, ['id' => $id]) }}" class="btn btn-xs btn-info">
                    <i class="ki-filled ki-exit-left"></i> Back
                </a>
            @else
                @if (Auth::check() && Auth::user()->hasAnyRole(['administrator', 'senior-officer', 'EO Appraisal', 'DD Appraisal']) && request()->is('otorisator/*'))
                    @if (isset($permohonan))
                        <a href="{{ route('otorisator.show', ['id' => $permohonan->id, 'type' => request('header') ?? 'Pelaporan']) }}" class="btn btn-xs btn-info">
                            <i class="ki-filled ki-exit-left"></i> Back
                        </a>
                    @endif
                @elseif (Auth::check() && Auth::user()->hasAnyRole(['administrator', 'surveyor', 'penilai']))
                    @if (request()->is('surveyor/inspeksi/*/create') && request()->has('documentId') && request()->has('jenis_jaminan'))
                        @if (isset($permohonan))
                            <a href="{{ route('surveyor.show', $permohonan->id) }}" class="btn btn-xs btn-info">
                                <i class="ki-filled ki-exit-left"></i> Back
                            </a>
                        @endif
                    @elseif (request()->is('penilai/*/*'))
                        @if (isset($permohonan))
                            <a href="{{ route('penilai.show', $permohonan->id) }}" class="btn btn-xs btn-info">
                                <i class="ki-filled ki-exit-left"></i> Back
                            </a>
                        @endif
                    @endif
                @endif
            @endif
        </div>
    </div>

    <div class="card-body">
        <div class="grid grid-cols-2 gap-5">
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Tujuan Penilaian</label>
                <div class="flex flex-wrap items-baseline w-full">
                    @if (isset($permohonan->tujuanPenilaian))
                        <p class="text-gray-700 text-2sm">{{ $permohonan->tujuanPenilaian->name }}</p>
                    @endif
                </div>
            </div>
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Tanggal Survey</label>
                <div class="flex flex-wrap w-full items-base line">
                    <p class="text-gray-700 text-2sm">{{ optional($permohonan->created_at)->format('d/m/Y') }}</p>
                </div>
            </div>

            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Cab/Direktorat</label>
                <div class="flex flex-wrap items-baseline w-full">
                    @if (isset($permohonan->branch))
                        <p class="text-gray-700 text-2sm">{{ $permohonan->branch->name }}</p>
                    @endif
                </div>
            </div>

            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">AO</label>
                <div class="flex flex-wrap items-baseline w-full">
                    @if (isset($permohonan->user))
                        <p class="text-gray-700 text-2sm">{{ $permohonan->user->name }}</p>
                    @endif
                </div>
            </div>
        </div>

        <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap" style="margin-top: 20px">
            <label class="form-label max-w-56">Debitur/Perwakilan</label>

            @if (isset($forminspeksi['asset']['debitur_perwakilan']) && !empty($forminspeksi['asset']['debitur_perwakilan']))
                <div id="perwakilan" class="flex flex-wrap gap-5 items-baseline w-full">
                    @foreach ($forminspeksi['asset']['debitur_perwakilan'] as $key => $item)
                        <div class="flex flex-wrap gap-2 items-baseline w-full perwakilan">
                            <input type="text" class="input form-control" name="debitur_perwakilan[]"
                                value="{{ old('debitur_perwakilan.' . $key, $item) }}"
                                placeholder="Masukkan Debitur/Perwakilan" />
                            <button type="button" class="btn btn-danger btn-outline btn-xs remove-btn">Hapus</button>
                        </div>
                    @endforeach
                </div>
            @else
                <div id="perwakilan" class="flex flex-wrap gap-5 items-baseline w-full">
                    <div class="flex flex-wrap gap-2 items-baseline w-full perwakilan">
                        <input type="text" class="input form-control" name="debitur_perwakilan[]"
                            value="{{ old('debitur_perwakilan') }}" placeholder="Masukkan Debitur/Perwakilan" />
                        <button type="button" class="btn btn-danger btn-outline btn-xs remove-btn"
                            style="display: none">Hapus
                        </button>
                    </div>
                </div>
            @endif

            <button onclick="addClonableItem('perwakilan', 'perwakilan')" type="button" class="btn-md btn btn-primary">
                <i class="ki-filled ki-plus"></i>
            </button>
            <em id="error-debitur_perwakilan" class="text-sm alert text-danger"></em>
        </div>

        <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap" style="margin-top: 20px">
            <label class="form-label max-w-56">Pihak Bank (Selain Appraisal)</label>
            <div id="perwakilan" class="flex gap-5 items-baseline w-full">
                <div class="flex gap-4 items-center w-full">
                    <input class="input" type="text" placeholder="Masukan Pihak Bank (Selain Appraisal)" type="text"
                        name="pihak_bank"
                        value="{{ old('pihak_bank', isset($forminspeksi['asset']['pihak_bank']) ? $forminspeksi['asset']['pihak_bank'] : '') }}">
                </div>
            </div>
        </div>
    </div>
</div>

<div class="overflow-hidden w-full rounded-lg border shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">
        <h1 class="font-medium text-gray-900 uppercase text-md">Nama Cadeb/Debitur</h1>
    </div>
    <div class="card-body">
        <div class="grid gap-5">
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Nama</label>
                <div class="flex flex-wrap items-baseline w-full">
                    @if (isset($permohonan->debiture))
                        <p class="text-gray-700 text-2sm">{{ $permohonan->debiture->name }}</p>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<div class="w-full rounded-lg border shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">
        <h1 class="font-medium text-gray-900 uppercase text-md">Deskripsi Aset jaminan</h1>
    </div>
    <div class="card-body">
        <div class="grid gap-5 w-full">
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Jenis Aset</label>
                <div>
                    <div class="flex flex-wrap items-baseline w-full">
                        @if (isset($permohonan->documents))
                            @foreach ($permohonan->documents as $item)
                                <p class="text-gray-700 text-2sm">{{ $item->jenisJaminan->name }}</p>
                                <input type="hidden" name="jenis_asset_name" value="{{ $item->jenisJaminan->name }}">
                            @endforeach
                        @endif
                    </div>
                    <div class="flex-wrap items-stretch">
                        <div class="grid grid-cols-3 gap-4 mt-2 md:grid-cols-3">
                            <label class="flex gap-3 items-center form-label text-nowrap">
                                <input onclick="toggleFieldVisibility('jenis_asset', 'jenis_asset', ['tidak sesuai'])"
                                    type="radio" class="radio" name="jenis_asset" value="sesuai"
                                    {{ isset($forminspeksi['asset']['jenis_asset']['sesuai']) ? 'checked' : '' }}>
                                <span class="ml-2">Ya</span>
                            </label>
                            <label class="flex gap-2.5 items-center form-label text-nowrap">
                                <input onclick="toggleFieldVisibility('jenis_asset', 'jenis_asset', ['tidak sesuai'])"
                                    type="radio" class="radio" name="jenis_asset" value="tidak sesuai"
                                    {{ isset($forminspeksi['asset']['jenis_asset']['tidak sesuai']) ? 'checked' : '' }}>
                                <span class="ml-2">Tidak</span>
                            </label>
                            <!-- Select dropdown untuk "Tidak Sesuai" -->
                            @php
                                $statusKey = isset($forminspeksi['asset']['jenis_asset']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $selectedValue = $forminspeksi['asset']['jenis_asset'][$statusKey] ?? null;
                            @endphp

                            <div id="jenis_asset" class="flex gap-2 items-baseline"
                                style="{{ old('jenis_asset_tidak_sesuai', isset($forminspeksi['asset']['jenis_asset']['tidak sesuai'])) ? '' : 'display: none;' }}">
                                <select id="jenis_asset_tidak_sesuai" class="w-full input"
                                    name="jenis_asset_tidak_sesuai" onchange="handleJenisAssetChange(this)">
                                    <option value="">Pilih Jenis Aset</option>
                                    @if (isset($basicData['jenisJaminan']))
                                        @foreach ($basicData['jenisJaminan'] as $item)
                                            <option value="{{ $item->name }}"
                                                {{ old('jenis_asset_tidak_sesuai', $selectedValue) == $item->name ? 'selected' : '' }}>
                                                {{ old('jenis_asset_tidak_sesuai', $selectedValue) == $item->name ? 'selected' : '' }}>
                                                {{ $item->name }}
                                            </option>
                                        @endforeach
                                    @endif
                                </select>
                            </div>
                            <!-- Input untuk "Lainnya" -->
                            <input id="jenis_asset_lainnya" type="text" name="jenis_asset_lainnya"
                                class="mt-2 w-full input" placeholder="Masukkan Jenis Aset Lainnya..."
                                value="{{ old('jenis_asset_lainnya', isset($forminspeksi['asset']['jenis_asset']['Lainnya']) ? $forminspeksi['asset']['jenis_asset']['Lainnya'] : '') }}"
                                style="{{ old('jenis_asset_lainnya', isset($forminspeksi['asset']['jenis_asset']['Lainnya'])) == 'Lainnya' ? '' : 'display: none;' }}" />
                        </div>
                        <em id="error-jenis_asset" class="text-sm alert text-danger"></em>
                    </div>

                </div>
            </div>

            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Alamat</label>
                <div class="w-full">
                    @foreach ($permohonan->documents as $dokumen)
                        @php
                            $alamat = $dokumen;
                        @endphp
                    @endforeach
                    <span class="text-gray-700 text-2sm">
                        {{ formatAlamat($dokumen) }}
                    </span>

                    <div class="flex-wrap items-stretch">
                        <div class="grid grid-cols-3 gap-4 mt-2 md:grid-cols-3">
                            <label class="flex gap-3 items-center form-label text-nowrap">
                                <input onclick="createAlamat('sesuai')" type="radio" class="radio"
                                    name="alamat_sesuai" value="sesuai"
                                    {{ old('jenis_asset', isset($forminspeksi['asset']['alamat']['sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Ya</span>
                            </label>
                            <label class="flex gap-3 items-center form-label text-nowrap">
                                <input onclick="createAlamat('tidak sesuai')" type="radio" class="radio"
                                    name="alamat_sesuai" value="tidak sesuai"
                                    {{ old('jenis_asset', isset($forminspeksi['asset']['alamat']['tidak sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Tidak</span>
                            </label>
                        </div>
                        <em id="error-alamat_sesuai" class="text-sm alert text-danger"></em>
                    </div>

                    @php
                        $statusKey = isset($forminspeksi['asset']['alamat']['sesuai']) ? 'sesuai' : 'tidak sesuai';
                        $address = $forminspeksi['asset']['alamat'][$statusKey] ?? null;
                        $cekAlamat = $address ?? '';
                    @endphp

                    <div id="alamat_form" class="grid gap-2 mt-5" style="display: none;">

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="overflow-hidden w-full rounded-lg border shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">
        <h1 class="font-medium text-gray-900 uppercase text-md">Titik Koordinat Google Maps</h1>
    </div>
    <div class="card-body">
        <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
            <label class="form-label max-w-56">Koordinat</label>
            <div id="perwakilan" class="flex gap-5 items-baseline w-full">
                <div class="grid grid-cols-2 gap-4 items-center w-full">
                    <input class="input" type="text" placeholder="Masukan Koordinat Latitude" 
                        name="kordinat_lat" id="lat" pattern="^-?([1-8]?\d(\.\d+)?|90(\.0+)?)$"
                        value="{{ old('kordinat_lat', isset($forminspeksi['asset']['kordinat_lat']) ? $forminspeksi['asset']['kordinat_lat'] : '') }}">
                    <input class="input" type="text" placeholder="Masukan Koordinat Longitude"
                        name="kordinat_lng" id="lng" pattern="^-?(1[0-7]?\d(\.\d+)?|180(\.0+)?|[1-9]?\d(\.\d+)?)$"
                        value="{{ old('kordinat_lng', isset($forminspeksi['asset']['kordinat_lng']) ? $forminspeksi['asset']['kordinat_lng'] : '') }}">
                </div>
                <a onclick="getCoordinates()" type="button" class="btn btn-md btn-outline btn-primary">
                    <i class="ki-filled ki-map"></i>Ambil
                    Koordinat </a>
                <a target="_blank" href="https://www.google.com/maps" type="button"
                    class="btn btn-md btn-outline btn-success">
                    <i class="ki-filled ki-map"></i>Ke google Maps</a>
            </div>
        </div>
    </div>
</div>


<div class="overflow-hidden w-full rounded-lg border shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">
        <h1 class="font-medium text-gray-900 uppercase text-md">Status</h1>
    </div>
    <div class="w-full card-body">
        @include('lpj::component.detail-jaminan', ['status' => true])

        {{-- <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
            <label class="font-medium form-label max-w-56">Nomor NIB</label>
            <div class="flex flex-wrap items-baseline w-full">
                <input type="text" name="nomor_nib" class="w-full input"
                    value="{{ isset($forminspeksi['asset']['nomor_nib']) ? $forminspeksi['asset']['nomor_nib'] : '' }}"
                    placeholder="Masukkan Nomor NIB">
            </div>
        </div> --}}

        <div class="overflow-hidden mx-auto rounded-lg">
            <div class="py-4">
                <h1 class="font-medium text-gray-900 text-md">Hubungan cadeb/debitur dengan Pemilik Jaminan</h1>
            </div>

            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Pemilik Jaminan</label>
                <div>
                    <div class="flex flex-wrap items-baseline w-full">
                        @foreach ($permohonan->documents as $dokumen)
                            @php
                                $hubCadebPemilik = $dokumen->pemilik->hubungan_pemilik->name ?? 'N/A';
                            @endphp
                           <label class="form-label">
                                {{ $hubCadebPemilik }}
                            </label>

                            <input type="hidden" name="hub_cadeb_sesuai" value="{{ $hubCadebPemilik }}"
                                id="">
                        @endforeach
                    </div>

                    <div class="flex-wrap items-stretch">
                        <div class="grid grid-cols-3 gap-4 mt-2 md:grid-cols-3">
                            <label class="flex gap-3 items-center form-label text-nowrap">
                                <input
                                    onclick="toggleFieldVisibility('hub_cadeb','hub_cadeb_tidak_sesuai', ['tidak sesuai'])"
                                    type="radio" class="radio" name="hub_cadeb" value="sesuai"
                                    {{ old('hub_cadeb', isset($forminspeksi['asset']['hub_cadeb']['sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Ya</span>
                            </label>
                            <label class="flex gap-2.5 items-center form-label text-nowrap">
                                <input
                                    onclick="toggleFieldVisibility('hub_cadeb','hub_cadeb_tidak_sesuai', ['tidak sesuai'])"
                                    type="radio" class="radio" name="hub_cadeb" value="tidak sesuai"
                                    {{ old('hub_cadeb', isset($forminspeksi['asset']['hub_cadeb']['tidak sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Tidak</span>
                            </label>
                            <!-- Select dropdown untuk "Tidak Sesuai" -->
                            @php
                                $statusKey = isset($forminspeksi['asset']['hub_cadeb']['sesuai'])
                                    ? 'sesuai'
                                    : 'tidak sesuai';
                                $selectedData = $forminspeksi['asset']['hub_cadeb'][$statusKey] ?? null;
                            @endphp

                            <select id="hub_cadeb_tidak_sesuai" class="w-full input" name="hub_cadeb_tidak_sesuai"
                                style="{{ $statusKey != 'sesuai' ? '' : 'display: none;' }}">
                                <option value="">Select Hubungan Cadeb</option>
                                @if (isset($basicData['hubCadeb']))
                                    @foreach ($basicData['hubCadeb'] as $item)
                                        @if ($item->name != $hubCadebPemilik)
                                            <option value="{{ $item->name }}"
                                                {{ old('hub_cadeb_tidak_sesuai', $selectedData) == $item->name ? 'selected' : '' }}>
                                                {{ $item->name }}
                                            </option>
                                        @endif
                                    @endforeach
                                @endif
                            </select>

                        </div>
                        <em id="error-hub_cadeb" class="text-sm alert text-danger"></em>
                    </div>
                </div>
            </div>

        </div>

        <div class="overflow-hidden mx-auto rounded-lg">
            <div class="py-4">
                <h1 class="font-medium text-gray-900 text-md">Hubungan Cadeb/Debitur dengan Penghuni Jaminan</h1>
                <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                    <label class="form-label max-w-56">Penghuni Jaminan</label>

                    <div class="mt-2">
                        <div class="flex flex-wrap items-baseline w-full text-sm">
                            <input type="hidden" name="hub_cadeb_penghuni_sesuai" value="ditempati sendiri"
                                id="">
                        </div>

                        <div class="flex-wrap items-stretch">
                            <div class="grid grid-cols-3 gap-4 mt-2 md:grid-cols-3">
                                <label class="flex gap-3 items-center form-label text-nowrap">

                                    <input
                                        onclick="toggleFieldVisibility('hub_cadeb_penghuni','hub_penghuni_tidak_sesuai', ['tidak sesuai'])"
                                        type="radio" class="radio" name="hub_cadeb_penghuni" value="sesuai"
                                        {{ old('hub_cadeb_penghuni', isset($forminspeksi['asset']['hub_cadeb_penghuni'])) ? 'checked' : '' }}>

                                    <span class="ml-2">Ditempati Sendiri</span>
                                </label>
                                <label class="flex gap-2.5 items-center form-label text-nowrap">
                                    <input
                                        onclick="toggleFieldVisibility('hub_cadeb_penghuni','hub_penghuni_tidak_sesuai', ['tidak sesuai'])"
                                        type="radio" class="radio" name="hub_cadeb_penghuni" value="tidak sesuai"
                                        {{ old('hub_cadeb_penghuni', $forminspeksi['asset']['hub_cadeb_penghuni']['tidak sesuai'] ?? false) ? 'checked' : '' }}>
                                    <span class="ml-2">Tidak</span>
                                </label>
                                <!-- Select dropdown untuk "Tidak Sesuai" -->
                                @php
                                    $statusKey = isset($forminspeksi['asset']['hub_cadeb_penghuni']['sesuai'])
                                        ? 'sesuai'
                                        : 'tidak sesuai';
                                    $selectedData = $forminspeksi['asset']['hub_cadeb_penghuni'][$statusKey] ?? null;
                                @endphp


                                <input id="hub_penghuni_tidak_sesuai" class="input" name="hub_penghuni_tidak_sesuai"
                                    placeholder="Masukkan Hubungan penghuni jaminan"
                                    value="{{ old('hub_penghuni_tidak_sesuai', isset($selectedData) ? $selectedData : '') }}"
                                    style="{{ $statusKey != 'sesuai' ? '' : 'display: none;' }}">
                            </div>
                            <em id="error-hub_cadeb_penghuni" class="text-sm alert text-danger"></em>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    function getCoordinates() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(
                (position) => {
                    // Ambil latitude dan longitude
                    const lat = position.coords.latitude;
                    const lng = position.coords.longitude;

                    // Masukkan ke dalam input field
                    document.getElementById('lat').value = lat;
                    document.getElementById('lng').value = lng;
                    toastrSuccessBuild('Koordinat berhasil diambil!');

                },
                (error) => {
                    // Handle error
                    switch (error.code) {
                        case error.PERMISSION_DENIED:
                            toastrErrorBuild('Pengguna menolak permintaan geolokasi.');
                            break;
                        case error.POSITION_UNAVAILABLE:
                            toastrErrorBuild('Informasi lokasi tidak tersedia.');
                            break;
                        case error.TIMEOUT:
                            toastrErrorBuild('Permintaan geolokasi mengalami timeout.');
                            break;
                        case error.UNKNOWN_ERROR:
                            toastrErrorBuild('Terjadi kesalahan yang tidak diketahui.');
                            break;
                    }
                }
            );
        } else {
            toastrErrorBuild('Geolocation tidak didukung oleh browser ini.');
        }
    }

    document.addEventListener('DOMContentLoaded', function() {
        // Cek radio button saat halaman dimuat
        const sesuaiRadio = document.querySelector('input[name="alamat_sesuai"][value="sesuai"]');
        const tidakSesuaiRadio = document.querySelector('input[name="alamat_sesuai"][value="tidak sesuai"]');

        // Jika "Ya" sudah tercentang saat load halaman
        if (sesuaiRadio.checked) {
            createAlamat('sesuai');
        }

        // Jika "Tidak" sudah tercentang saat load halaman
        if (tidakSesuaiRadio.checked) {
            createAlamat('tidak sesuai');
        }
    });

    function createAlamat(data) {
        const createElementAlamat = document.getElementById('alamat_form');


        if (data == 'sesuai') {
            createElementAlamat.innerHTML = `
        <div class="grid gap-2.5 w-full" >
            <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                <label for="address" class="form-label max-w-56">Terletak di.</label>
                <div class="flex flex-wrap items-baseline w-full">
                    <input type="text" id="address" name="address" class="w-full input"
                        value="{{ isset($alamat->address) ? $alamat->address : old('address') }}">
                </div>
            </div>
            <input type="hidden" name="province_code"
                value="{{ isset($alamat->province) ? $alamat->province->code : '' }}">
            <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                <label for="province" class="form-label max-w-56">Provinsi</label>
                <div class="flex flex-wrap items-baseline w-full">
                    <input type="text" id="province" class="w-full cursor-not-allowed input" readonly
                        value="{{ isset($alamat->province) ? $alamat->province->name : '' }}">
                </div>
            </div>
            <input type="hidden" name="city_code"
                value="{{ isset($alamat->city) ? $alamat->city->code : '' }}">
            <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                <label for="city" class="form-label max-w-56">Kabupaten/Kota</label>
                <div class="flex flex-wrap items-baseline w-full">
                    <input type="text" id="city" class="w-full cursor-not-allowed input" readonly
                        value="{{ isset($alamat->city) ? $alamat->city->name : '' }}">
                </div>
            </div>
            <input type="hidden" name="district_code"
                value="{{ isset($alamat->district) ? $alamat->district->code : '' }}">
            <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                <label for="district" class="form-label max-w-56">Kecamatan</label>
                <div class="flex flex-wrap items-baseline w-full">
                    <input type="text" id="district" class="w-full cursor-not-allowed input" readonly
                        value="{{ isset($alamat->district) ? $alamat->district->name : '' }}">
                </div>
            </div>
            <input type="hidden" name="village_code"
                value="{{ isset($alamat->village) ? $alamat->village->code : '' }}">
            <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                <label for="village" class="form-label max-w-56">Desa/Kelurahan</label>
                <div class="flex flex-wrap items-baseline w-full">
                    <input type="text" id="village" class="w-full cursor-not-allowed input" readonly
                        value="{{ isset($alamat->village) ? $alamat->village->name : '' }}">
                </div>
            </div>
        </div>
        `;
        }

        if (data === 'tidak sesuai') {
            createElementAlamat.style.display = 'block';
            createElementAlamat.innerHTML = `
            <div class="grid gap-2.5 w-full">
                <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                    <label for="address" class="form-label max-w-56">Terletak di.</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <input type="text" id="address" name="address" class="w-full input"
                            placeholder="Masukkan Jl." value="{{ old('address', $cekAlamat['address'] ?? '') }}">
                    </div>
                </div>

                <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                    <label for="province_code" class="form-label max-w-56">Provinsi</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <select id="province_code" name="province_code" class="w-full input" onchange="getCity(this.value)">
                            <option value="">Select Province</option>
                            @foreach ($provinces as $province)
                                <option value="{{ $province->code }}"
                                    @if (
                                        (isset($cekAlamat['province_code']) && $cekAlamat['province_code'] == $province->code) ||
                                            (!isset($cekAlamat['province_code']) &&
                                                isset($debitur->province_code) &&
                                                $debitur->province_code == $province->code)) selected @endif>
                                    {{ $province->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                </div>

                <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                    <label for="city_code" class="form-label max-w-56">Kota/Kabupaten</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <select id="city_code" name="city_code" class="w-full select" onchange="getDistrict(this.value)">
                            <option value="">Pilih Kota/Kabupaten</option>
                        </select>
                    </div>
                </div>

                <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                    <label for="district_code" class="form-label max-w-56">Kecamatan</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <select id="district_code" name="district_code" class="w-full select" onchange="getVillage(this.value)">
                            <option value="">Pilih Kecamatan</option>
                        </select>
                    </div>
                </div>

                <div class="flex flex-wrap gap-2.5 items-baseline w-full lg:flex-nowrap">
                    <label for="village_code" class="form-label max-w-56">Desa/Kelurahan</label>
                    <div class="flex flex-wrap items-baseline w-full">
                        <select id="village_code" name="village_code" class="w-full select">
                            <option value="">Pilih Kelurahan</option>
                        </select>
                    </div>
                </div>
            </div>
        `;
            loadSavedLocationData();
        }
    }



    document.addEventListener('DOMContentLoaded', () => {
        loadSavedLocationData();
    });

    function handleJenisAssetChange(select) {
        const lainnyaInput = document.getElementById('jenis_asset_lainnya');
        if (select.value === 'Lainnya') {
            lainnyaInput.style.display = 'block';
        } else {
            lainnyaInput.style.display = 'none';
            lainnyaInput.value = ''; // Reset nilai input jika bukan "lainnya"
        }
    }
</script>
@include('lpj::surveyor.js.utils')
