{% extends "base.html" %}
{% load static %}

{% block title %}Liste des Clients - ECOLUX{% endblock %}

{% block content %}
<div class="container mx-auto px-4 py-8">
    <div class="bg-white rounded-lg shadow-lg p-6">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-3xl font-bold text-gray-800">👥 Clients ECOLUX</h1>
            <div class="text-sm text-gray-600">
                Total: {{ customers.paginator.count }} client{{ customers.paginator.count|pluralize }}
            </div>
        </div>

        <!-- Recherche -->
        <div class="bg-gray-50 rounded-lg p-4 mb-6">
            <form method="GET" class="flex items-end gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Recherche</label>
                    <input type="text" name="search" value="{{ search_query }}" 
                           placeholder="Nom, email..." 
                           class="border border-gray-300 rounded-md px-3 py-2 w-64">
                </div>
                
                <button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-md">
                    Rechercher
                </button>
                
                {% if search_query %}
                    <a href="{% url 'app:customer_list' %}" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-md">
                        Réinitialiser
                    </a>
                {% endif %}
            </form>
        </div>

        <!-- Liste des clients -->
        <div class="overflow-x-auto">
            <table class="min-w-full bg-white border border-gray-200 rounded-lg">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Client</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Contact</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Simulations</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Dernière activité</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Membre depuis</th>
                        <th class="px-4 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    {% for customer in customers %}
                    <tr class="hover:bg-gray-50">
                        <td class="px-4 py-4 whitespace-nowrap">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10">
                                    <div class="h-10 w-10 rounded-full bg-blue-100 flex items-center justify-center">
                                        <span class="text-blue-600 font-medium text-sm">
                                            {{ customer.full_name|slice:":2"|upper }}
                                        </span>
                                    </div>
                                </div>
                                <div class="ml-4">
                                    <div class="text-sm font-medium text-gray-900">
                                        {{ customer.full_name }}
                                    </div>
                                    <div class="text-sm text-gray-500">
                                        ID: #{{ customer.id }}
                                    </div>
                                </div>
                            </div>
                        </td>
                        <td class="px-4 py-4 whitespace-nowrap">
                            <div class="text-sm text-gray-900">
                                <a href="mailto:{{ customer.email }}" class="text-blue-600 hover:text-blue-800">
                                    {{ customer.email }}
                                </a>
                            </div>
                            {% if customer.phone %}
                                <div class="text-sm text-gray-500">
                                    <a href="tel:{{ customer.phone }}" class="text-blue-600 hover:text-blue-800">
                                        {{ customer.phone }}
                                    </a>
                                </div>
                            {% else %}
                                <div class="text-sm text-gray-400">Téléphone non renseigné</div>
                            {% endif %}
                        </td>
                        <td class="px-4 py-4 whitespace-nowrap">
                            <div class="flex items-center">
                                {% if customer.simulation_count > 0 %}
                                    <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-green-100 text-green-800">
                                        {{ customer.simulation_count }} simulation{{ customer.simulation_count|pluralize }}
                                    </span>
                                {% else %}
                                    <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full bg-gray-100 text-gray-800">
                                        Aucune simulation
                                    </span>
                                {% endif %}
                            </div>
                        </td>
                        <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-900">
                            {% if customer.last_simulation_date %}
                                {{ customer.last_simulation_date|date:"d/m/Y H:i" }}
                            {% else %}
                                <span class="text-gray-400">Jamais</span>
                            {% endif %}
                        </td>
                        <td class="px-4 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ customer.created_at|date:"d/m/Y" }}
                        </td>
                        <td class="px-4 py-4 whitespace-nowrap text-sm">
                            <div class="flex space-x-2">
                                {% if customer.simulation_count > 0 %}
                                    <a href="{% url 'app:simulation_list' %}?search={{ customer.email }}" 
                                       class="text-blue-600 hover:text-blue-900">
                                        Voir simulations
                                    </a>
                                {% endif %}
                                {% if customer.phone %}
                                    <a href="https://wa.me/{{ customer.phone|cut:'+' }}" 
                                       target="_blank"
                                       class="text-green-600 hover:text-green-900">
                                        WhatsApp
                                    </a>
                                {% endif %}
                                <a href="mailto:{{ customer.email }}" 
                                   class="text-purple-600 hover:text-purple-900">
                                    Email
                                </a>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="px-4 py-8 text-center text-gray-500">
                            Aucun client trouvé.
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        {% if customers.has_other_pages %}
        <div class="mt-6 flex justify-center">
            <nav class="flex space-x-2">
                {% if customers.has_previous %}
                    <a href="?page={{ customers.previous_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}" 
                       class="px-3 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300">
                        Précédent
                    </a>
                {% endif %}

                <span class="px-3 py-2 bg-blue-500 text-white rounded">
                    Page {{ customers.number }} sur {{ customers.paginator.num_pages }}
                </span>

                {% if customers.has_next %}
                    <a href="?page={{ customers.next_page_number }}{% if search_query %}&search={{ search_query }}{% endif %}" 
                       class="px-3 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300">
                        Suivant
                    </a>
                {% endif %}
            </nav>
        </div>
        {% endif %}

        <!-- Statistiques rapides -->
        <div class="mt-8 grid grid-cols-1 md:grid-cols-3 gap-4">
            <div class="bg-blue-50 rounded-lg p-4">
                <div class="text-blue-800 text-sm font-medium">Total clients</div>
                <div class="text-blue-900 text-2xl font-bold">{{ customers.paginator.count }}</div>
            </div>
            <div class="bg-green-50 rounded-lg p-4">
                <div class="text-green-800 text-sm font-medium">Clients actifs</div>
                <div class="text-green-900 text-2xl font-bold">
                    {% for customer in customers %}
                        {% if customer.simulation_count > 0 %}{% if forloop.first %}0{% endif %}{% endif %}
                    {% endfor %}
                    {{ customers.object_list|length }}
                </div>
            </div>
            <div class="bg-yellow-50 rounded-lg p-4">
                <div class="text-yellow-800 text-sm font-medium">Nouveaux ce mois</div>
                <div class="text-yellow-900 text-2xl font-bold">-</div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
