@extends('layouts.guru')

@section('title', 'Presensi Kelas')
@section('subtitle', $kelasBinaan ? 'Kelas ' . $kelasBinaan->name : 'Belum ada kelas binaan')

@section('content')

@if(!$kelasBinaan)
    <div class="alert alert-warning">
        <i class="fa-solid fa-triangle-exclamation me-2"></i>
        Anda belum ditugaskan sebagai wali kelas. Hubungi administrator.
    </div>
@else

<div class="row g-3 mb-3">
    <div class="col-12">
        <div class="panel">
            <div class="panel-head">
                <h3>Presensi Kelas {{ $kelasBinaan->name }}</h3>
                <form method="GET" class="d-flex align-items-center gap-2">
                    <label class="form-label mb-0" style="font-size:.78rem;white-space:nowrap;">Tanggal:</label>
                    <input type="date" name="tanggal" class="form-control form-control-sm" value="{{ $tanggal }}"
                        max="{{ today()->format('Y-m-d') }}" onchange="this.form.submit()">
                </form>
            </div>

            @if($sudahIsiAbsen)
                <div class="px-4 pt-3">
                    <div class="alert alert-success d-flex align-items-center gap-2 mb-0">
                        <i class="fa-solid fa-circle-check"></i>
                        Absensi tanggal {{ \Carbon\Carbon::parse($tanggal)->format('d/m/Y') }} sudah diisi.
                        Anda masih bisa memperbarui data di bawah.
                    </div>
                </div>
            @endif

            <form action="{{ route('guru.absen.siswa.store') }}" method="POST">
                @csrf
                <input type="hidden" name="tanggal" value="{{ $tanggal }}">

                <div class="table-responsive">
                    <table class="table-modern">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Nama Siswa</th>
                                <th>NIS</th>
                                <th>Status Kehadiran</th>
                                <th>Keterangan</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($siswa as $i => $s)
                                <tr>
                                    <td class="text-muted">{{ $i + 1 }}</td>
                                    <td class="fw-semibold">{{ $s->name }}</td>
                                    <td class="text-muted">{{ $s->nis }}</td>
                                    <td>
                                        <div class="d-flex gap-2 flex-wrap">
                                            @foreach(['hadir' => ['success','circle-check'], 'sakit' => ['warning','face-dizzy'], 'izin' => ['info','envelope'], 'alfa' => ['danger','ban']] as $val => [$color, $icon])
                                                <label class="d-flex align-items-center gap-1" style="cursor:pointer;font-size:.8rem;font-weight:500;">
                                                    <input type="radio" name="absen[{{ $s->id }}][status]"
                                                        value="{{ $val }}"
                                                        {{ ($s->absen?->status ?? 'hadir') === $val ? 'checked' : '' }}
                                                        style="accent-color:var(--brand);">
                                                    <span class="badge badge-{{ $val }}">
                                                        <i class="fa-solid fa-{{ $icon }} me-1"></i>{{ ucfirst($val) }}
                                                    </span>
                                                </label>
                                            @endforeach
                                        </div>
                                    </td>
                                    <td>
                                        <input type="text" name="absen[{{ $s->id }}][notes]"
                                            class="form-control form-control-sm"
                                            value="{{ $s->absen?->notes }}"
                                            placeholder="Keterangan (opsional)">
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="5" class="text-center text-muted py-4">
                                        Tidak ada data siswa di kelas ini.
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>

                @if($siswa->count())
                    <div class="p-3 d-flex justify-content-end gap-2" style="border-top:1px solid var(--border);">
                        <button type="submit" class="btn btn-brand">
                            <i class="fa-solid fa-floppy-disk me-1"></i>
                            {{ $sudahIsiAbsen ? 'Perbarui Absensi' : 'Simpan Absensi' }}
                        </button>
                    </div>
                @endif
            </form>
        </div>
    </div>
</div>

@endif
@endsection
