{% extends 'admin/_layout.html.twig' %}

{% block title %}Clients - Administration - Association E-Cosplay{% endblock %}

{% block admin_content %}
<div class="page-container">
    <div class="flex items-center justify-between mb-8">
        <h1 class="text-2xl font-bold heading-page">Clients</h1>
        <a href="{{ path('app_admin_clients_create') }}" class="px-4 py-2 btn-gold font-bold uppercase text-xs tracking-wider">+ Nouveau client</a>
    </div>

    {% for type, messages in app.flashes %}
        {% for message in messages %}
            <div class="mb-6 p-4 glass font-medium text-sm {{ type == 'success' ? 'border-green-300 text-green-800' : 'border-red-300 text-red-800' }}">
                {{ message }}
            </div>
        {% endfor %}
    {% endfor %}

    {# Barre de recherche #}
    <div class="mb-6">
        <div class="relative">
            <input type="text" id="search-customers" placeholder="Rechercher un client (nom, email, SIRET, ville...)"
                   class="w-full px-4 py-3 pl-10 input-glass text-sm font-medium">
            <svg class="w-4 h-4 absolute left-3 top-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/></svg>
        </div>
        <div id="search-results" class="hidden glass border-t-0 bg-white max-h-64 overflow-y-auto"></div>
    </div>

    {# Liste des clients #}
    <div class="glass overflow-x-auto overflow-hidden">
        <table class="w-full text-sm">
            <thead>
                <tr class="glass-dark text-white">
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Client</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Email</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Type</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">SIRET</th>
                    <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Stripe</th>
                    <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Statut</th>
                    <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Inscription</th>
                    <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for customer in customers %}
                    {% set info = customersInfo[customer.id] ?? {} %}
                    <tr class="hover:bg-white/50">
                        <td class="px-4 pt-3 pb-1">
                            <a href="{{ path('app_admin_clients_show', {id: customer.id}) }}" class="font-bold hover:text-[#fabf04] transition-colors">{{ customer.fullName }}</a>
                            {% if customer.raisonSociale and customer.firstName %}
                                <span class="text-[10px] text-gray-400 ml-1">{{ customer.firstName }} {{ customer.lastName }}</span>
                            {% endif %}
                        </td>
                        <td class="px-4 pt-3 pb-1 text-xs font-mono">{{ customer.email ?? customer.user.email }}</td>
                        <td class="px-4 pt-3 pb-1">
                            {% if customer.typeCompany %}
                                <span class="px-2 py-0.5 bg-gray-100 text-gray-700 font-bold uppercase text-[10px]">{{ customer.typeCompany }}</span>
                            {% else %}
                                <span class="text-gray-400">—</span>
                            {% endif %}
                        </td>
                        <td class="px-4 pt-3 pb-1 text-xs font-mono">{{ customer.siret ?? '—' }}</td>
                        <td class="px-4 pt-3 pb-1 text-center">
                            {% if customer.stripeCustomerId %}
                                <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Lie</span>
                            {% else %}
                                <span class="px-2 py-0.5 bg-gray-100 text-gray-500 font-bold uppercase text-[10px]">Non</span>
                            {% endif %}
                        </td>
                        <td class="px-4 pt-3 pb-1 text-center">
                            {% if customer.state == 'active' %}
                                <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Actif</span>
                            {% elseif customer.state == 'suspended' %}
                                <span class="px-2 py-0.5 bg-yellow-100 text-yellow-800 font-bold uppercase text-[10px]">Suspendu</span>
                            {% elseif customer.state == 'pending_delete' %}
                                <span class="px-2 py-0.5 bg-red-500/20 text-red-700 font-bold uppercase text-[10px]animate-pulse">Suppression</span>
                            {% else %}
                                <span class="px-2 py-0.5 bg-red-500/20 text-red-700 font-bold uppercase text-[10px]">Desactive</span>
                            {% endif %}
                        </td>
                        <td class="px-4 pt-3 pb-1 text-xs text-gray-500">{{ customer.createdAt|date('d/m/Y') }}</td>
                        <td class="px-4 pt-3 pb-1 text-center">
                            <div class="flex items-center justify-center gap-2">
                                <a href="{{ path('app_admin_clients_show', {id: customer.id}) }}" class="px-2 py-1 border-2 border-indigo-600 bg-white text-indigo-600 font-bold uppercase text-[10px] tracking-widest hover:bg-indigo-600 hover:text-white transition-all">Fiche</a>
                                {% if not customer.isPendingDelete %}
                                    <form method="post" action="{{ path('app_admin_clients_toggle', {id: customer.id}) }}">
                                        {% if customer.isActive %}
                                            <button type="submit" class="px-2 py-1 border-2 border-yellow-600 bg-white text-yellow-600 font-bold uppercase text-[10px] tracking-widest hover:bg-yellow-600 hover:text-white transition-all">Suspendre</button>
                                        {% else %}
                                            <button type="submit" class="px-2 py-1 border-2 border-green-600 bg-white text-green-600 font-bold uppercase text-[10px] tracking-widest hover:bg-green-600 hover:text-white transition-all">Activer</button>
                                        {% endif %}
                                    </form>
                                    <form method="post" action="{{ path('app_admin_clients_delete', {id: customer.id}) }}" data-confirm="Supprimer le client {{ customer.fullName }} ? Il sera supprime automatiquement cette nuit.">
                                        <button type="submit" class="px-2 py-1 border-2 border-red-600 bg-white text-red-600 font-bold uppercase text-[10px] tracking-widest hover:bg-red-600 hover:text-white transition-all">Supprimer</button>
                                    </form>
                                {% else %}
                                    <span class="text-[10px] text-red-500 font-bold uppercase">En attente</span>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {# Ligne info services #}
                    <tr class="border-b border-white/20">
                        <td colspan="8" class="px-4 pb-3 pt-0">
                            <div class="flex flex-wrap items-center gap-x-4 gap-y-1 text-[10px]">
                                {% if customer.raisonSociale %}
                                    <span class="text-gray-400">{{ customer.raisonSociale }}</span>
                                    <span class="text-gray-300">|</span>
                                {% endif %}
                                {% if customer.siret %}
                                    <span class="text-gray-400 font-mono">{{ customer.siret }}</span>
                                    <span class="text-gray-300">|</span>
                                {% endif %}
                                {% if customer.typeCompany %}
                                    <span class="text-gray-400 uppercase font-bold">{{ customer.typeCompany }}</span>
                                    <span class="text-gray-300">|</span>
                                {% endif %}

                                <span class="font-bold text-gray-500">Sites : {{ info.sites ?? 0 }}</span>
                                <span class="text-gray-300">|</span>
                                <span class="font-bold text-gray-500">NDD : {{ info.domains ?? 0 }}</span>
                                <span class="text-gray-300">|</span>
                                <span class="font-bold text-gray-500">Emails : {{ info.emails ?? 0 }}</span>
                                <span class="text-gray-300">|</span>

                                {# Services checks #}
                                <span class="font-bold" title="E-Sign">Sign {% if info.esySign ?? false %}<span class="text-green-600">&#10003;</span>{% else %}<span class="text-red-500">&#10007;</span>{% endif %}</span>
                                <span class="font-bold" title="E-Mailer">News {% if info.esyNewsletter ?? false %}<span class="text-green-600">&#10003;</span>{% else %}<span class="text-red-500">&#10007;</span>{% endif %}</span>
                                <span class="font-bold" title="E-Mail">Mail {% if info.esyMail ?? false %}<span class="text-green-600">&#10003;</span>{% else %}<span class="text-red-500">&#10007;</span>{% endif %}</span>

                                <span class="text-gray-300">|</span>
                                {# Statut paiement #}
                                {% if (info.unpaid ?? 0) > 0 %}
                                    <span class="px-1.5 py-0.5 bg-red-500/20 text-red-700 font-bold uppercase">Impayee ({{ info.unpaid }})</span>
                                {% else %}
                                    <span class="px-1.5 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase">OK</span>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td colspan="8" class="px-4 py-8 text-center text-gray-400 font-bold">Aucun client.</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    <p class="mt-3 text-xs text-gray-400">{{ customers|length }} client(s)</p>
</div>
{% endblock %}
