@foreach ($rujukan as $item)
    <div class="modal fade" id="exampleModal_{{ $item->id_rujukan }}" tabindex="-1"
        aria-labelledby="exampleModalLabel_{{ $item->id_rujukan }}" aria-hidden="true">
        <div class="modal-dialog modal-md">
            <div class="modal-content">
                <div class="modal-header">
                    <h1 class="modal-title fs-5 font-bold" id="exampleModalLabel_{{ $item->id_rujukan }}">
                        UBAH DATA TINDAK LANJUT RUJUKAN SPESIALIS
                    </h1>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form method="post"
                        action="{{ route('tindak-lanjut.rujukan-spesialis.update', $item->id_rujukan) }}"
                        enctype="multipart/form-data" class="mt-6 space-y-6">
                        @csrf
                        @method('PATCH')

                        {{-- Kirim ID rujukan agar bisa dicocokkan saat old() --}}
                        <input type="hidden" name="id_rujukan" value="{{ $item->id_rujukan }}">

                        @php
                            $siswas = App\Models\Siswa::all();
                            $pertanyaans = App\Models\PertanyaanKuesioner::all();
                            $chunks = \App\Models\PertanyaanKuesioner::all()->chunk(4);
                        @endphp

                        {{-- Pilih siswa --}}
                        <div class="max-w-xl">
                            <x-input-label for="id_siswa" value="NAMA SISWA" />
                            <select id="id_siswa" name="id_siswa" class="mt-1 block w-full rounded-md" required>
                                <option value="">Pilih Siswa</option>
                                @foreach ($siswas as $siswa)
                                    <option value="{{ $siswa->id_siswa }}"
                                        {{ old('id_rujukan') == $item->id_rujukan && old('id_siswa') == $siswa->id_siswa ? 'selected' : ($item->id_siswa == $siswa->id_siswa ? 'selected' : '') }}>
                                        {{ $siswa->nama_siswa }}</option>
                                @endforeach
                            </select>
                        </div>

                        {{-- Checkbox pertanyaan --}}
                        <div class="col-12">
                            <label class="form-label font-bold">Pilih permasalahan yang dialami siswa:</label>
                            <div class="container">
                                @php
                                    $selectedOld =
                                        old('id_rujukan') == $item->id_rujukan ? old('selected_siswa', []) : [];
                                    $selectedFromDB = [];

                                    // ambil array dari teks permasalahan jika belum ada relasi
                                    if (empty($selectedOld) && $item->permasalahan) {
                                        $daftar = explode("\n", $item->permasalahan);
                                        $selectedFromDB = \App\Models\PertanyaanKuesioner::whereIn('pertanyaan', $daftar)
                                            ->pluck('id')
                                            ->toArray();
                                    }
                                @endphp

                                @foreach ($chunks as $chunk)
                                    <div>
                                        @foreach ($chunk as $pertanyaan)
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox"
                                                    id="pertanyaan_{{ $pertanyaan->id }}" name="selected_siswa[]"
                                                    value="{{ $pertanyaan->id }}"
                                                    @if (in_array($pertanyaan->id, $selectedOld) || in_array($pertanyaan->id, $selectedFromDB)) checked @endif>
                                                <label class="form-check-label" for="pertanyaan_{{ $pertanyaan->id }}">
                                                    {{ $pertanyaan->pertanyaan }}
                                                </label>
                                            </div>
                                        @endforeach
                                    </div>
                                @endforeach
                            </div>
                        </div>

                        {{-- Select tujuan rujukan --}}
                        <div class="max-w-xl">
                            <x-input-label for="tujuan_rujukan" value="RUJUKAN" />
                            <x-select-input id="tujuan_rujukan" name="tujuan_rujukan" class="mt-1 block w-full"
                                required>
                                <option value="">Pilih Rujukan</option>
                                <option value="Psikolog"
                                    {{ old('id_rujukan') == $item->id_rujukan && old('tujuan_rujukan') == 'Psikolog' ? 'selected' : ($item->tujuan_rujukan == 'Psikolog' ? 'selected' : '') }}>
                                    Psikolog
                                </option>
                                <option value="Psikiater"
                                    {{ old('id_rujukan') == $item->id_rujukan && old('tujuan_rujukan') == 'Psikiater' ? 'selected' : ($item->tujuan_rujukan == 'Psikiater' ? 'selected' : '') }}>
                                    Psikiater
                                </option>
                            </x-select-input>
                        </div>

                        <div class="modal-footer">
                            <x-secondary-button tag="a" data-bs-dismiss="modal">Batal</x-secondary-button>
                            <x-primary-button name="save" value="true">Simpan</x-primary-button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endforeach
