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

{% block title %}Acheteurs{% endblock %}

{% block body %}
<div class="mb-8">
    <h1 class="text-3xl font-black uppercase tracking-tighter italic heading-page">Acheteurs</h1>
    <p class="font-bold text-gray-500 italic">{{ buyers.getTotalItemCount }} acheteur{{ buyers.getTotalItemCount > 1 ? 's' : '' }} enregistre{{ buyers.getTotalItemCount > 1 ? 's' : '' }}.</p>
</div>

<div class="admin-card mb-8">
    <h2 class="text-sm font-black uppercase tracking-widest mb-4">Rechercher</h2>
    <form method="get" action="{{ path('app_admin_buyers') }}" class="flex gap-4 items-end">
        <div class="flex-1">
            <input type="text" name="q" value="{{ query }}"
                class="admin-form-input"
                placeholder="Rechercher par nom, prenom ou email...">
        </div>
        <button type="submit" class="admin-btn-search font-black uppercase text-xs tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Rechercher</button>
        {% if query %}
            <a href="{{ path('app_admin_buyers') }}" class="admin-btn-clear font-black uppercase text-xs tracking-widest hover:bg-gray-100 transition-all">Effacer</a>
        {% endif %}
    </form>
</div>

{% if not query %}
<div class="admin-card mb-8">
    <h2 class="text-sm font-black uppercase tracking-widest mb-4">Creer un acheteur</h2>
    <form method="post" action="{{ path('app_admin_create_buyer') }}" class="flex flex-wrap gap-4 items-end">
        <div class="flex-1 min-w-[140px]">
            <label for="create_last_name" class="admin-form-label font-black uppercase text-gray-400">Nom</label>
            <input type="text" id="create_last_name" name="last_name" required class="admin-form-input" placeholder="Dupont">
        </div>
        <div class="flex-1 min-w-[140px]">
            <label for="create_first_name" class="admin-form-label font-black uppercase text-gray-400">Prenom</label>
            <input type="text" id="create_first_name" name="first_name" required class="admin-form-input" placeholder="Jean">
        </div>
        <div class="flex-2 min-w-[200px]">
            <label for="create_email" class="admin-form-label font-black uppercase text-gray-400">Email</label>
            <input type="email" id="create_email" name="email" required class="admin-form-input" placeholder="jean.dupont@exemple.fr">
        </div>
        <button type="submit" class="admin-btn-search font-black uppercase text-xs tracking-widest hover:bg-green-500 hover:text-black transition-all">Creer</button>
    </form>
</div>
{% endif %}

<div class="admin-card !p-0">
    <table class="admin-table">
        <thead>
            <tr>
                <th class="text-[10px] font-black uppercase tracking-widest text-white">Nom</th>
                <th class="text-[10px] font-black uppercase tracking-widest text-white">Email</th>
                <th class="text-[10px] font-black uppercase tracking-widest text-white">Inscription</th>
                <th class="text-[10px] font-black uppercase tracking-widest text-white text-center">Email verifie</th>
                <th class="text-[10px] font-black uppercase tracking-widest text-white text-right">Actions</th>
            </tr>
        </thead>
        <tbody>
            {% for buyer in buyers %}
            <tr class="hover:bg-gray-50 transition-all">
                <td class="font-bold text-sm">{{ buyer.firstName }} {{ buyer.lastName }}</td>
                <td class="text-sm text-gray-600">{{ buyer.email }}</td>
                <td class="text-sm text-gray-400">{{ buyer.createdAt|date('d/m/Y') }}</td>
                <td class="text-center">
                    {% if buyer.verified %}
                        <span class="admin-badge-green text-xs font-black uppercase">Oui</span>
                    {% else %}
                        <span class="admin-badge-red text-xs font-black uppercase">Non</span>
                    {% endif %}
                </td>
                <td class="text-right">
                    <div class="flex gap-2 justify-end">
                        {% if not buyer.verified %}
                            <form method="post" action="{{ path('app_admin_resend_verification', {id: buyer.id}) }}">
                                <button type="submit" class="admin-btn-sm-white text-xs font-black uppercase tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Renvoyer</button>
                            </form>
                            <form method="post" action="{{ path('app_admin_force_verification', {id: buyer.id}) }}">
                                <button type="submit" class="admin-btn-sm-yellow text-xs font-black uppercase tracking-widest hover:bg-green-500 hover:text-black transition-all">Forcer</button>
                            </form>
                        {% else %}
                            <form method="post" action="{{ path('app_admin_reset_password', {id: buyer.id}) }}">
                                <button type="submit" class="admin-btn-sm-white text-xs font-black uppercase tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Reset MDP</button>
                            </form>
                        {% endif %}
                        <form method="post" action="{{ path('app_admin_delete_buyer', {id: buyer.id}) }}" data-confirm="Etes-vous sur de vouloir supprimer le compte de {{ buyer.firstName }} {{ buyer.lastName }} ({{ buyer.email }}) ? Cette action est irreversible.">
                            <button type="submit" class="admin-btn-sm-danger text-xs font-black uppercase tracking-widest hover:bg-red-800 transition-all">Supprimer</button>
                        </form>
                    </div>
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="5" class="!text-center !py-12 text-gray-400 font-bold text-sm">Aucun acheteur enregistre.</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>

{% if buyers.getTotalItemCount > 10 %}
<div class="flex justify-center gap-2 mt-6">
    {% for page in 1..buyers.getPageCount %}
        {% if page == buyers.getCurrentPageNumber %}
            <span class="admin-page-active text-xs font-black">{{ page }}</span>
        {% else %}
            <a href="{{ path('app_admin_buyers', {page: page}) }}" class="admin-page-link text-xs font-black hover:bg-gray-100 transition-all">{{ page }}</a>
        {% endif %}
    {% endfor %}
</div>
{% endif %}
{% endblock %}
