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

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

{% block admin_content %}
<div class="page-container">
    <h1 class="text-2xl font-bold heading-page mb-8">Administration</h1>

    {% 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' }}" style="border-color: {{ type == 'success' ? 'rgba(34,197,94,0.3)' : 'rgba(220,38,38,0.3)' }}">
                {{ message }}
            </div>
        {% endfor %}
    {% endfor %}

    <div class="flex flex-col gap-8">

        <section>
            <h2 class="text-xl font-bold uppercase mb-4">Creer un administrateur</h2>
            <div class="glass p-6">
                <form method="post" action="{{ path('app_admin_membres_create') }}" class="flex flex-col gap-4">
                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                        <div>
                            <label for="firstName" class="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-600">Prenom</label>
                            <input type="text" id="firstName" name="firstName" required
                                   class="input-glass w-full px-4 py-3 text-sm font-medium"
                                   placeholder="Prenom">
                        </div>
                        <div>
                            <label for="lastName" class="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-600">Nom</label>
                            <input type="text" id="lastName" name="lastName" required
                                   class="input-glass w-full px-4 py-3 text-sm font-medium"
                                   placeholder="Nom">
                        </div>
                    </div>
                    <div>
                        <label for="email" class="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-600">Email</label>
                        <input type="email" id="email" name="email" required
                               class="input-glass w-full px-4 py-3 text-sm font-medium"
                               placeholder="email@exemple.fr">
                    </div>
                    <div>
                        <span class="block text-xs font-bold uppercase tracking-wider mb-2 text-gray-600">Groupes d'acces</span>
                        <div class="grid grid-cols-2 md:grid-cols-4 gap-3">
                            <label for="group-member" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all" style="border-color: rgba(99,102,241,0.3);">
                                <input type="checkbox" id="group-member" name="groups[]" value="gp_member" class="accent-indigo-600" checked>
                                <span class="text-xs font-bold text-indigo-800">Membre</span>
                            </label>
                            <label for="group-admin" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all" style="border-color: rgba(220,38,38,0.3);">
                                <input type="checkbox" id="group-admin" name="groups[]" value="superadmin" class="accent-red-600">
                                <span class="text-xs font-bold text-red-800">Super Admin</span>
                            </label>
                            <label for="group-gp-asso" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-asso" name="groups[]" value="gp_asso" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Association</span>
                            </label>
                            <label for="group-gp-contest" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-contest" name="groups[]" value="gp_contest" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Concours</span>
                            </label>
                            <label for="group-gp-mail" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-mail" name="groups[]" value="gp_mail" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">E-Mail</span>
                            </label>
                            <label for="group-gp-mailling" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-mailling" name="groups[]" value="gp_mailling" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Mailing</span>
                            </label>
                            <label for="group-gp-ndd" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-ndd" name="groups[]" value="gp_ndd" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Nom de domaine</span>
                            </label>
                            <label for="group-gp-sign" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-sign" name="groups[]" value="gp_sign" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Signature</span>
                            </label>
                            <label for="group-gp-ticket" class="flex items-center gap-2 px-3 py-2 glass cursor-pointer hover:bg-white/80 transition-all">
                                <input type="checkbox" id="group-gp-ticket" name="groups[]" value="gp_ticket" class="accent-[#fabf04]">
                                <span class="text-xs font-bold">Billetterie</span>
                            </label>
                        </div>
                    </div>
                    <p class="text-xs text-gray-500">Le mot de passe sera genere automatiquement et envoye par email au nouveau membre.</p>
                    <button type="submit" class="btn-gold self-start px-6 py-3 text-sm font-bold uppercase tracking-wider text-gray-900">
                        Creer le membre
                    </button>
                </form>
            </div>
        </section>

        <section>
            <h2 class="text-xl font-bold uppercase mb-4">Membres (Keycloak)</h2>
            <div class="glass overflow-x-auto overflow-hidden">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="glass-dark" >
                            <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-wider text-white/80">Nom</th>
                            <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-wider text-white/80">Email</th>
                            <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-wider text-white/80">Statut</th>
                            <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-wider text-white/80">Email verifie</th>
                            <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-wider text-white/80">Synchro BDD</th>
                            <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-wider text-white/80">Groupes</th>
                            <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-wider text-white/80">Role local</th>
                            <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-wider text-white/80">Inscription</th>
                            <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-wider text-white/80">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for membre in membres %}
                            <tr class="border-b border-white/10 hover:bg-white/30 transition-colors">
                                <td class="px-4 py-3 font-bold">{{ membre.firstName }} {{ membre.lastName }}</td>
                                <td class="px-4 py-3 font-mono text-xs">{{ membre.email }}</td>
                                <td class="px-4 py-3 text-center">
                                    {% if membre.enabled %}
                                        <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Actif</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 py-3 text-center">
                                    {% if membre.emailVerified %}
                                        <span class="text-green-600">&#10003;</span>
                                    {% else %}
                                        <span class="text-red-500">&#10007;</span>
                                    {% endif %}
                                </td>
                                <td class="px-4 py-3 text-center">
                                    {% if membre.existsInDb %}
                                        <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Synchronise</span>
                                    {% else %}
                                        <span class="px-2 py-0.5 bg-orange-500/20 text-orange-700 font-bold uppercase text-[10px]">Non synchronise</span>
                                    {% endif %}
                                </td>
                                <td class="px-4 py-3">
                                    <div class="flex flex-wrap gap-1">
                                        {% for group in membre.groups %}
                                            <span class="px-1.5 py-0.5 bg-[#fabf04]/20 text-gray-700 font-bold text-[9px]">{{ group }}</span>
                                        {% else %}
                                            <span class="text-gray-400 text-xs">—</span>
                                        {% endfor %}
                                    </div>
                                </td>
                                <td class="px-4 py-3">
                                    {% if membre.localUser %}
                                        {% if 'ROLE_ROOT' in membre.localUser.roles %}
                                            <span class="px-2 py-0.5 bg-red-500/20 text-red-700 font-bold uppercase text-[10px]">Super Admin</span>
                                        {% elseif 'ROLE_EMPLOYE' in membre.localUser.roles %}
                                            <span class="px-2 py-0.5 bg-indigo-500/20 text-indigo-700 font-bold uppercase text-[10px]">Employe</span>
                                        {% else %}
                                            <span class="px-2 py-0.5 bg-gray-500/20 text-gray-600 font-bold uppercase text-[10px]">User</span>
                                        {% endif %}
                                    {% else %}
                                        <span class="text-gray-400 text-xs">—</span>
                                    {% endif %}
                                </td>
                                <td class="px-4 py-3 text-xs text-gray-500">
                                    {% if membre.createdTimestamp > 0 %}
                                        {{ '@' ~ (membre.createdTimestamp // 1000)|date('d/m/Y') }}
                                    {% else %}
                                        —
                                    {% endif %}
                                </td>
                                <td class="px-4 py-3 text-center">
                                    <div class="flex items-center justify-center gap-2">
                                        {% if membre.existsInDb and membre.localUser and membre.localUser.hasTempPassword %}
                                            <form method="post" action="{{ path('app_admin_membres_resend', {id: membre.localUser.id}) }}">
                                                <button type="submit" class="btn-glass px-2 py-1 text-indigo-600 font-bold uppercase text-[10px] tracking-wider">Renvoyer</button>
                                            </form>
                                        {% endif %}
                                        <form method="post" action="{{ path('app_admin_membres_delete', {keycloakId: membre.keycloakId}) }}" data-confirm="Supprimer {{ membre.firstName }} {{ membre.lastName }} ? Cette action est irreversible.">
                                            <button type="submit" class="btn-glass px-2 py-1 text-red-600 font-bold uppercase text-[10px] tracking-wider" style="border-color: rgba(220,38,38,0.3);">Supprimer</button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="9" class="px-4 py-8 text-center text-gray-400 font-bold">Aucun membre trouve.</td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            <p class="mt-3 text-xs text-gray-400">{{ membres|length }} membre(s) au total</p>
        </section>

        <section>
            <h2 class="text-xl font-bold uppercase mb-4">Groupes Keycloak</h2>
            <div class="flex flex-wrap gap-2">
                {% for group in availableGroups %}
                    <div class="glass px-4 py-2 flex items-center gap-2">
                        <span class="w-2 h-2 bg-[#fabf04]"></span>
                        <span class="text-sm font-bold">{{ group.name }}</span>
                    </div>
                {% else %}
                    <p class="text-sm text-gray-400">Aucun groupe trouve.</p>
                {% endfor %}
            </div>
        </section>

    </div>
</div>
{% endblock %}
