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

{% block title %}Expenses - HAVEN GRAZURI ADVANCE{% endblock %}

{% block content %}
<div>
    <!-- Summary Statistics -->
    <div class="mb-8 grid grid-cols-1 gap-5 sm:grid-cols-2 lg:grid-cols-5">
        <a href="?date_from={{ today }}&date_to={{ today }}" class="overflow-hidden rounded-lg bg-gradient-to-br from-blue-50 to-blue-100 px-4 py-5 shadow sm:p-6 hover:shadow-lg transition-all hover:scale-105 cursor-pointer block border border-blue-200">
            <dt class="truncate text-sm font-medium text-blue-700">Total Today</dt>
            <dd class="mt-1 text-3xl font-semibold tracking-tight text-blue-900">KES {{ total_today|floatformat:2|intcomma }}</dd>
            <p class="mt-2 text-sm text-blue-600"><span>🔍</span> Click to filter</p>
        </a>
        <a href="?date_from={{ month_start }}" class="overflow-hidden rounded-lg bg-gradient-to-br from-green-50 to-green-100 px-4 py-5 shadow sm:p-6 hover:shadow-lg transition-all hover:scale-105 cursor-pointer block border border-green-200">
            <dt class="truncate text-sm font-medium text-green-700">Total This Month</dt>
            <dd class="mt-1 text-3xl font-semibold tracking-tight text-green-900">KES {{ total_month|floatformat:2|intcomma }}</dd>
            <p class="mt-2 text-sm text-green-600"><span>🔍</span> Click to filter</p>
        </a>
        <a href="?date_from={{ year_start }}" class="overflow-hidden rounded-lg bg-gradient-to-br from-purple-50 to-purple-100 px-4 py-5 shadow sm:p-6 hover:shadow-lg transition-all hover:scale-105 cursor-pointer block border border-purple-200">
            <dt class="truncate text-sm font-medium text-purple-700">Total This Year</dt>
            <dd class="mt-1 text-3xl font-semibold tracking-tight text-purple-900">KES {{ total_year|floatformat:2|intcomma }}</dd>
            <p class="mt-2 text-sm text-purple-600"><span>🔍</span> Click to filter</p>
        </a>
        <a href="{% url 'expenses:pending_approvals' %}" class="overflow-hidden rounded-lg bg-yellow-50 px-4 py-5 shadow sm:p-6 hover:shadow-lg transition-shadow cursor-pointer block">
            <dt class="truncate text-sm font-medium text-yellow-700">Pending Approvals</dt>
            <dd class="mt-1 text-3xl font-semibold tracking-tight text-yellow-900">{{ pending_count }}</dd>
            <p class="mt-2 text-sm text-yellow-600">Requires action</p>
        </a>
        <a href="{% url 'expenses:expense_analytics' %}" class="overflow-hidden rounded-lg bg-blue-50 px-4 py-5 shadow sm:p-6 hover:shadow-lg transition-shadow cursor-pointer block">
            <dt class="truncate text-sm font-medium text-blue-700">Top Category</dt>
            <dd class="mt-1 text-xl font-semibold tracking-tight text-blue-900">
                {% if top_category %}
                    {{ top_category.category|title }}
                {% else %}
                    N/A
                {% endif %}
            </dd>
            <p class="mt-2 text-sm text-blue-600">
                {% if top_category %}
                    KES {{ top_category.total|floatformat:2|intcomma }}
                {% endif %}
            </p>
        </a>
    </div>

    <div class="sm:flex sm:items-center">
        <div class="sm:flex-auto">
            <div class="flex items-center justify-between">
                <div>
                    <h1 class="text-2xl font-semibold text-text-dark">Expense Management</h1>
                    <p class="mt-2 text-sm text-gray-700">
                        Track and manage all business expenses across branches.
                    </p>
                </div>
                <div class="flex gap-4">
                    <a href="{% url 'expenses:add_expense' %}" 
                       class="inline-flex items-center rounded-md bg-primary px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-primary-dark focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary">
                        <span>+</span> Add Expense
                    </a>
                    {% if user.role == 'admin' or user.role == 'team_leader' %}
                    <a href="{% url 'expenses:pending_approvals' %}" 
                       class="inline-flex items-center rounded-md bg-yellow-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-yellow-700">
                        <span>✓</span> Approvals ({{ pending_count }})
                    </a>
                    {% endif %}
                    <a href="{% url 'expenses:export_expenses_excel' %}?{{ request.GET.urlencode }}" 
                       class="inline-flex items-center rounded-md bg-green-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-green-700">
                        <span>📊</span> Export Excel
                    </a>
                    <a href="{% url 'expenses:expense_analytics' %}" 
                       class="inline-flex items-center rounded-md bg-blue-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-700">
                        <span>📊</span> Analytics
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="mt-6 bg-white rounded-lg shadow p-6">
        <form method="get" class="space-y-4">
            <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">
                <div>
                    <label for="id_search" class="block text-sm font-medium text-gray-700">Search</label>
                    {{ filter_form.search }}
                </div>
                <div>
                    <label for="id_category" class="block text-sm font-medium text-gray-700">Category</label>
                    {{ filter_form.category }}
                </div>
                <div>
                    <label for="id_payment_method" class="block text-sm font-medium text-gray-700">Payment Method</label>
                    {{ filter_form.payment_method }}
                </div>
                <div>
                    <label for="id_status" class="block text-sm font-medium text-gray-700">Status</label>
                    {{ filter_form.status }}
                </div>
                <div>
                    <label for="id_branch" class="block text-sm font-medium text-gray-700">Branch</label>
                    {{ filter_form.branch }}
                </div>
                <div>
                    <label for="id_date_from" class="block text-sm font-medium text-gray-700">Date From</label>
                    {{ filter_form.date_from }}
                </div>
                <div>
                    <label for="id_date_to" class="block text-sm font-medium text-gray-700">Date To</label>
                    {{ filter_form.date_to }}
                </div>
                <div>
                    <label for="id_loan_related" class="block text-sm font-medium text-gray-700">Loan Related</label>
                    {{ filter_form.loan_related }}
                </div>
            </div>
            <div class="flex gap-2">
                <button type="submit" class="inline-flex items-center rounded-md bg-primary px-4 py-2 text-sm font-semibold text-white shadow-sm hover:bg-primary-dark">
                    <span>🔍</span> Apply Filters
                </button>
                <a href="{% url 'expenses:expenses_list' %}" class="inline-flex items-center rounded-md bg-gray-200 px-4 py-2 text-sm font-semibold text-gray-700 shadow-sm hover:bg-gray-300">
                    <span>✕</span> Clear Filters
                </a>
            </div>
        </form>
    </div>

    <!-- Expenses Table -->
    <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">Staff</th>
                                <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Payment</th>
                                <th scope="col" class="px-3 py-3.5 text-center text-sm font-semibold text-gray-900">Status</th>
                                <th scope="col" class="px-3 py-3.5 text-center text-sm font-semibold text-gray-900">Receipt</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>
                                    {% if expense.loan %}
                                    <span class="ml-2 inline-flex items-center rounded-full bg-blue-100 px-2 py-0.5 text-xs font-medium text-blue-800">
                                        <span>🔗</span> Loan
                                    </span>
                                    {% endif %}
                                </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="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                                    {{ expense.get_payment_method_display }}
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-center">
                                    {% if expense.status == 'pending' %}
                                    <span class="inline-flex items-center rounded-full bg-yellow-100 px-2.5 py-0.5 text-xs font-medium text-yellow-800">
                                        Pending
                                    </span>
                                    {% elif expense.status == 'approved' %}
                                    <span class="inline-flex items-center rounded-full bg-green-100 px-2.5 py-0.5 text-xs font-medium text-green-800">
                                        Approved
                                    </span>
                                    {% elif expense.status == 'rejected' %}
                                    <span class="inline-flex items-center rounded-full bg-red-100 px-2.5 py-0.5 text-xs font-medium text-red-800">
                                        Rejected
                                    </span>
                                    {% endif %}
                                </td>
                                <td class="whitespace-nowrap px-3 py-4 text-sm text-center">
                                    {% if expense.has_receipt %}
                                    <a href="{{ expense.receipt_path.url }}" target="_blank" class="text-primary hover:text-primary-dark">
                                        <span>📄</span>
                                    </a>
                                    {% else %}
                                    <span class="text-gray-400">
                                        <span>−</span>
                                    </span>
                                    {% endif %}
                                </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>
                                    {% if expense.status == 'pending' %}
                                    <a href="{% url 'expenses:edit_expense' expense.pk %}" class="text-blue-600 hover:text-blue-900 mr-3">
                                        Edit
                                    </a>
                                    {% endif %}
                                </td>
                            </tr>
                            {% empty %}
                            <tr>
                                <td colspan="10" class="px-3 py-8 text-center text-sm text-gray-500">
                                    <span>📥</span>
                                    <p>No expenses found.</p>
                                    <a href="{% url 'expenses:add_expense' %}" class="mt-2 inline-block text-primary hover:text-primary-dark">
                                        Add your first expense
                                    </a>
                                </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="flex flex-1 justify-between sm:hidden">
            {% if expenses.has_previous %}
            <a href="?page={{ expenses.previous_page_number }}&{{ request.GET.urlencode }}" class="relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50">Previous</a>
            {% endif %}
            {% if expenses.has_next %}
            <a href="?page={{ expenses.next_page_number }}&{{ request.GET.urlencode }}" class="relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50">Next</a>
            {% endif %}
        </div>
        <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 }}&{{ request.GET.urlencode }}" 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 focus:z-20 focus:outline-offset-0">
                        <span class="sr-only">Previous</span>
                        <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 focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary">{{ num }}</span>
                        {% elif num > expenses.number|add:'-3' and num < expenses.number|add:'3' %}
                        <a href="?page={{ num }}&{{ request.GET.urlencode }}" 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 focus:z-20 focus:outline-offset-0">{{ num }}</a>
                        {% endif %}
                    {% endfor %}
                    
                    {% if expenses.has_next %}
                    <a href="?page={{ expenses.next_page_number }}&{{ request.GET.urlencode }}" 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 focus:z-20 focus:outline-offset-0">
                        <span class="sr-only">Next</span>
                        <span>›</span>
                    </a>
                    {% endif %}
                </nav>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
