{% extends "base.html" %}
{% block title %}Expenses{% endblock %}

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

    <!-- ================= HEADER ================= -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4><i class="bi bi-receipt"></i> Expenses</h4>
            <p class="text-muted mb-0">Track and manage operational costs</p>
        </div>

        <div>
            <a href="{% url 'create_category' %}" class="btn btn-outline-secondary btn-sm me-2">
                <i class="bi bi-tags"></i> Categories
            </a>

            <a href="{% url 'create_expense' %}" class="btn btn-primary btn-sm">
                <i class="bi bi-plus-circle"></i> Add Expense
            </a>
        </div>
    </div>

    <!-- ================= TABLE ================= -->
    <div class="card shadow-sm">
        <div class="card-body p-0">

            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">

                    <thead class="table-dark">
                        <tr>
                            <th>Building</th>
                            <th>Category</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th>Date</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>

                    <tbody>
                    {% for e in expenses %}
                        <tr>

                            <td>{{ e.building.name }}</td>

                            <td>
                                <span class="badge bg-light text-dark border">
                                    {{ e.category.name|default:"-" }}
                                </span>
                            </td>

                            <td class="fw-semibold">
                                KES {{ e.amount|floatformat:2 }}
                            </td>

                            <td>
                                {% if e.status == "approved" %}
                                    <span class="badge bg-success">Approved</span>
                                {% elif e.status == "rejected" %}
                                    <span class="badge bg-danger">Rejected</span>
                                {% else %}
                                    <span class="badge bg-warning text-dark">Pending</span>
                                {% endif %}
                            </td>

                            <td class="text-muted small">
                                {{ e.expense_date|date:"M d, Y" }}
                            </td>

                            <td class="text-end">

                                {% if user.role == "landlord" and e.status == "pending" %}
                                    <a href="{% url 'approve_expense' e.id %}"
                                       class="btn btn-success btn-sm">
                                        <i class="bi bi-check-circle"></i>
                                    </a>

                                    <a href="{% url 'reject_expense' e.id %}"
                                       class="btn btn-danger btn-sm">
                                        <i class="bi bi-x-circle"></i>
                                    </a>
                                {% else %}
                                    <span class="text-muted small">—</span>
                                {% endif %}

                            </td>

                        </tr>
                    {% empty %}
                        <tr>
                            <td colspan="6">
                                <div class="text-center py-4 text-muted">
                                    <i class="bi bi-receipt" style="font-size: 2rem;"></i>
                                    <p class="mb-0 mt-2">No expenses recorded yet</p>
                                </div>
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>

                </table>
            </div>

        </div>
    </div>

</div>
{% endblock %}