{% extends 'base.html' %}
{% load static %}
{% block title %}Expenses - 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">Expenses</h1>
            <p class="text-muted">Total: {{ total_expenses|floatformat:0 }} XAF</p>
        </div>
        <a href="{% url 'finance:expense_add' %}" class="btn btn-primary"><i class="fas fa-plus me-2"></i>Add Expense</a>
    </div>

    <div class="card shadow mb-4">
        <div class="card-body">
            <form method="get" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">Category</label>
                    <select name="category" class="form-select">
                        <option value="">All Categories</option>
                        {% for cat in categories %}
                        <option value="{{ cat.pk }}" {% if request.GET.category == cat.pk|stringformat:"s" %}selected{% endif %}>{{ cat.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-md-3"><label class="form-label">From</label><input type="date" name="from_date" class="form-control" value="{{ request.GET.from_date }}"></div>
                <div class="col-md-3"><label class="form-label">To</label><input type="date" name="to_date" class="form-control" value="{{ request.GET.to_date }}"></div>
                <div class="col-md-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary flex-fill"><i class="fas fa-filter me-1"></i>Filter</button>
                    <a href="{% url 'finance:expense_list' %}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card shadow">
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead class="table-dark">
                    <tr><th>Date</th><th>Category</th><th>Description</th><th>Vendor</th><th>Amount</th><th>Method</th><th>Approved By</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    {% for expense in expenses %}
                    <tr>
                        <td>{{ expense.expense_date|date:"d M Y" }}</td>
                        <td><span class="badge bg-secondary">{{ expense.category.name }}</span></td>
                        <td>{{ expense.description }}</td>
                        <td>{{ expense.vendor|default:"—" }}</td>
                        <td class="text-danger fw-bold">{{ expense.amount|floatformat:0 }} XAF</td>
                        <td>{{ expense.get_payment_method_display }}</td>
                        <td>{% if expense.approved_by %}<span class="badge bg-success">{{ expense.approved_by.get_full_name }}</span>{% else %}<span class="badge bg-warning text-dark">Pending</span>{% endif %}</td>
                        <td>
                            <a href="{% url 'finance:expense_edit' expense.pk %}" class="btn btn-sm btn-outline-warning"><i class="fas fa-edit"></i></a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr><td colspan="8" class="text-center py-4 text-muted"><i class="fas fa-receipt fa-2x mb-2 d-block"></i>No expenses found.</td></tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    {% 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 }}">Previous</a></li>{% endif %}
        <li class="page-item active"><a class="page-link" href="#">{{ page_obj.number }} / {{ page_obj.paginator.num_pages }}</a></li>
        {% if page_obj.has_next %}<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a></li>{% endif %}
    </ul></nav>
    {% endif %}
</div>
{% endblock %}
