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

{% block title %}{{ student.get_full_name }} — Fees{% 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">Fees & Payments</h1>
            <p class="text-muted">{{ student.get_full_name }}</p>
        </div>
        <a href="{% url 'parents:parent_student_detail' student.pk %}" class="btn btn-outline-secondary">
            <i class="fas fa-arrow-left me-2"></i>Back
        </a>
    </div>

    <!-- Summary Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <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 Invoiced</div>
                    <div class="h4 fw-bold">{{ total_invoiced|floatformat:0 }} XAF</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <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">Total Paid</div>
                    <div class="h4 fw-bold text-success">{{ total_paid|floatformat:0 }} XAF</div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-left-{% if balance_due > 0 %}danger{% else %}success{% endif %} shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold {% if balance_due > 0 %}text-danger{% else %}text-success{% endif %} text-uppercase mb-1">Balance Due</div>
                    <div class="h4 fw-bold {% if balance_due > 0 %}text-danger{% else %}text-success{% endif %}">{{ balance_due|floatformat:0 }} XAF</div>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-7">
            <div class="card shadow mb-4">
                <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">Invoices</h6></div>
                <div class="card-body p-0">
                    <table class="table table-hover mb-0">
                        <thead class="table-dark"><tr><th>Invoice #</th><th>Term</th><th>Amount</th><th>Status</th><th>Due</th></tr></thead>
                        <tbody>
                            {% for invoice in invoices %}
                            <tr>
                                <td class="fw-bold">{{ invoice.invoice_number }}</td>
                                <td>{{ invoice.get_term_display }}</td>
                                <td>{{ invoice.total_amount|floatformat:0 }} XAF</td>
                                <td><span class="badge bg-{% if invoice.status == 'paid' %}success{% elif invoice.status == 'overdue' %}danger{% elif invoice.status == 'partial' %}warning{% else %}secondary{% endif %}">{{ invoice.get_status_display }}</span></td>
                                <td class="text-muted">{{ invoice.due_date|date:"d M Y" }}</td>
                            </tr>
                            {% empty %}
                            <tr><td colspan="5" class="text-center text-muted py-3">No invoices yet.</td></tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div class="col-md-5">
            <div class="card shadow mb-4">
                <div class="card-header py-3"><h6 class="m-0 font-weight-bold text-success">Payment History</h6></div>
                <div class="card-body p-0">
                    <table class="table table-hover mb-0">
                        <thead class="table-dark"><tr><th>Date</th><th>Amount</th><th>Method</th></tr></thead>
                        <tbody>
                            {% for payment in payments %}
                            <tr>
                                <td class="text-muted small">{{ payment.payment_date|date:"d M Y" }}</td>
                                <td class="fw-bold text-success">{{ payment.amount|floatformat:0 }}</td>
                                <td><span class="badge bg-secondary small">{{ payment.get_payment_method_display }}</span></td>
                            </tr>
                            {% empty %}
                            <tr><td colspan="3" class="text-center text-muted py-3">No payments yet.</td></tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
