@extends('admin.layout')

@section('title', '')

@section('content')

<style>
    .bg-pink { background-color: #667722ff !important; }
</style>

<div class="card">
    <div class="card-header bg-pink text-white d-flex justify-content-between align-items-center">
            <div>
                <h5 class="mb-0"><i class="bi bi-star me-1"></i> Kehadiran</h5>
                <p class="mb-0 small text-white-50">Halaman Data Kehadiran Peserta Didik</p>
            </div>
        </div>
        @php
            $activePeriode = \App\Models\Periode::where('status', \App\Models\Periode::STATUS_AKTIF)->first();
            $tahunPel = $activePeriode->tahun_pelajaran ?? '-';
            $semester = $activePeriode->semester ?? '-';
        @endphp
        <div class="card-body">
            <div class="mb-3 d-flex justify-content-between align-items-start">
                <div>
                    <h5 class="mb-1">Data Kehadiran Peserta Didik</h5>
                    <div class="small text-muted">Tahun Pelajaran: <strong>{{ $tahunPel }}</strong> &nbsp;|&nbsp; Semester: <strong>{{ $semester }}</strong></div>
                </div>
                <div>
                    <a href="{{ url('admin/pendukung/kehadiran') }}" class="btn btn-primary btn-sm"><i class="bi bi-search me-1"></i> Pilih Kelas</a>
                </div>
            </div>

            @if(session('status'))
                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    {{ session('status') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif

            @if($errors->any())
                <div class="alert alert-danger">{{ $errors->first() }}</div>
            @endif

            <form method="GET" class="mb-3">
                <div class="row g-2 align-items-end">
                    <div class="col-md-6">
                        <label class="form-label">Pilih Kelas</label>
                        <select name="kelas_id" class="form-select" onchange="this.form.submit()">
                            <option value="">-- Pilih Kelas --</option>
                            @foreach($kelasList ?? [] as $k)
                                <option value="{{ $k->id }}" @if(isset($selected) && $selected && $selected->id == $k->id) selected @endif>{{ $k->nama }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-6">
                        <a href="{{ url('admin/pendukung/kehadiran') }}" class="btn btn-secondary">Reset</a>
                    </div>
                </div>
            </form>

            @if(isset($selected) && $selected)
                <div class="mb-2"><strong>Menampilkan data untuk :</strong> {{ $selected->nama }} </div>

                <form method="POST" action="{{ url('admin/pendukung/kehadiran/update') }}">
                    @csrf
                    <input type="hidden" name="kelas_id" value="{{ $selected->id }}">
                    <div class="table-responsive">
                        <table class="table table-striped table-sm table-bordered">
                        <thead>
                            <tr>
                                <th>Nama Peserta Didik</th>
                                <th class="small-col">NIS</th>
                                <th class="small-col">NISN</th>
                                <th class="small-col">Sakit</th>
                                <th class="small-col">Ijin</th>
                                <th class="small-col">Tanpa Keterangan</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($pesertas as $p)
                                <tr>
                                    <td>{{ $p->nama ?? '-' }}</td>
                                    <td>{{ $p->nis ?? '-' }}</td>
                                    <td>{{ $p->nisn ?? '-' }}</td>
                                    <td>
                                        <input type="number" min="0" name="attendance[{{ $p->id }}][sakit]" value="{{ old('attendance.'.$p->id.'.sakit', $p->sakit ?? 0) }}" class="form-control form-control-sm">
                                    </td>
                                    <td>
                                        <input type="number" min="0" name="attendance[{{ $p->id }}][ijin]" value="{{ old('attendance.'.$p->id.'.ijin', $p->ijin ?? 0) }}" class="form-control form-control-sm">
                                    </td>
                                    <td>
                                        <input type="number" min="0" name="attendance[{{ $p->id }}][tanpa_keterangan]" value="{{ old('attendance.'.$p->id.'.tanpa_keterangan', $p->tanpa_keterangan ?? 0) }}" class="form-control form-control-sm">
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="6" class="text-center">Tidak ada data peserta untuk kelas ini.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                    <div class="d-flex justify-content-end">
                        <button type="submit" class="btn btn-success">Simpan Kehadiran</button>
                    </div>
                </form>
                </div>
            @endif
@endsection
