<x-guru-layout>
    <x-slot name="header">
        <h2 class="text-3xl font-bold text-gray-800 mb-2">Input Absensi</h2>
        <p class="text-gray-600">Masukkan data kehadiran santri untuk tanggal yang dipilih</p>
    </x-slot>
    <form action="{{ route('guru.absensis.store') }}" method="POST">
        @csrf
        <!-- Form Controls -->
        <div class="grid grid-cols-1 md:grid-cols-2 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>
                @error('tanggal')
                    <div class="text-red-600 text-sm mt-1">{{ $message }}</div>
                @enderror
            </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 onchange="this.form.submit()">
                    <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>
                @error('kelas_id')
                    <div class="text-red-600 text-sm mt-1">{{ $message }}</div>
                @enderror
            </div>
        </div>

        <!-- Data Table -->
        @if(isset($santris) && $santris->count())
            <div class="bg-gray-50 rounded-xl p-6 mb-8">
                <h3 class="text-lg font-semibold text-gray-800 mb-4">Data Santri</h3>
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-gradient-to-r from-green-600 to-yellow-400 text-white">
                                <tr>
                                    <th class="px-6 py-4 text-left text-sm font-semibold">Nama Santri</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold">Status</th>
                                    <th class="px-6 py-4 text-left text-sm font-semibold">Keterangan</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @foreach($santris as $i => $s)
                                    <tr class="hover:bg-gray-50 transition-colors">
                                        <td class="px-6 py-4">
                                            <div class="flex items-center">
                                                <div
                                                    class="w-8 h-8 bg-green-100 rounded-full flex items-center justify-center mr-3">
                                                    <span
                                                        class="text-green-600 font-semibold text-sm">{{ substr($s->nama, 0, 1) }}</span>
                                                </div>
                                                <span class="font-medium text-gray-900">{{ $s->nama }}</span>
                                            </div>
                                            <input type="hidden" name="absensi[{{$i}}][santri_id]" value="{{ $s->id }}">
                                        </td>
                                        <td class="px-6 py-4">
                                            <select name="absensi[{{$i}}][status]"
                                                class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                                                required>
                                                <option value="">Pilih Status</option>
                                                <option value="hadir">Hadir</option>
                                                <option value="izin">Izin</option>
                                                <option value="sakit">Sakit</option>
                                                <option value="alfa">Alpa</option>
                                            </select>
                                        </td>
                                        <td class="px-6 py-4">
                                            <input type="text" name="absensi[{{$i}}][keterangan]"
                                                class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-green-500 transition-colors"
                                                placeholder="Keterangan (opsional)">
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        @else
            <div class="text-center py-12">
                <div class="w-16 h-16 bg-blue-100 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-users text-blue-600 text-2xl"></i>
                </div>
                <h3 class="text-lg font-medium text-gray-900 mb-2">Pilih Kelas</h3>
                <p class="text-gray-600">Pilih kelas terlebih dahulu untuk menampilkan daftar santri.</p>
            </div>
        @endif

        <!-- Action Buttons -->
        <div class="flex items-center justify-end space-x-4 pt-6 border-t border-gray-200">
            <a href="{{ route('guru.absensis.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>
            @if(isset($santris) && $santris->count())
                <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 Absensi
                </button>
            @endif
        </div>
    </form>
    </div>
    </div>
    </div>
    </div>
</x-guru-layout>