{% extends 'base.html' %}
{% load static %}
{% block title %}{{ title }} — 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">{{ title }}</h1></div>
    <a href="{% url 'finance:invoice_list' %}" class="btn btn-outline-secondary">
      <i class="fas fa-arrow-left me-1"></i>Back
    </a>
  </div>

  <div class="row justify-content-center">
    <div class="col-md-8">
      <div class="card shadow">
        <div class="card-header py-3 bg-primary text-white">
          <h6 class="m-0"><i class="fas fa-file-invoice me-2"></i>Invoice Details</h6>
        </div>
        <div class="card-body">
          <form method="post">
            {% csrf_token %}
            {% if form.errors %}
            <div class="alert alert-danger">
              Please correct the errors below. {{ form.non_field_errors }}
            </div>
            {% endif %}

            {% if not object %}
            <!-- Student -->
            <div class="mb-3">
              <label class="form-label fw-bold">Student <span class="text-danger">*</span></label>
              {{ form.student }}
              {% if form.student.errors %}<div class="text-danger small">{{ form.student.errors }}</div>{% endif %}
            </div>
            {% else %}
            <div class="alert alert-info">
              Editing invoice for: <strong>{{ object.student.get_full_name }}</strong>
            </div>
            {% endif %}

            <div class="row mb-3">
              <div class="col-md-6">
                <label class="form-label fw-bold">Academic Year <span class="text-danger">*</span></label>
                {{ form.academic_year }}
              </div>
              <div class="col-md-6">
                <label class="form-label fw-bold">Invoice Type <span class="text-danger">*</span></label>
                {{ form.invoice_type }}
                <div class="form-text">
                  Registration / 1st Installment / 2nd Installment / Item Sale
                </div>
              </div>
            </div>

            {% if not object %}
            <div class="mb-3">
              <label class="form-label fw-bold">Enrollment Fee Record</label>
              {{ form.enrollment_fee }}
              <div class="form-text">
                Link to the student's enrollment fee for this year (optional but recommended).
              </div>
            </div>
            {% endif %}

            <div class="row mb-3">
              {% if not object %}
              <div class="col-md-6">
                <label class="form-label fw-bold">Total Amount (XAF) <span class="text-danger">*</span></label>
                <input type="number" name="total_amount" class="form-control"
                       value="{{ form.total_amount.value|default:'' }}" min="0">
              </div>
              {% endif %}
              <div class="col-md-{% if not object %}6{% else %}12{% endif %}">
                <label class="form-label fw-bold">Due Date <span class="text-danger">*</span></label>
                {{ form.due_date }}
                {% if form.due_date.errors %}<div class="text-danger small">{{ form.due_date.errors }}</div>{% endif %}
              </div>
            </div>

            {% if object %}
            <div class="mb-3">
              <label class="form-label fw-bold">Status</label>
              {{ form.status }}
            </div>
            {% endif %}

            <div class="mb-3">
              <label class="form-label fw-bold">Notes</label>
              {{ form.notes }}
            </div>

            <div class="d-flex justify-content-end gap-2 mt-4">
              <a href="{% url 'finance:invoice_list' %}" class="btn btn-secondary">Cancel</a>
              <button type="submit" class="btn btn-primary">
                <i class="fas fa-save me-1"></i>
                {{ object|yesno:"Update Invoice,Create Invoice" }}
              </button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.querySelectorAll('input:not(.form-check-input):not([type="submit"]), select, textarea').forEach(el => {
  el.classList.add(el.tagName === 'SELECT' ? 'form-select' : 'form-control');
});
</script>
{% endblock %}