{% extends 'base.html' %}
{% load static %}
{% block title %}Enrollment Fees — 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">Student Enrollment Fees</h1>
      <p class="text-muted mb-0">Links each student to their fee tier and records new/returning status</p>
    </div>
    <a href="{% url 'finance:enrollment_fee_add' %}" class="btn btn-primary">
      <i class="fas fa-plus me-1"></i>Enroll Student
    </a>
  </div>

  {% if messages %}
    {% for msg in messages %}
    <div class="alert alert-{{ msg.tags|default:'info' }} alert-dismissible fade show">
      {{ msg }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    {% endfor %}
  {% endif %}

  <!-- Filter -->
  <div class="card shadow-sm mb-4">
    <div class="card-body py-2">
      <form method="get" class="row g-2 align-items-center">
        <div class="col-md-3">
          <input type="text" class="form-control form-control-sm" name="q"
                 placeholder="Search student…" value="{{ request.GET.q }}">
        </div>
        <div class="col-md-3">
          <select class="form-select form-select-sm" name="academic_year">
            <option value="">All Years</option>
            {% for yr in academic_years %}
            <option value="{{ yr }}" {% if request.GET.academic_year == yr %}selected{% endif %}>{{ yr }}</option>
            {% endfor %}
          </select>
        </div>
        <div class="col-auto">
          <button type="submit" class="btn btn-sm btn-primary">Filter</button>
          <a href="{% url 'finance:enrollment_fee_list' %}" class="btn btn-sm btn-outline-secondary">Clear</a>
        </div>
      </form>
    </div>
  </div>

  <div class="card shadow">
    <div class="card-body p-0">
      <div class="table-responsive">
        <table class="table table-hover mb-0">
          <thead class="table-light">
            <tr>
              <th>Student</th>
              <th>Year</th>
              <th>Section / Tier</th>
              <th>New/Returning</th>
              <th class="text-end">Reg. Fee</th>
              <th class="text-end">Total Fees</th>
              <th>1st Due</th>
              <th>2nd Due</th>
            </tr>
          </thead>
          <tbody>
            {% for ef in enrollment_fees %}
            <tr>
              <td>
                <a href="{% url 'students:student_detail' ef.student.pk %}">
                  {{ ef.student.get_full_name }}
                </a>
                <small class="text-muted d-block">{{ ef.student.student_id }}</small>
              </td>
              <td>{{ ef.academic_year }}</td>
              <td>
                <span class="badge {% if ef.fee_structure.section == 'anglo' %}bg-primary{% else %}bg-success{% endif %}">
                  {{ ef.fee_structure.get_section_display }}
                </span>
                <small class="d-block text-muted">{{ ef.fee_structure.get_class_group_display }}</small>
              </td>
              <td>
                {% if ef.is_new_pupil %}
                  <span class="badge bg-warning text-dark">New Pupil</span>
                {% else %}
                  <span class="badge bg-light text-dark border">Returning</span>
                {% endif %}
              </td>
              <td class="text-end">{{ ef.registration_fee|floatformat:0 }}</td>
              <td class="text-end fw-bold text-primary">{{ ef.total_fees|floatformat:0 }}</td>
              <td>{{ ef.installment_1_due|date:"d M Y"|default:"—" }}</td>
              <td>{{ ef.installment_2_due|date:"d M Y"|default:"—" }}</td>
            </tr>
            {% empty %}
            <tr>
              <td colspan="8" class="text-center py-5 text-muted">
                <i class="fas fa-user-graduate fa-3x d-block mb-3"></i>
                No enrollment records yet.
                <a href="{% url 'finance:enrollment_fee_add' %}">Add the first one</a>
              </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
    </div>
  </div>

  {% if is_paginated %}
  <nav class="mt-3">
    <ul class="pagination justify-content-center">
      {% if page_obj.has_previous %}
      <li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">«</a></li>
      {% endif %}
      {% for num in page_obj.paginator.page_range %}
        {% if page_obj.number == num %}
        <li class="page-item active"><a class="page-link" href="#">{{ num }}</a></li>
        {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
        <li class="page-item"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
        {% endif %}
      {% endfor %}
      {% if page_obj.has_next %}
      <li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">»</a></li>
      {% endif %}
    </ul>
  </nav>
  {% endif %}
</div>
{% endblock %}