@extends('layouts.app')
@section('title', 'Attendance Report')
@section('page_title', 'Monthly Attendance Report')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('attendance.students.index') }}">Student Attendance</a></li>
    <li class="breadcrumb-item active">Report</li>
@endsection

@section('content')
    <div class="card mb-3"><div class="card-body">
        <form method="GET" class="row g-2 align-items-end">
            <div class="col-md-3"><label class="form-label small">Class</label>
                <select name="class_id" class="form-select form-select-sm">
                    <option value="">— Select class —</option>
                    @foreach($classes as $c)<option value="{{ $c->id }}" @selected($classId==$c->id)>{{ $c->name }}</option>@endforeach
                </select></div>
            <div class="col-md-3"><label class="form-label small">Section</label>
                <select name="section_id" class="form-select form-select-sm">
                    <option value="">All sections</option>
                    @foreach($sections as $s)<option value="{{ $s->id }}" @selected($sectionId==$s->id)>{{ $s->name }}</option>@endforeach
                </select></div>
            <div class="col-md-3"><label class="form-label small">Month</label>
                <input type="month" name="month" value="{{ $month }}" class="form-control form-control-sm"></div>
            <div class="col-md-3"><button class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Generate</button></div>
        </form>
    </div></div>

    @if($classId && $rows->isNotEmpty())
        <div class="card"><div class="card-body">
            <table class="table table-hover align-middle">
                <thead><tr><th>Student</th><th class="text-center">Present</th><th class="text-center">Absent</th><th class="text-center">Leave</th><th class="text-center">Late</th><th class="text-center">Half-day</th><th class="text-center">Total</th><th class="text-center">%</th></tr></thead>
                <tbody>
                    @foreach($rows as $row)
                        @php $pct = $row['total'] ? round(($row['present'] + $row['late'] + $row['half_day']*0.5) / $row['total'] * 100) : 0; @endphp
                        <tr>
                            <td class="fw-semibold">{{ $row['student']->name }}</td>
                            <td class="text-center text-success">{{ $row['present'] }}</td>
                            <td class="text-center text-danger">{{ $row['absent'] }}</td>
                            <td class="text-center">{{ $row['leave'] }}</td>
                            <td class="text-center text-warning">{{ $row['late'] }}</td>
                            <td class="text-center">{{ $row['half_day'] }}</td>
                            <td class="text-center fw-semibold">{{ $row['total'] }}</td>
                            <td class="text-center"><span class="badge bg-soft-{{ $pct >= 75 ? 'success' : ($pct >= 50 ? 'warning' : 'danger') }}">{{ $pct }}%</span></td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div></div>
    @elseif($classId)
        <div class="card"><div class="card-body text-center text-muted py-4">No data for this selection.</div></div>
    @else
        <div class="card"><div class="card-body text-center text-muted py-4">Select a class and month.</div></div>
    @endif
@endsection
