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

{% block title %}{{ title }} - Haven Grazuri Investment Limited{% endblock %}

{% block content %}
<div class="mb-8">
    <div class="flex justify-between items-center">
        <div>
            <h2 class="text-2xl font-bold leading-7 text-gray-900 sm:truncate sm:text-3xl sm:tracking-tight">
                {{ title }}
            </h2>
            <p class="text-gray-600 mt-1">{{ total_count }} applications found</p>
        </div>
        <div class="flex space-x-3">
            <a href="{% url 'loans:applications' %}" class="inline-flex items-center px-4 py-2 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
                <span>←</span>
                Back to All Applications
            </a>
            <a href="{% url 'loans:new_application' %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary-dark">
                <span>+</span>
                New Application
            </a>
        </div>
    </div>
</div>

<!-- Summary Stats -->
<div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>📄</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Applications</dt>
                        <dd class="text-lg font-medium text-gray-900">{{ total_count|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>💵</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Total Requested</dt>
                        <dd class="text-lg font-medium text-gray-900">KES {{ total_amount|floatformat:2|intcomma }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>

    <div class="bg-white overflow-hidden shadow rounded-lg">
        <div class="p-5">
            <div class="flex items-center">
                <div class="flex-shrink-0">
                    <span>🔍</span>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">Status</dt>
                        <dd class="text-lg font-medium text-gray-900">{{ status_filter|title }}</dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Applications Table -->
<div class="bg-white shadow overflow-hidden sm:rounded-md">
    <div class="px-4 py-5 sm:px-6">
        <h3 class="text-lg leading-6 font-medium text-gray-900">Application Details</h3>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Detailed information about {{ status_filter }} applications</p>
    </div>
    
    {% if applications %}
    <div class="overflow-x-auto">
        <table class="min-w-full divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Applicant
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Requested Amount
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Status
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Submitted
                    </th>
                    <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Product
                    </th>
                    <th scope="col" class="relative px-6 py-3">
                        <span class="sr-only">Actions</span>
                    </th>
                </tr>
            </thead>
            <tbody class="bg-white divide-y divide-gray-200">
                {% for application in applications %}
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="flex items-center">
                            <div class="flex-shrink-0 h-10 w-10">
                                {% if application.borrower.selfie %}
                                    <img src="{{ application.borrower.selfie.url }}" alt="{{ application.borrower.get_full_name }}" 
                                         class="h-10 w-10 rounded-full object-cover border-2 border-gray-200">
                                {% else %}
                                    <div class="h-10 w-10 rounded-full bg-primary flex items-center justify-center">
                                        <span class="text-sm font-medium text-white">{{ application.borrower.first_name|first }}{{ application.borrower.last_name|first }}</span>
                                    </div>
                                {% endif %}
                            </div>
                            <div class="ml-4">
                                <div class="text-sm font-medium text-gray-900">
                                    {{ application.borrower.get_full_name }}
                                </div>
                                <div class="text-sm text-gray-500">
                                    {{ application.borrower.phone_number }}
                                </div>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">KES {{ application.requested_amount|floatformat:2|intcomma }}</div>
                        <div class="text-sm text-gray-500">{{ application.loan_product.name }}</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full
                            {% if application.status == 'pending' %}bg-yellow-100 text-yellow-800
                            {% elif application.status == 'approved' %}bg-green-100 text-green-800
                            {% elif application.status == 'rejected' %}bg-red-100 text-red-800
                            {% else %}bg-gray-100 text-gray-800{% endif %}">
                            {{ application.get_status_display }}
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                        {{ application.submitted_at|date:"M d, Y H:i" }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ application.loan_product.name }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="{% url 'loans:application_detail' application.pk %}" class="text-primary hover:text-primary-dark">
                            View Details
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if applications.has_other_pages %}
    <div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
        <div class="flex-1 flex justify-between sm:hidden">
            {% if applications.has_previous %}
                <a href="?page={{ applications.previous_page_number }}&status={{ status_filter }}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Previous
                </a>
            {% endif %}
            {% if applications.has_next %}
                <a href="?page={{ applications.next_page_number }}&status={{ status_filter }}" class="ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
                    Next
                </a>
            {% endif %}
        </div>
        <div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
            <div>
                <p class="text-sm text-gray-700">
                    Showing
                    <span class="font-medium">{{ applications.start_index }}</span>
                    to
                    <span class="font-medium">{{ applications.end_index }}</span>
                    of
                    <span class="font-medium">{{ applications.paginator.count }}</span>
                    results
                </p>
            </div>
            <div>
                <nav class="relative z-0 inline-flex rounded-md shadow-sm -space-x-px" aria-label="Pagination">
                    {% if applications.has_previous %}
                        <a href="?page={{ applications.previous_page_number }}&status={{ status_filter }}" class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <span class="sr-only">Previous</span>
                            <span>‹</span>
                        </a>
                    {% endif %}
                    
                    {% for num in applications.paginator.page_range %}
                        {% if applications.number == num %}
                            <span class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-primary text-sm font-medium text-white">
                                {{ num }}
                            </span>
                        {% else %}
                            <a href="?page={{ num }}&status={{ status_filter }}" class="relative inline-flex items-center px-4 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-700 hover:bg-gray-50">
                                {{ num }}
                            </a>
                        {% endif %}
                    {% endfor %}
                    
                    {% if applications.has_next %}
                        <a href="?page={{ applications.next_page_number }}&status={{ status_filter }}" class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            <span class="sr-only">Next</span>
                            <span>›</span>
                        </a>
                    {% endif %}
                </nav>
            </div>
        </div>
    </div>
    {% endif %}
    
    {% else %}
    <div class="text-center py-12">
        <span>🔍</span>
        <h3 class="text-lg font-medium text-gray-900 mb-2">No {{ status_filter }} applications found</h3>
        <p class="text-gray-500">There are currently no applications with the status "{{ status_filter }}".</p>
        <div class="mt-6">
            <a href="{% url 'loans:applications' %}" class="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-primary hover:bg-primary-dark">
                View All Applications
            </a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}