<div class="overflow-hidden w-full rounded-lg border shadow-md card border-agi-100">
    <div class="card-header light:bg-agi-50">
        <h3 class="uppercase card-title">
            Analisa Tanah
        </h3>
    </div>

    <div class="card-body">
        <div class="grid gap-5">
            <!-- Luas tanah -->
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <label class="form-label max-w-56">Luas Tanah</label>
                <div class="mt-2">

                    @if (isset($permohonan->documents))
                        @foreach ($permohonan->documents as $item)
                            @php
                                $total_luas_tanah = 0;
                                $jenis_legalitas_jaminan_id = 0;
                                if (isset($item->detail)) {
                                    $total_luas_tanah = calculateTotalLuas($item->detail, 'luas_tanah', 1, 27, 3);
                                }
                            @endphp
                            <input type="hidden" id="jenis_legalistas_jaminan_tanah_id"
                                name="jenis_legalistas_jaminan_tanah_id" class="input"
                                value="{{ $jenis_legalitas_jaminan_id }}">
                            <input type="hidden" name="luas_tanah_sesuai" class="input"
                                value="{{ $total_luas_tanah }}">
                            <p class="text-gray-700 text-2sm">{{ $total_luas_tanah }} m<sup>2</sup></p>
                        @endforeach
                    @endif

                    <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 type="radio" class="radio" name="luas_tanah" value="sesuai"
                                    onclick="toggleFieldVisibility('luas_tanah', 'luas_tanah_tidak_sesuai', ['tidak sesuai'])"
                                    {{ old('luas_tanah', isset($forminspeksi['tanah']['luas_tanah']['sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Sesuai</span>
                            </label>
                            <label class="flex gap-2.5 items-center form-label text-nowrap">
                                <input type="radio" class="radio" name="luas_tanah" value="tidak sesuai"
                                    onclick="toggleFieldVisibility('luas_tanah', 'luas_tanah_tidak_sesuai', ['tidak sesuai'])"
                                    {{ old('luas_tanah', isset($forminspeksi['tanah']['luas_tanah']['tidak sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Tidak Sesuai</span>
                            </label>
                            <div id="luas_tanah_tidak_sesuai" class="flex gap-2 items-baseline"
                                style="{{ old('luas_tanah', isset($forminspeksi['tanah']['luas_tanah']['tidak sesuai'])) ? '' : 'display: none;' }}">
                                <div class="input">
                                    <input id="analisa_tanah_tidak_sesuai" type="text" name="luas_tanah_tidak_sesuai"
                                        class="w-full number-format" placeholder="Masukan Luas Tanah"
                                        value="{{ old('luas_tanah_tidak_sesuai', $forminspeksi['tanah']['luas_tanah']['tidak sesuai'] ?? '') }}">

                                </div>
                                {{-- <button type="button" class="btn btn-md btn-primary"
                                    onclick="updateAnalisa('analisa_tanah')">Save
                                </button> --}}
                            </div>
                        </div>
                        <em id="error-luas_tanah" class="text-sm alert text-danger"></em>
                    </div>
                </div>
            </div>

            <!-- Hadap Mata Angin -->
            <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                <input type="hidden" name="hadap_mata_angin_sesuai" class="input" value="sesuai">
                <label class="form-label max-w-56">Hadap Mata Angin</label>
                <div class="mt-2">
                    <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 type="radio" class="radio" name="hadap_mata_angin" value="sesuai"
                                    onclick="toggleFieldVisibility('hadap_mata_angin', 'hadap_mata_angin_tidak_sesuai' ,['tidak sesuai'])"
                                    {{ old('hadap_mata_angin', isset($forminspeksi['tanah']['hadap_mata_angin']['sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Sesuai</span>
                            </label>

                            <label class="flex gap-2.5 items-center form-label text-nowrap">
                                <input type="radio" class="radio" name="hadap_mata_angin" value="tidak sesuai"
                                    onclick="toggleFieldVisibility('hadap_mata_angin', 'hadap_mata_angin_tidak_sesuai' ,['tidak sesuai'])"
                                    {{ old('hadap_mata_angin', isset($forminspeksi['tanah']['hadap_mata_angin']['tidak sesuai'])) ? 'checked' : '' }}>
                                <span class="ml-2">Tidak Sesuai</span>
                            </label>

                            <!-- Select dropdown untuk "Tidak Sesuai" -->
                            <div id="hadap_mata_angin_tidak_sesuai" class="flex gap-2 items-baseline"
                                style="{{ old('hadap_mata_angin', isset($forminspeksi['tanah']['hadap_mata_angin']['tidak sesuai'])) ? '' : 'display: none;' }}">
                                <select class="input w-full
                                id="
                                    hadap_mata_angin_tidak_sesuai" name="hadap_mata_angin_tidak_sesuai">
                                    @php
                                        $statusKey = isset($forminspeksi['tanah']['hadap_mata_angin']['sesuai'])
                                            ? 'sesuai'
                                            : 'tidak sesuai';
                                        $selectedData = $forminspeksi['tanah']['hadap_mata_angin'][$statusKey] ?? null;
                                    @endphp
                                    <option value="">Select Hadap Mata Angin</option>
                                    @if (isset($basicData['arahMataAngin']))
                                        @foreach ($basicData['arahMataAngin'] as $item)
                                            <option value="{{ $item->name }}"
                                                {{ old('hadap_mata_angin_tidak_sesuai', $selectedData ?? '') == $item->name ? 'selected' : '' }}>
                                                {{ $item->name }}
                                            </option>
                                        @endforeach
                                    @endif
                                </select>
                                {{-- <button type="button" class="btn btn-md btn-primary" onclick="updateAnalisa('hadap_mata_angin')">Save</button> --}}
                            </div>
                        </div>
                        <em id="error-hadap_mata_angin" class="text-sm alert text-danger"></em>
                    </div>
                </div>
            </div>

            <!-- Bentuk Tanah -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Bentuk Tanah</label>
                <div class="flex-wrap items-stretch">
                    <div class="flex flex-col gap-4 items-start">
                        @if (isset($basicData['bentukTanah']))
                            @foreach ($basicData['bentukTanah'] as $item)
                                <div class="flex items-center">
                                    <label class="flex gap-2.5 items-center form-label text-nowrap">
                                        <input class="checkbox" name="bentuk_tanah[]" type="checkbox"
                                            value="{{ $item->name }}"
                                            {{ in_array($item->name, old('bentuk_tanah', [])) ||
                                            (isset($forminspeksi['tanah']['bentuk_tanah']['bentuk_tanah']) &&
                                                is_array($forminspeksi['tanah']['bentuk_tanah']['bentuk_tanah']) &&
                                                in_array($item->name, $forminspeksi['tanah']['bentuk_tanah']['bentuk_tanah']))
                                                ? 'checked'
                                                : '' }}
                                            onclick="toggleCheckboxVisibility('bentuk_tanah', 'bentuk_tanah_lainnya', ['lainnya'])" />
                                        {{ $item->name }}
                                    </label>
                                    @if (strcasecmp($item->name, 'lainnya') == 0)
                                        <input id="bentuk_tanah_lainnya" type="text"
                                            style="{{ isset($forminspeksi['tanah']['bentuk_tanah']['lainnya']) && $forminspeksi['tanah']['bentuk_tanah']['lainnya'] ? '' : 'display: none;' }}"
                                            name="bentuk_tanah_lainnya" class="mt-2 w-full input"
                                            placeholder="Masukkan bentuk tanah..."
                                            value="{{ old('bentuk_tanah_lainnya', $forminspeksi['tanah']['bentuk_tanah']['lainnya'] ?? '') }}" />
                                    @endif
                                </div>
                            @endforeach
                        @endif
                    </div>
                    <em id="error-bentuk_tanah" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Kontur Tanah</label>
                <div class="flex-wrap items-stretch">
                    <div class="flex flex-col gap-4 items-start">
                        @if (isset($basicData['konturTanah']))
                            @foreach ($basicData['konturTanah'] as $item)
                                <label class="flex gap-2.5 items-center form-label text-nowrap">
                                    <input class="checkbox" name="kontur_tanah[]" type="checkbox"
                                        value="{{ $item->name }}"
                                        {{ in_array($item->name, old('kontur_tanah', $forminspeksi['tanah']['kontur_tanah'] ?? [])) ? 'checked' : '' }} />
                                    {{ $item->name }}
                                </label>
                            @endforeach
                        @endif
                    </div>
                    <em id="error-kontur_tanah" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <!-- Beda Ketinggian Dengan Jalan -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Beda Ketinggian Dengan Jalan</label>
                <div class="flex-wrap items-stretch">
                    <div class="flex flex-col gap-4 items-start">
                        @if (isset($basicData['ketinggianTanah']))
                            @foreach ($basicData['ketinggianTanah'] as $item)
                                <div class="flex flex-col gap-2">
                                    <label class="flex gap-2.5 items-center form-label text-nowrap">
                                        <input class="checkbox" name="ketinggian_jalan[]" type="checkbox"
                                            value="{{ $item->name }}"
                                            {{ in_array($item->name, old('ketinggian_jalan', $forminspeksi['tanah']['ketinggian_jalan'] ?? [])) ? 'checked' : '' }}
                                            onclick="toggleMultipleFields('ketinggian_jalan', {
                                                'Lebih Tinggi': 'input-lebih-tinggi',
                                                'Lebih Rendah': 'input-lebih-rendah'
                                            })" />
                                        {{ $item->name }}
                                    </label>
                                    @if (strcasecmp($item->name, 'Lebih Tinggi') == 0)
                                        <input id="input-lebih-tinggi" type="text"
                                            style="{{ isset($forminspeksi['tanah']['ketinggian_tanah']['lebih_tinggi']) && $forminspeksi['tanah']['ketinggian_tanah']['lebih_tinggi'] ? '' : 'display: none;' }}"
                                            name="ketinggian_lebih_tinggi" class="mt-2 w-full input" placeholder="Masukkan Ketinggian..."
                                            value="{{ old('ketinggian_lebih_tinggi', $forminspeksi['tanah']['ketinggian_tanah']['lebih_tinggi'] ?? '') }}" />
                                    @elseif (strcasecmp($item->name, 'Lebih Rendah') == 0)
                                        <input id="input-lebih-rendah" type="text"
                                            style="{{ isset($forminspeksi['tanah']['ketinggian_tanah']['lebih_rendah']) && $forminspeksi['tanah']['ketinggian_tanah']['lebih_rendah'] ? '' : 'display: none;' }}"
                                            name="ketinggian_lebih_rendah" class="mt-2 w-full input" placeholder="Masukkan Ketinggian..."
                                            value="{{ old('ketinggian_lebih_rendah', $forminspeksi['tanah']['ketinggian_tanah']['lebih_rendah'] ?? '') }}" />
                                    @endif

                                </div>
                            @endforeach
                        @endif
                    </div>
                    <em id="error-ketinggian_jalan" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <!-- Kontur Jalan Depan Objek -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Kontur Jalan Depan Objek</label>
                <div class="flex-wrap items-stretch">
                    <div class="grid grid-cols-2 gap-4 md:grid-cols-3">
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input type="radio" class="radio" name="kontur_jalan" value="menurun"
                                {{ old('kontur_jalan', isset($forminspeksi['tanah']['kontur_jalan']) ? $forminspeksi['tanah']['kontur_jalan'] : '') == 'menurun' ? 'checked' : '' }}>
                            <span class="ml-2">Menurun</span>
                        </label>
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input type="radio" class="radio" name="kontur_jalan" value="rata"
                                {{ old('kontur_jalan', isset($forminspeksi['tanah']['kontur_jalan']) ? $forminspeksi['tanah']['kontur_jalan'] : '') == 'rata' ? 'checked' : '' }}>
                            <span class="ml-2">Rata</span>
                        </label>
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input type="radio" class="radio" name="kontur_jalan" value="Menanjak"
                                {{ old('kontur_jalan', isset($forminspeksi['tanah']['kontur_jalan']) ? $forminspeksi['tanah']['kontur_jalan'] : '') == 'Menanjak' ? 'checked' : '' }}>
                            <span class="ml-2">Menanjak</span>
                        </label>
                    </div>
                    <em id="error-kontur_jalan" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Posisi Kavling</label>
                <div class="flex-wrap items-stretch">
                    <div class="flex flex-col gap-4 items-start">
                        @if (isset($basicData['posisiKavling']))
                            @php
                                $selectedPosisiKavling = old(
                                    'posisi_kavling',
                                    $forminspeksi['tanah']['posisi_kavling']['posisi_kavling'] ?? [],
                                );
                                $lainnyaValue = old(
                                    'posisi_kavling_lainnya',
                                    $forminspeksi['tanah']['posisi_kavling']['lainnya'] ?? '',
                                );
                            @endphp

                            @foreach ($basicData['posisiKavling'] as $item)
                                <div class="flex items-center">
                                    <label class="flex gap-2.5 items-center form-label text-nowrap">
                                        <input class="checkbox" name="posisi_kavling[]" type="checkbox"
                                            value="{{ $item->name }}"
                                            {{ in_array($item->name, $selectedPosisiKavling) ? 'checked' : '' }}
                                            onclick="toggleCheckboxVisibility('posisi_kavling', 'posisi_kavling_lainnya', ['Lainnya'])" />
                                        {{ $item->name }}
                                    </label>
                                    @if (strcasecmp($item->name, 'Lainnya') == 0)
                                        <input id="posisi_kavling_lainnya" type="text"
                                            style="{{ $lainnyaValue ? '' : 'display: none' }}"
                                            name="posisi_kavling_lainnya" class="mt-2 w-full input"
                                            placeholder="Masukkan Posisi Kavling lainnya..."
                                            value="{{ $lainnyaValue }}" />
                                    @endif
                                </div>
                            @endforeach
                        @endif
                    </div>
                    <em id="error-posisi_kavling" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <!-- Tusuk Sate -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Tusuk Sate</label>
                <div class="flex-wrap items-stretch">
                    <div class="grid grid-cols-2 gap-4 mt-2 md:grid-cols-3">
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input onclick="toggleFieldVisibility('tusuk_sate', 'tusuk_sate_ya', ['Ya'])"
                                type="radio" class="radio" name="tusuk_sate" value="Ya"
                                {{ old('tusuk_sate') == 'Ya' || (isset($forminspeksi['tanah']['tusuk_sate']['Ya']) && $forminspeksi['tanah']['tusuk_sate']['Ya']) ? 'checked' : '' }}>
                            <span class="ml-2">Ya</span>

                            @php
                                $statusKey = isset($forminspeksi['tanah']['tusuk_sate']['Ya']) ? 'Ya' : 'Tidak';
                                $selectedData = $forminspeksi['tanah']['tusuk_sate'][$statusKey] ?? null;
                            @endphp

                            <input id="tusuk_sate_ya" class="input" name="tusuk_sate_ya"
                                placeholder="Masukkan Tusuk Sate..."
                                value="{{ old('tusuk_sate_ya', isset($selectedData) ? $selectedData : '') }}"
                                style="{{ $statusKey == 'Ya' ? '' : 'display: none;' }}">
                        </label>

                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input onclick="toggleFieldVisibility('tusuk_sate', 'tusuk_sate_ya', ['Ya'])"
                                type="radio" class="radio" name="tusuk_sate" value="Tidak"
                                {{ old('tusuk_sate') == 'Tidak' || (isset($forminspeksi['tanah']['tusuk_sate']['Tidak']) && $forminspeksi['tanah']['tusuk_sate']['Tidak']) ? 'checked' : '' }}>
                            <span class="ml-2">Tidak</span>
                        </label>
                    </div>
                    <em id="error-tusuk_sate" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <!-- Lockland -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Lockland</label>
                <div class="flex-wrap items-stretch">
                    <div class="grid grid-cols-2 gap-4 mt-2 md:grid-cols-3">
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input type="radio" class="radio" name="lockland" value="yes"
                                {{ old('lockland') == 'yes' || (isset($forminspeksi['tanah']['lockland']) && $forminspeksi['tanah']['lockland'] == 'yes') ? 'checked' : '' }}>
                            <span class="ml-2">Ya</span>
                        </label>
                        <label class="flex gap-2.5 items-center form-label text-nowrap">
                            <input type="radio" class="radio" name="lockland" value="no"
                                {{ old('lockland') == 'no' || (isset($forminspeksi['tanah']['lockland']) && $forminspeksi['tanah']['lockland'] == 'no') ? 'checked' : '' }}>
                            <span class="ml-2">Tidak</span>
                        </label>
                    </div>
                    <em id="error-lockland" class="text-sm alert text-danger"></em>
                </div>
            </div>

            <!-- Kondisi Fisik Tanah -->
            <div class="flex flex-wrap gap-2.5 items-stretch lg:flex-nowrap">
                <label class="form-label max-w-56">Kondisi Fisik Tanah</label>
                <div class="flex-wrap items-stretch">
                    <div class="flex flex-col gap-4 items-start">
                        @if (isset($basicData['kondisiFisikTanah']))
                            @foreach ($basicData['kondisiFisikTanah'] as $item)
                                <label class="flex gap-2.5 items-center form-label text-nowrap">
                                    <input class="checkbox" name="kondisi_fisik_tanah[]" type="checkbox"
                                        value="{{ $item->name }}"
                                        {{ in_array($item->name, old('kondisi_fisik_tanah', [])) || (isset($forminspeksi['tanah']['kondisi_fisik_tanah']['kondisi_fisik_tanah']) && in_array($item->name, $forminspeksi['tanah']['kondisi_fisik_tanah']['kondisi_fisik_tanah'])) ? 'checked' : '' }}
                                        onclick="toggleInputLainnya(this, 'kondisi_fisik_tanah_lainnya', '{{ $item->name }}')" />
                                    {{ $item->name }}
                                </label>

                                <!-- Display input field only if "lainnya" is selected -->
                                @if (strcasecmp($item->name, 'lainnya') == 0)
                                    <div class="flex items-center">
                                        <input type="text" name="kondisi_fisik_tanah_lainnya"
                                            id="kondisi_fisik_tanah_lainnya" class="mt-2 input"
                                            placeholder="Masukkan Kondisi Fisik Tanah..." style="display: none;"
                                            value="{{ old('kondisi_fisik_tanah_lainnya', isset($forminspeksi['tanah']['kondisi_fisik_tanah']['lainnya'])) }}">
                                    </div>
                                @endif
                            @endforeach
                        @endif
                    </div>
                    <em id="error-kondisi_fisik_tanah" class="text-sm alert text-danger"></em>
                </div>
            </div>
        </div>
    </div>
</div>
