{% extends 'base.html' %}
{% load static %}
{% block title %}Attendance Report - Rapha-Bethel BNPS{% endblock %}
{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div><h1 class="h3 mb-0">Attendance Report</h1><p class="text-muted">School-wide attendance overview</p></div>
        <a href="{% url 'reports:export_attendance_report' %}?from_date={{ from_date }}&to_date={{ to_date }}" class="btn btn-outline-primary"><i class="fas fa-download me-2"></i>Export CSV</a>
    </div>

    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-3"><label class="form-label">Class</label>
                    <select name="class" class="form-select">
                        <option value="">All Classes</option>
                        {% for cls in classes %}<option value="{{ cls.pk }}" {% if request.GET.class == cls.pk|stringformat:"s" %}selected{% endif %}>{{ cls }}</option>{% endfor %}
                    </select>
                </div>
                <div class="col-md-3"><label class="form-label">From Date</label><input type="date" name="from_date" class="form-control" value="{{ from_date }}"></div>
                <div class="col-md-3"><label class="form-label">To Date</label><input type="date" name="to_date" class="form-control" value="{{ to_date }}"></div>
                <div class="col-md-3"><button type="submit" class="btn btn-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
            </form>
        </div>
    </div>

    {% if overall_rate %}
    <div class="alert alert-info mb-4">
        <i class="fas fa-chart-line me-2"></i>Overall attendance rate for this period: <strong>{{ overall_rate }}%</strong>
    </div>
    {% endif %}

    <div class="card shadow">
        <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">Attendance by Class</h6></div>
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead class="table-dark"><tr><th>Class</th><th>Total Records</th><th>Present</th><th>Absent</th><th>Late</th><th>Rate</th><th>Actions</th></tr></thead>
                <tbody>
                    {% for stat in stats %}
                    <tr>
                        <td class="fw-bold">{{ stat.class }}</td>
                        <td>{{ stat.total }}</td>
                        <td class="text-success">{{ stat.present }}</td>
                        <td class="text-danger">{{ stat.absent }}</td>
                        <td class="text-warning">{{ stat.late }}</td>
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <div class="progress flex-fill" style="height:8px;">
                                    <div class="progress-bar bg-{% if stat.rate >= 80 %}success{% elif stat.rate >= 60 %}warning{% else %}danger{% endif %}" style="width:{{ stat.rate }}%"></div>
                                </div>
                                <span class="fw-bold small">{{ stat.rate }}%</span>
                            </div>
                        </td>
                        <td><a href="{% url 'reports:class_attendance_report' stat.class_id %}" class="btn btn-sm btn-outline-primary">Detail</a></td>
                    </tr>
                    {% empty %}
                    <tr><td colspan="7" class="text-center text-muted py-4">No attendance data for this period.</td></tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}