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

{% block title %}Pending Approvals - HAVEN GRAZURI ADVANCE{% endblock %}

{% block content %}
<div>
    <div class="mb-6">
        <a href="{% url 'expenses:expenses_list' %}" class="inline-flex items-center text-sm text-gray-500 hover:text-gray-700">
            <span>←</span> Back to Expenses
        </a>
    </div>

    <div class="sm:flex sm:items-center">
        <div class="sm:flex-auto">
            <h1 class="text-2xl font-semibold text-text-dark">Pending Expense Approvals</h1>
            <p class="mt-2 text-sm text-gray-700">
                Review and approve or reject pending expense requests.
            </p>
        </div>
        <div class="mt-4 sm:ml-16 sm:mt-0 sm:flex-none">
            <span class="inline-flex items-center rounded-full bg-yellow-100 px-4 py-2 text-sm font-medium text-yellow-800">
                {{ total_pending }} Pending
            </span>
        </div>
    </div>

    <div class="mt-8 flow-root">
        <div class="-mx-4 -my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
            <div class="inline-block min-w-full py-2 align-middle sm:px-6 lg:px-8">
                <div class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 sm:rounded-lg">
                    <table class="min-w-full divide-y divide-gray-300">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6">Date</th>
                                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Title</th>
                                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Category</th>
                                <th scope="col" class="px-3 py-3.5 text-right text-sm font-semibold text-gray-900">Amount</th>
                                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Branch</th>
                                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Requested By</th>
                                <th scope="col" class="relative py-3.5 pl-3 pr-4 sm:pr-6">
                                    <span class="sr-only">Actions</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200 bg-white">
                            {% for expense in expenses %}
                            <tr class="hover:bg-gray-50">
                                <td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-gray-900 sm:pl-6">
                                    {{ expense.expense_date|date:"d M Y" }}
                                </td>
                                <td class="px-3 py-4 text-sm text-gray-900">
                                    <a href="{% url 'expenses:expense_detail' expense.pk %}" class="font-medium text-primary hover:text-primary-dark">
                                        {{ expense.title }}
                                    </a>
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                    {{ expense.get_category_display }}
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-right font-medium text-gray-900">
                                    KES {{ expense.amount|floatformat:2|intcomma }}
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                    {{ expense.branch.name }}
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                    {{ expense.staff.get_full_name }}
                                </td>
                                <td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
                                    <a href="{% url 'expenses:expense_detail' expense.pk %}" class="text-primary hover:text-primary-dark mr-3">
                                        View
                                    </a>
                                    <a href="{% url 'expenses:approve_expense' expense.pk %}" class="text-green-600 hover:text-green-900 mr-3">
                                        Approve
                                    </a>
                                    <a href="{% url 'expenses:reject_expense' expense.pk %}" class="text-red-600 hover:text-red-900">
                                        Reject
                                    </a>
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="7" class="px-3 py-8 text-center text-sm text-gray-500">
                                    <span>✓</span>
                                    <p>No pending approvals.</p>
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if expenses.has_other_pages %}
    <div class="mt-6 flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6 rounded-lg shadow">
        <div class="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between">
            <div>
                <p class="text-sm text-gray-700">
                    Showing <span class="font-medium">{{ expenses.start_index }}</span> to <span class="font-medium">{{ expenses.end_index }}</span> of
                    <span class="font-medium">{{ expenses.paginator.count }}</span> results
                </p>
            </div>
            <div>
                <nav class="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination">
                    {% if expenses.has_previous %}
                    <a href="?page={{ expenses.previous_page_number }}" class="relative inline-flex items-center rounded-l-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50">
                        <span>‹</span>
                    </a>
                    {% endif %}
                    
                    {% for num in expenses.paginator.page_range %}
                        {% if expenses.number == num %}
                        <span class="relative z-10 inline-flex items-center bg-primary px-4 py-2 text-sm font-semibold text-white">{{ num }}</span>
                        {% elif num > expenses.number|add:'-3' and num < expenses.number|add:'3' %}
                        <a href="?page={{ num }}" class="relative inline-flex items-center px-4 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50">{{ num }}</a>
                        {% endif %}
                    {% endfor %}
                    
                    {% if expenses.has_next %}
                    <a href="?page={{ expenses.next_page_number }}" class="relative inline-flex items-center rounded-r-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50">
                        <span>›</span>
                    </a>
                    {% endif %}
                </nav>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
