{% extends 'base.html' %}
{% load static %}
{% block title %}Class Report — {{ class }}{% 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">Class Report — {{ class }}</h1>
            <p class="text-muted">Term {{ term }} &bull; Year {{ year }} &bull; {{ students.count }} students</p>
        </div>
        <div class="btn-group">
            <a href="{% url 'reports:academic_report' %}" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-2"></i>Back</a>
            <a href="{% url 'reports:generate_academic_report' %}" class="btn btn-outline-primary" onclick="this.closest('form') && this.closest('form').submit()">
                <i class="fas fa-download me-2"></i>Export CSV
            </a>
        </div>
    </div>

    <!-- Filter -->
    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-4"><label class="form-label">Term</label>
                    <select name="term" class="form-select">
                        {% for t in "123" %}<option value="{{ t }}" {% if term|stringformat:"s" == t %}selected{% endif %}>Term {{ t }}</option>{% endfor %}
                    </select>
                </div>
                <div class="col-md-4"><label class="form-label">Year</label>
                    <input type="number" name="year" class="form-control" value="{{ year }}">
                </div>
                <div class="col-md-4"><button type="submit" class="btn btn-primary w-100">Apply</button></div>
            </form>
        </div>
    </div>

    <!-- Summary Stats -->
    {% if student_stats %}
    <div class="row mb-4">
        <div class="col-md-3"><div class="card border-left-primary shadow h-100 py-2"><div class="card-body"><div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Total Students</div><div class="h4 mb-0 fw-bold">{{ student_stats|length }}</div></div></div></div>
        <div class="col-md-3"><div class="card border-left-success shadow h-100 py-2"><div class="card-body"><div class="text-xs font-weight-bold text-success text-uppercase mb-1">Avg Attendance</div><div class="h4 mb-0 fw-bold">{% with total=0 %}{% for s in student_stats %}{% endfor %}—{% endwith %}</div></div></div></div>
    </div>
    {% endif %}

    <div class="card shadow">
        <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">Student Attendance Summary</h6></div>
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead class="table-dark">
                    <tr><th>#</th><th>Student</th><th>Total Days</th><th>Present</th><th>Absent</th><th>Late</th><th>Attendance Rate</th></tr>
                </thead>
                <tbody>
                    {% for stat in student_stats %}
                    <tr>
                        <td class="text-muted">{{ forloop.counter }}</td>
                        <td><a href="{% url 'reports:student_report' stat.student.pk %}">{{ stat.student.get_full_name }}</a></td>
                        <td>{{ stat.total_days }}</td>
                        <td class="text-success fw-bold">{{ 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.attendance_rate >= 80 %}success{% elif stat.attendance_rate >= 60 %}warning{% else %}danger{% endif %}" style="width:{{ stat.attendance_rate }}%"></div>
                                </div>
                                <span class="fw-bold small">{{ stat.attendance_rate }}%</span>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr><td colspan="7" class="text-center text-muted py-4">No data available for this period.</td></tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %}
