<x-guru-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Tambah nilai santri') }}
        </h2>
    </x-slot>

    <form action="{{ auth()->user()->isGuru() ? route('guru.nilai_santris.store') : route('nilai_santris.store') }}"
        method="POST">
        @csrf
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Tanggal</label>
                <input type="date" name="tanggal"
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                    value="{{ old('tanggal', $tanggal ?? '') }}" required>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Kelas</label>
                <select name="kelas_id"
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                    required>
                    <option value="">Pilih Kelas</option>
                    @foreach($kelas as $k)
                        <option value="{{ $k->id }}" {{ old('kelas_id', $kelasId ?? '') == $k->id ? 'selected' : '' }}>
                            {{ $k->nama_kelas }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Mata Pelajaran</label>
                <select name="mapel_id"
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                    required>
                    <option value="">Pilih Mapel</option>
                    @foreach($mapelList as $m)
                        <option value="{{ $m->id }}" {{ old('mapel_id', $mapelId ?? '') == $m->id ? 'selected' : '' }}>
                            {{ $m->nama_mapel }}
                        </option>
                    @endforeach

                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Semester</label>
                <select name="semester_id"
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                    required>
                    <option value="">Pilih Semester</option>
                    @foreach($semesters as $s)
                        <option value="{{ $s->id }}" {{ old('semester_id') == $s->id ? 'selected' : '' }}>{{ $s->semester }} -
                            {{ $s->tahun_ajaran }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 mb-2">Jenis Nilai</label>
                <select name="jenis_nilai"
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                    required>
                    <option value="">Pilih Jenis Nilai</option>
                    <option value="Tugas" {{ old('jenis_nilai') == 'Tugas' ? 'selected' : '' }}>Tugas</option>
                    <option value="UTS" {{ old('jenis_nilai') == 'UTS' ? 'selected' : '' }}>UTS</option>
                    <option value="UAS" {{ old('jenis_nilai') == 'UAS' ? 'selected' : '' }}>UAS</option>
                    <option value="Quiz" {{ old('jenis_nilai') == 'Quiz' ? 'selected' : '' }}>Quiz</option>
                    <option value="Praktikum" {{ old('jenis_nilai') == 'Praktikum' ? 'selected' : '' }}>Praktikum</option>
                    <option value="Nilai Akhir" {{ old('jenis_nilai') == 'Nilai Akhir' ? 'selected' : '' }}>Nilai Akhir
                    </option>
                </select>
            </div>
        </div>
        <div class="bg-gray-50 rounded-xl p-6 mb-8">
            <h3 class="text-lg font-semibold text-gray-800 mb-4">Nilai Santri</h3>
            <div class="overflow-x-auto">
                <table class="min-w-full bg-white border border-gray-200 rounded-lg">
                    <thead class="bg-gradient-to-r from-green-600 to-yellow-400 text-white">
                        <tr>
                            <th class="px-4 py-3 text-left text-xs font-medium uppercase tracking-wider">No</th>
                            <th class="px-4 py-3 text-left text-xs font-medium uppercase tracking-wider">Nama Santri
                            </th>
                            <th class="px-4 py-3 text-left text-xs font-medium uppercase tracking-wider">Nilai</th>
                            <th class="px-4 py-3 text-left text-xs font-medium uppercase tracking-wider">Keterangan</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($santris as $index => $santri)
                            <tr class="hover:bg-gray-50">
                                <td class="px-4 py-3 text-sm text-gray-900">{{ $index + 1 }}</td>
                                <td class="px-4 py-3 text-sm text-gray-900">{{ $santri->nama }}</td>
                                <td class="px-4 py-3">
                                    <input type="number" name="nilai[{{ $index }}][nilai]"
                                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors text-sm"
                                        value="{{ old("nilai.{$index}.nilai") }}" min="0" max="100" step="0.01"
                                        placeholder="0-100">
                                    <input type="hidden" name="nilai[{{ $index }}][santri_id]" value="{{ $santri->id }}">
                                </td>
                                <td class="px-4 py-3">
                                    <input type="text" name="nilai[{{ $index }}][keterangan]"
                                        class="w-full px-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors text-sm"
                                        value="{{ old("nilai.{$index}.keterangan") }}" placeholder="Keterangan (opsional)">
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <div class="mt-4">
                <p class="text-sm text-gray-600">Nilai dapat berupa angka 0-100 dengan maksimal 2 desimal</p>
            </div>
        </div>
        <div class="flex items-center justify-end space-x-4 pt-6 border-t border-gray-200">
            <a href="{{ route('nilai_santris.index') }}"
                class="inline-flex items-center px-6 py-3 text-sm font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200 transition-colors">
                <i class="fas fa-arrow-left mr-2"></i>
                Kembali
            </a>
            <button type="submit"
                class="inline-flex items-center px-6 py-3 text-sm font-medium text-white bg-green-600 rounded-lg hover:bg-green-700 transition-colors">
                <i class="fas fa-save mr-2"></i>
                Simpan Nilai
            </button>
        </div>
    </form>
</x-guru-layout>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const selectSemester = document.querySelector('select[name="semester_id"]');
        const selectJenisNilai = document.querySelector('select[name="jenis_nilai"]');
        const hiddenSemester = document.getElementById('hiddenSemester');
        const hiddenJenisNilai = document.getElementById('hiddenJenisNilai');

        if (selectSemester && hiddenSemester) {
            hiddenSemester.value = selectSemester.value;
            selectSemester.addEventListener('change', function () {
                hiddenSemester.value = this.value;
            });
        }
        if (selectJenisNilai && hiddenJenisNilai) {
            hiddenJenisNilai.value = selectJenisNilai.value;
            selectJenisNilai.addEventListener('change', function () {
                hiddenJenisNilai.value = this.value;
            });
        }
    });
</script>