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

{% block title %}Payments - Rapha-Bethel BNPS{% endblock %}

{% block content %}
<div class="container-fluid">

    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0 text-gray-800">Payments</h1>
        <div class="btn-group">
            <a href="{% url 'finance:payment_add' %}" class="btn btn-primary">
                <i class="fas fa-plus"></i> Record Payment
            </a>
            <a href="{% url 'finance:export_payments' %}" class="btn btn-outline-secondary">
                <i class="fas fa-download"></i> Export CSV
            </a>
        </div>
    </div>

    <!-- Stats Cards -->
    <div class="row mb-4">
        <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 Received</div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">{{ total_payments|floatformat:0 }} XAF</div>
                    <small class="text-muted">{{ payment_count }} completed payment{{ payment_count|pluralize }}</small>
                </div>
            </div>
        </div>
        <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">Showing</div>
                    <div class="h5 mb-0 font-weight-bold text-gray-800">{{ paginator.count|default:payments|length }} payment{{ paginator.count|default:payments|length|pluralize }}</div>
                    <small class="text-muted">Based on current filters</small>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-left-info shadow h-100 py-2">
                <div class="card-body">
                    <div class="text-xs font-weight-bold text-info text-uppercase mb-1">Quick Links</div>
                    <a href="{% url 'finance:bursar_dashboard' %}" class="small me-3">
                        <i class="fas fa-tachometer-alt me-1"></i>Dashboard
                    </a>
                    <a href="{% url 'finance:invoice_list' %}" class="small">
                        <i class="fas fa-file-invoice me-1"></i>Invoices
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Filter Payments</h6>
        </div>
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-3">
                    <label class="form-label small">From Date</label>
                    <input type="date" class="form-control" name="from_date" value="{{ request.GET.from_date }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label small">To Date</label>
                    <input type="date" class="form-control" name="to_date" value="{{ request.GET.to_date }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label small">Method</label>
                    <select class="form-control" name="method">
                        <option value="">All Methods</option>
                        {% for val, label in payment_methods %}
                        <option value="{{ val }}" {% if request.GET.method == val %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label small">Status</label>
                    <select class="form-control" name="status">
                        <option value="">All</option>
                        {% for val, label in status_choices %}
                        <option value="{{ val }}" {% if request.GET.status == val %}selected{% endif %}>{{ label }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-2 d-flex align-items-end gap-2">
                    <button type="submit" class="btn btn-primary flex-fill">
                        <i class="fas fa-filter"></i> Filter
                    </button>
                    <a href="{% url 'finance:payment_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-times"></i>
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Table -->
    <div class="card shadow">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Payment Records</h6>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>Payment #</th>
                            <th>Invoice #</th>
                            <th>Student</th>
                            <th>Amount (XAF)</th>
                            <th>Method</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Receipt</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for payment in payments %}
                        <tr>
                            <td><a href="{% url 'finance:payment_detail' payment.pk %}">{{ payment.payment_number }}</a></td>
                            <td>
                                <a href="{% url 'finance:invoice_detail' payment.invoice.pk %}">
                                    {{ payment.invoice.invoice_number }}
                                </a>
                            </td>
                            <td>
                                <a href="{% url 'students:student_detail' payment.student.pk %}">
                                    {{ payment.student.get_full_name }}
                                </a>
                            </td>
                            <td class="text-success fw-bold">{{ payment.amount|floatformat:0 }}</td>
                            <td>
                                <span class="badge bg-info">{{ payment.get_payment_method_display }}</span>
                            </td>
                            <td>{{ payment.payment_date|date:"d M Y" }}</td>
                            <td>
                                <span class="badge bg-{% if payment.status == 'completed' %}success{% elif payment.status == 'pending' %}warning{% else %}danger{% endif %}">
                                    {{ payment.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if payment.receipt_issued %}
                                <span class="badge bg-success">
                                    <i class="fas fa-check me-1"></i>{{ payment.receipt_number }}
                                </span>
                                {% else %}
                                <a href="{% url 'finance:generate_receipt' payment.pk %}" class="btn btn-sm btn-outline-secondary">
                                    <i class="fas fa-receipt"></i> Issue
                                </a>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{% url 'finance:payment_detail' payment.pk %}" class="btn btn-sm btn-outline-primary" title="View">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="{% url 'finance:payment_edit' payment.pk %}" class="btn btn-sm btn-outline-warning" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="9" class="text-center py-5 text-muted">
                                <i class="fas fa-money-bill-wave fa-3x mb-3 d-block"></i>
                                No payments found.
                                {% if not request.GET.from_date and not request.GET.method %}
                                <a href="{% url 'finance:payment_add' %}">Record the first payment</a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if is_paginated %}
    <nav class="mt-4">
        <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 }}{% if request.GET.from_date %}&from_date={{ request.GET.from_date }}{% endif %}{% if request.GET.to_date %}&to_date={{ request.GET.to_date }}{% endif %}{% if request.GET.method %}&method={{ request.GET.method }}{% endif %}">
                    &laquo; Previous
                </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 }}{% if request.GET.from_date %}&from_date={{ request.GET.from_date }}{% endif %}{% if request.GET.method %}&method={{ request.GET.method }}{% endif %}">
                    {{ 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 }}{% if request.GET.from_date %}&from_date={{ request.GET.from_date }}{% endif %}{% if request.GET.method %}&method={{ request.GET.method }}{% endif %}">
                    Next &raquo;
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}

</div>
{% endblock %}