{% extends 'base.html' %}
{% load static %}
{% block title %}Fee Structure — 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">Fee Structure</h1>
      <p class="text-muted mb-0">Anglo &amp; Bilingual sections — tuition installments</p>
    </div>
    <div class="btn-group">
      <a href="{% url 'finance:fee_add' %}" class="btn btn-primary">
        <i class="fas fa-plus me-1"></i> Add Fee Row
      </a>
      <a href="{% url 'finance:school_item_list' %}" class="btn btn-outline-secondary">
        <i class="fas fa-tshirt me-1"></i> Item Prices
      </a>
    </div>
  </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 bar -->
  <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">
          <select class="form-select form-select-sm" name="academic_year">
            <option value="">All Academic 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-md-3">
          <select class="form-select form-select-sm" name="section">
            <option value="">All Sections</option>
            {% for val, label in section_choices %}
            <option value="{{ val }}" {% if request.GET.section == val %}selected{% endif %}>{{ label }}</option>
            {% endfor %}
          </select>
        </div>
        <div class="col-md-2">
          <select class="form-select form-select-sm" name="active">
            <option value="">All</option>
            <option value="active"   {% if request.GET.active == 'active'   %}selected{% endif %}>Active</option>
            <option value="inactive" {% if request.GET.active == 'inactive' %}selected{% endif %}>Inactive</option>
          </select>
        </div>
        <div class="col-auto">
          <button type="submit" class="btn btn-sm btn-primary">Filter</button>
          <a href="{% url 'finance:fee_list' %}" class="btn btn-sm btn-outline-secondary">Clear</a>
        </div>
      </form>
    </div>
  </div>

  <!-- Fee table -->
  <div class="card shadow">
    <div class="card-body p-0">
      <div class="table-responsive">
        <table class="table table-hover mb-0">
          <thead class="table-dark">
            <tr>
              <th>Section</th>
              <th>Class Group</th>
              <th>Year</th>
              <th class="text-end">Reg. (New)</th>
              <th class="text-end">Reg. (Old)</th>
              <th class="text-end">Total Fees</th>
              <th class="text-end">1st Inst.</th>
              <th class="text-end">2nd Inst.</th>
              <th>Status</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            {% for fee in fees %}
            <tr>
              <td>
                <span class="badge {% if fee.section == 'anglo' %}bg-primary{% else %}bg-success{% endif %}">
                  {{ fee.get_section_display }}
                </span>
              </td>
              <td class="fw-bold">{{ fee.get_class_group_display }}</td>
              <td>{{ fee.academic_year }}</td>
              <td class="text-end">{{ fee.registration_new|floatformat:0 }}</td>
              <td class="text-end">{{ fee.registration_old|floatformat:0 }}</td>
              <td class="text-end fw-bold text-primary">{{ fee.total_fees|floatformat:0 }}</td>
              <td class="text-end text-success">{{ fee.installment_1|floatformat:0 }}</td>
              <td class="text-end text-success">{{ fee.installment_2|floatformat:0 }}</td>
              <td>
                {% if fee.is_active %}
                  <span class="badge bg-success">Active</span>
                {% else %}
                  <span class="badge bg-secondary">Inactive</span>
                {% endif %}
              </td>
              <td>
                <a href="{% url 'finance:fee_detail' fee.pk %}" class="btn btn-sm btn-outline-primary" title="View">
                  <i class="fas fa-eye"></i>
                </a>
                <a href="{% url 'finance:fee_edit' fee.pk %}" class="btn btn-sm btn-outline-warning" title="Edit">
                  <i class="fas fa-edit"></i>
                </a>
                <a href="{% url 'finance:fee_delete' fee.pk %}" class="btn btn-sm btn-outline-danger" title="Delete">
                  <i class="fas fa-trash"></i>
                </a>
              </td>
            </tr>
            {% empty %}
            <tr>
              <td colspan="10" class="text-center py-5 text-muted">
                <i class="fas fa-table fa-3x d-block mb-3"></i>
                No fee structures defined yet.
                <a href="{% url 'finance: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 %}