{% extends 'base.html' %}
{% load static %}

{% block title %}Attendance — {{ student.get_full_name }}{% 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">{{ student.get_full_name }}</h1>
            <p class="text-muted">Attendance Record &bull; {{ student.current_class|default:"No class" }}</p>
        </div>
        <a href="{% url 'students:student_detail' student.pk %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>Back to Student
        </a>
    </div>

    <!-- Summary Stats -->
    <div class="row mb-4">
        <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">Present</div>
                    <div class="h4 mb-0 font-weight-bold">{{ present_count|default:"0" }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-danger shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-danger text-uppercase mb-1">Absent</div>
                    <div class="h4 mb-0 font-weight-bold">{{ absent_count|default:"0" }}</div>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card border-left-warning shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">Late</div>
                    <div class="h4 mb-0 font-weight-bold">{{ late_count|default:"0" }}</div>
                </div>
            </div>
        </div>
        <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">Attendance Rate</div>
                    <div class="h4 mb-0 font-weight-bold">{{ attendance_rate|default:"0" }}%</div>
                </div>
            </div>
        </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-3">
                    <label class="form-label">Month</label>
                    <select name="month" class="form-control">
                        {% for num, name in months %}
                        <option value="{{ num }}" {% if selected_month|stringformat:"s" == num|stringformat:"s" %}selected{% endif %}>{{ name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Year</label>
                    <select name="year" class="form-control">
                        {% for yr in years %}
                        <option value="{{ yr }}" {% if selected_year|stringformat:"s" == yr|stringformat:"s" %}selected{% endif %}>{{ yr }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button>
                </div>
            </form>
        </div>
    </div>

    <!-- Attendance Records -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Attendance Records</h6>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-dark">
                        <tr>
                            <th>Date</th>
                            <th>Day</th>
                            <th>Status</th>
                            <th>Period</th>
                            <th>Marked By</th>
                            <th>Remarks</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for att in attendances %}
                        <tr>
                            <td class="fw-bold">{{ att.date|date:"d M Y" }}</td>
                            <td class="text-muted">{{ att.date|date:"l" }}</td>
                            <td>
                                <span class="badge bg-{% if att.status == 'present' %}success{% elif att.status == 'absent' %}danger{% elif att.status == 'late' %}warning{% elif att.status == 'excused' %}info{% else %}secondary{% endif %}">
                                    {{ att.get_status_display }}
                                </span>
                            </td>
                            <td>{{ att.get_period_display }}</td>
                            <td>{{ att.marked_by.user.get_full_name|default:"—" }}</td>
                            <td class="text-muted small">{{ att.remarks|default:"—" }}</td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="6" class="text-center py-4 text-muted">
                                <i class="fas fa-calendar-times fa-2x mb-2 d-block"></i>
                                No attendance records for this period.
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
