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

{% block title %}{{ title }} - HAVEN GRAZURI ADVANCE{% 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 }} clients found</p>
        </div>
        <div class="flex space-x-3">
            <a href="{% url 'users:client_list' %}"
                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 Clients
            </a>
            {% load permission_tags %}
            {% has_permission user 'clients' 'create' as can_create_client %}
            {% if can_create_client %}
            <a href="{% url 'users:client_create' %}"
                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>
                Add Client
            </a>
            {% endif %}
        </div>
    </div>
</div>

<!-- Filter Options -->
<div class="mb-6">
    <div class="flex flex-wrap gap-2">
        <a href="?status=active"
            class="inline-flex items-center px-3 py-2 rounded-md text-sm font-medium {% if status_filter == 'active' %}bg-primary text-white{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200{% endif %}">
            Active Clients
        </a>
        <a href="?status=inactive"
            class="inline-flex items-center px-3 py-2 rounded-md text-sm font-medium {% if status_filter == 'inactive' %}bg-primary text-white{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200{% endif %}">
            Inactive Clients
        </a>
        <a href="?status=with_loans"
            class="inline-flex items-center px-3 py-2 rounded-md text-sm font-medium {% if status_filter == 'with_loans' %}bg-primary text-white{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200{% endif %}">
            With Loans
        </a>
        <a href="?status=without_loans"
            class="inline-flex items-center px-3 py-2 rounded-md text-sm font-medium {% if status_filter == 'without_loans' %}bg-primary text-white{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200{% endif %}">
            Without Loans
        </a>
        <a href="?status=blacklisted"
            class="inline-flex items-center px-3 py-2 rounded-md text-sm font-medium {% if status_filter == 'blacklisted' %}bg-primary text-white{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200{% endif %}">
            Blacklisted
        </a>
    </div>
</div>

<!-- Summary Stats -->
<div class="grid grid-cols-1 md:grid-cols-4 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 Clients</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">Filter</dt>
                        <dd class="text-lg font-medium text-gray-900">{{ status_filter|title }}</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">
                    <i class="fas fa-handshake text-gray-400 text-2xl"></i>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">With Active Loans</dt>
                        <dd class="text-lg font-medium text-gray-900">
                            {% for client in clients %}
                            {% if client.active_loans > 0 %}{{ forloop.counter0|add:1 }}{% endif %}
                            {% empty %}0{% endfor %}
                        </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">
                    <i class="fas fa-calendar-plus text-gray-400 text-2xl"></i>
                </div>
                <div class="ml-5 w-0 flex-1">
                    <dl>
                        <dt class="text-sm font-medium text-gray-500 truncate">New This Month</dt>
                        <dd class="text-lg font-medium text-gray-900">
                            {% now "Y-m-01" as month_start %}
                            {% for client in clients %}
                            {% if client.date_joined >= month_start %}{{ forloop.counter0|add:1 }}{% endif %}
                            {% empty %}0{% endfor %}
                        </dd>
                    </dl>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Clients 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">Client Details</h3>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Detailed information about {{status_filter|default:"all"|cut:"_"|title }} clients</p>
    </div>

    {% if clients %}
    <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">
                        Client
                    </th>
                    <th scope="col"
                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Contact
                    </th>
                    <th scope="col"
                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Loans
                    </th>
                    <th scope="col"
                        class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                        Total Borrowed
                    </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">
                        Joined
                    </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 client in clients %}
                <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 client.selfie %}
                                <img src="{{ client.selfie.url }}" alt="{{ client.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">{{ client.first_name|first }}{{
                                        client.last_name|first }}</span>
                                </div>
                                {% endif %}
                            </div>
                            <div class="ml-4">
                                <div class="text-sm font-medium text-gray-900">
                                    {{ client.get_full_name }}
                                </div>
                                <div class="text-sm text-gray-500">
                                    ID: {{ client.national_id|default:"N/A" }}
                                </div>
                            </div>
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">{{ client.phone_number }}</div>
                        <div class="text-sm text-gray-500">{{ client.email|default:"No email" }}</div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">
                            Total: {{ client.total_loans }}
                        </div>
                        <div class="text-sm text-gray-500">
                            Active: {{ client.active_loans }}
                        </div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <div class="text-sm text-gray-900">KES {{ client.total_borrowed|floatformat:2|intcomma }}</div>
                        <div class="text-sm text-gray-500">Repaid: KES {{ client.total_repaid|floatformat:2|intcomma }}
                        </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 client.is_active %}bg-green-100 text-green-800
                            {% elif client.status == 'blacklisted' %}bg-red-100 text-red-800
                            {% else %}bg-gray-100 text-gray-800{% endif %}">
                            {% if client.is_active %}Active
                            {% elif client.status == 'blacklisted' %}Blacklisted
                            {% else %}Inactive{% endif %}
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        {{ client.date_joined|date:"M d, Y" }}
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="{% url 'users:client_detail' client.pk %}"
                            class="text-primary hover:text-primary-dark">
                            View Details
                        </a>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- Pagination -->
    {% if clients.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 clients.has_previous %}
            <a href="?page={{ clients.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 clients.has_next %}
            <a href="?page={{ clients.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">{{ clients.start_index }}</span>
                    to
                    <span class="font-medium">{{ clients.end_index }}</span>
                    of
                    <span class="font-medium">{{ clients.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 clients.has_previous %}
                    <a href="?page={{ clients.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 clients.paginator.page_range %}
                    {% if clients.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 clients.has_next %}
                    <a href="?page={{ clients.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|default:"all"|cut:"_"|title }} clients
            found</h3>
        <p class="text-gray-500">There are currently no clients matching the "{{ status_filter|default:"all"|cut:"_"|title }}" criteria.</p>
        <div class="mt-6">
            <a href="{% url 'users:client_list' %}"
                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 Clients
            </a>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}