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

{% block title %}Mon compte - E-Ticket{% endblock %}

{% block body %}
<div class="page-container-lg">
    <div class="mb-8">
        <h1 class="text-3xl font-black uppercase tracking-tighter italic heading-page">Mon compte</h1>
        <p class="font-bold text-gray-500 italic">Bonjour {{ app.user.firstName }}, bienvenue sur votre espace.</p>
    </div>

    {% for message in app.flashes('success') %}
        <div class="flash-success">
            <p class="font-black text-sm">{{ message }}</p>
        </div>
    {% endfor %}
    {% for message in app.flashes('error') %}
        <div class="flash-error">
            <p class="font-black text-sm">{{ message }}</p>
        </div>
    {% endfor %}

    {% if isOrganizer and not app.user.approved %}
    <div class="card-brutal-warn p-8 text-center">
        <div class="text-4xl mb-4">&#9203;</div>
        <h2 class="text-xl font-black uppercase tracking-tighter italic mb-2">Compte en cours de validation</h2>
        <p class="font-bold text-gray-700 text-sm">Votre compte organisateur est en cours de validation par l'equipe E-Ticket. Vous recevrez un email une fois votre compte approuve.</p>
        <p class="text-gray-500 text-xs font-bold mt-4">Contactez <a href="mailto:contact@e-cosplay.fr" class="text-indigo-800 underline hover:text-indigo-950">contact@e-cosplay.fr</a> pour toute question.</p>
    </div>

    {% else %}

    {% if isOrganizer and app.user.debt is not null and app.user.debt > 0 %}
    <div class="card-brutal-error p-6 mb-8">
        <div class="flex items-center gap-3 mb-2">
            <span class="text-2xl">&#9888;</span>
            <h2 class="text-sm font-black uppercase tracking-widest text-red-800">Dette en cours</h2>
        </div>
        <p class="font-bold text-gray-700 text-sm">Suite a des remboursements ou litiges, votre compte presente une dette de <strong class="text-red-700">{{ (app.user.debt / 100)|number_format(2, ',', ' ') }} &euro;</strong>. Les paiements de vos clients seront retenus jusqu'a l'apurement complet de cette dette.</p>
    </div>
    {% endif %}

    {% if isOrganizer and app.user.billing and app.user.billingState != 'good' %}
    <div class="card-brutal-error p-8 text-center mb-8">
        <div class="text-4xl mb-4">&#9888;</div>
        {% if app.user.billingState == 'suspendu' %}
            <h2 class="text-xl font-black uppercase tracking-tighter italic mb-2">Abonnement suspendu</h2>
            <p class="font-bold text-gray-700 text-sm mb-4">Votre abonnement a ete suspendu suite a un echec de paiement. Vos evenements ne sont plus accessibles. Regularisez votre situation pour reactiver votre compte.</p>
        {% else %}
            <h2 class="text-xl font-black uppercase tracking-tighter italic mb-2">Abonnement requis</h2>
            <p class="font-bold text-gray-700 text-sm mb-4">Vous devez regler les frais de votre abonnement pour utiliser notre plateforme. Votre abonnement mensuel est de <strong>{{ (app.user.billingAmount / 100)|number_format(2, ',', ' ') }} &euro;</strong>.</p>
        {% endif %}
        <a href="{{ path('app_account_billing_subscribe') }}" class="btn-brutal font-black uppercase text-xs tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Regler mon abonnement</a>
    </div>
    {% endif %}

    {% if isOrganizer and (not app.user.billing or app.user.billingState == 'good') %}
        {% if not app.user.stripeAccountId %}
        <div class="card-brutal-warn mb-8">
            <h2 class="text-sm font-black uppercase tracking-widest mb-2">Configuration Stripe requise
                <span class="bg-white border-2 border-gray-900 px-1.5 py-0.5 text-[10px] ml-2">Statut : non configure</span>
            </h2>
            <p class="text-sm font-bold text-gray-700 mb-4">Pour creer vos evenements, vendre des billets et recevoir vos paiements, vous devez creer votre compte vendeur via Stripe.</p>
            <a href="{{ path('app_account_stripe_connect') }}" class="btn-brutal font-black uppercase text-xs tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Creer mon compte Stripe</a>
        </div>

        {% elseif not app.user.stripeChargesEnabled and not app.user.stripePayoutsEnabled %}
        <div class="card-brutal-warn mb-8">
            <h2 class="text-sm font-black uppercase tracking-widest mb-2">Verification Stripe en cours
                <span class="bg-white border-2 border-gray-900 px-1.5 py-0.5 text-[10px] ml-2">Statut : {{ app.user.stripeStatus ?? 'en attente' }}</span>
            </h2>
            <p class="text-sm font-bold text-gray-700 mb-4">Votre compte Stripe est en cours de verification. Merci de patienter, vous serez notifie une fois la verification terminee.</p>
            <div class="flex gap-3 flex-wrap">
                <a href="{{ path('app_account_stripe_connect') }}" class="px-4 py-2 border-3 border-gray-900 bg-white inline-block font-black uppercase text-xs tracking-widest hover:bg-gray-100 transition-all">Completer ma verification</a>
                <form method="post" action="{{ path('app_account_stripe_cancel') }}" data-confirm="Etes-vous sur de vouloir annuler la creation de votre compte Stripe ?" class="inline">
                    <button type="submit" class="px-4 py-2 border-3 border-red-800 bg-red-600 text-white cursor-pointer font-black uppercase text-xs tracking-widest hover:bg-red-800 transition-all">Annuler la creation</button>
                </form>
            </div>
        </div>

        {% elseif app.user.stripeChargesEnabled and app.user.stripePayoutsEnabled %}
        <div class="card-brutal-green flex justify-between items-center flex-wrap gap-4 mb-8">
            <p class="font-black text-sm">Stripe Connect actif — Paiements et virements actives.
                <span class="bg-white border-2 border-gray-900 px-1.5 py-0.5 text-[10px] ml-2">Statut : {{ app.user.stripeStatus }}</span>
            </p>
            <div class="flex gap-2 flex-wrap">
                <a href="{{ path('app_account_stripe_dashboard') }}" target="_blank" class="px-3 py-1.5 border-2 border-gray-900 bg-white inline-block text-xs font-black uppercase tracking-widest hover:bg-gray-100 transition-all">Dashboard Stripe</a>
                <form method="post" action="{{ path('app_account_stripe_cancel') }}" data-confirm="Etes-vous sur de vouloir cloturer votre compte Stripe ? Vous ne pourrez plus recevoir de paiements." class="inline">
                    <button type="submit" class="px-3 py-1.5 border-2 border-red-800 bg-red-600 text-white cursor-pointer text-xs font-black uppercase tracking-widest hover:bg-red-800 transition-all">Cloturer</button>
                </form>
            </div>
        </div>

        {% else %}
        <div class="card-brutal-error mb-8">
            <h2 class="text-sm font-black uppercase tracking-widest mb-2 text-red-800">Compte Stripe refuse
                <span class="bg-white border-2 border-red-800 px-1.5 py-0.5 text-[10px] ml-2">Statut : {{ app.user.stripeStatus ?? 'refuse' }}</span>
            </h2>
            <p class="text-sm font-bold text-gray-700">Stripe a refuse votre compte vendeur. Vous ne pouvez pas utiliser nos services de vente pour le moment. Contactez <a href="mailto:contact@e-cosplay.fr" class="text-indigo-800 underline hover:text-indigo-950">contact@e-cosplay.fr</a> pour plus d'informations.</p>
        </div>
        {% endif %}
    {% endif %}

    {% set stripeReady = app.user.stripeChargesEnabled and app.user.stripePayoutsEnabled %}

    <div class="flex flex-wrap overflow-x-auto mb-8">
        {% set isSubAccount = app.user.parentOrganizer is not null %}
        {% if isOrganizer and stripeReady %}
            {% if not isSubAccount or app.user.hasPermission('events') %}
                <a href="{{ path('app_account', {tab: 'events'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'events' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Evenements / Brocantes</a>
            {% endif %}
            {% if not isSubAccount %}
                <a href="{{ path('app_account', {tab: 'subaccounts'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'subaccounts' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Sous-comptes</a>
                <a href="{{ path('app_account', {tab: 'payouts'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'payouts' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Virements</a>
            {% endif %}
        {% endif %}
        <a href="{{ path('app_account', {tab: 'tickets'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'tickets' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Billets</a>
        <a href="{{ path('app_account', {tab: 'purchases'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'purchases' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Achats</a>
        <a href="{{ path('app_account', {tab: 'invoices'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 border-r-0 {{ tab == 'invoices' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Factures</a>
        <a href="{{ path('app_account', {tab: 'settings'}) }}" class="flex-1 min-w-[100px] text-center py-3 border-3 border-gray-900 {{ tab == 'settings' ? 'bg-yellow-400' : 'bg-white' }} font-black uppercase text-xs tracking-widest transition-all">Parametres</a>
    </div>

    {% if tab == 'tickets' %}
    <div class="card-brutal overflow-hidden">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Mes billets</h2>
        </div>
        {% if userTickets|length > 0 %}
        <div class="p-6">
            {% for ticket in userTickets %}
            {% set order = ticket.billetBuyer %}
            {% set evt = order.event %}
            <div class="border-2 border-gray-900 bg-white mb-4 overflow-hidden">
                <div class="flex flex-col md:flex-row">
                    <div class="flex-1 p-4">
                        <div class="flex flex-wrap items-center gap-3 mb-2">
                            <span class="font-black uppercase text-sm">{{ ticket.billetName }}</span>
                            {% if ticket.state == 'valid' %}
                                <span class="badge-green text-[10px] font-black uppercase">Actif</span>
                            {% elseif ticket.state == 'expired' %}
                                <span class="badge-yellow text-[10px] font-black uppercase">Expire</span>
                            {% else %}
                                <span class="badge-red text-[10px] font-black uppercase">{{ ticket.state == 'invalid' ? 'Annule' : ticket.state }}</span>
                            {% endif %}
                            {% if ticket.firstScannedAt %}
                                <span class="text-[10px] font-bold text-gray-400">Scanne le {{ ticket.firstScannedAt|date('d/m/Y H:i') }}</span>
                            {% endif %}
                        </div>

                        <div class="space-y-1 text-sm font-bold text-gray-600 mb-3">
                            <p class="font-black text-gray-900">{{ evt.title }}</p>
                            <p>{{ evt.startAt|date('d/m/Y') }} — {{ evt.startAt|date('H:i') }} a {{ evt.endAt|date('H:i') }}</p>
                            <p>{{ evt.address }}, {{ evt.zipcode }} {{ evt.city }}</p>
                        </div>

                        <div class="flex flex-wrap gap-4 text-xs font-bold text-gray-400">
                            <span>Ref: <span class="font-mono text-gray-600">{{ ticket.reference }}</span></span>
                            <span>Commande: <span class="text-gray-600">{{ order.orderNumber }}</span></span>
                            <span>Prix: <span class="text-indigo-600 font-black">{{ ticket.unitPriceHTDecimal|number_format(2, ',', ' ') }} &euro;</span></span>
                        </div>
                    </div>

                    <div class="flex items-center gap-2 p-4 border-t md:border-t-0 md:border-l border-gray-200">
                        <a href="{{ path('app_order_download_ticket', {orderNumber: order.orderNumber, token: order.accessToken, ticketReference: ticket.reference}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-indigo-600 hover:text-white transition-all" target="_blank">
                            Telecharger PDF
                        </a>
                        <a href="{{ path('app_order_public', {orderNumber: order.orderNumber, token: order.accessToken}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-gray-100 transition-all">
                            Commande
                        </a>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucun billet pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% elseif tab == 'purchases' %}
    <div class="card-brutal overflow-hidden">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Mes achats</h2>
        </div>
        {% set filtered_orders = orders|filter(o => o.status != 'pending') %}
        {% if filtered_orders|length > 0 %}
        <div class="p-6">
            {% for order in filtered_orders %}
            <div class="border-2 border-gray-900 bg-white mb-4 p-4">
                <div class="flex flex-wrap items-center gap-3 mb-3">
                    <span class="font-black uppercase text-sm">{{ order.orderNumber }}</span>
                    {% if order.status == 'paid' %}
                        <span class="badge-green text-[10px] font-black uppercase">Payee</span>
                    {% elseif order.status == 'refunded' %}
                        <span class="badge-yellow text-[10px] font-black uppercase">Remboursee</span>
                    {% elseif order.status == 'cancelled' %}
                        <span class="badge-red text-[10px] font-black uppercase">Annulee</span>
                    {% endif %}
                    <span class="text-xs font-bold text-gray-400">{{ order.createdAt|date('d/m/Y H:i') }}</span>
                </div>

                <div class="space-y-1 text-sm font-bold text-gray-600 mb-3">
                    <p class="font-black text-gray-900">{{ order.event.title }}</p>
                    <p>{{ order.event.startAt|date('d/m/Y') }} — {{ order.event.startAt|date('H:i') }} a {{ order.event.endAt|date('H:i') }}</p>
                    <p>{{ order.event.address }}, {{ order.event.zipcode }} {{ order.event.city }}</p>
                </div>

                <div class="flex flex-wrap items-center gap-4 mb-3">
                    {% for item in order.items %}
                    <div class="text-xs font-bold text-gray-500">
                        {{ item.billetName }} x{{ item.quantity }} — {{ item.lineTotalHTDecimal|number_format(2, ',', ' ') }} &euro;
                    </div>
                    {% endfor %}
                </div>

                <div class="flex flex-wrap items-center justify-between gap-3">
                    <div class="flex flex-wrap gap-3 text-xs font-bold text-gray-400">
                        <span>Total: <span class="font-black text-indigo-600">{{ order.totalHTDecimal|number_format(2, ',', ' ') }} &euro;</span></span>
                        {% if order.paymentMethod %}
                            <span>{{ order.paymentMethod }}{% if order.cardBrand and order.cardLast4 %} — {{ order.cardBrand|upper }} **** {{ order.cardLast4 }}{% endif %}</span>
                        {% endif %}
                    </div>
                    <div class="flex gap-2">
                        {% if order.status == 'paid' %}
                        <a href="{{ path('app_order_invoice', {orderNumber: order.orderNumber, token: order.accessToken}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-gray-100 transition-all" target="_blank">
                            Facture
                        </a>
                        {% endif %}
                        <a href="{{ path('app_order_public', {orderNumber: order.orderNumber, token: order.accessToken}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-indigo-600 hover:text-white transition-all">
                            Commande
                        </a>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucun achat pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% elseif tab == 'invoices' %}
    <div class="card-brutal overflow-hidden">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Mes factures</h2>
        </div>
        {% set paid_orders = orders|filter(o => o.status == 'paid') %}
        {% if paid_orders|length > 0 %}
        <div class="p-6">
            {% for order in paid_orders %}
            <div class="flex flex-wrap items-center gap-4 py-3 {{ not loop.last ? 'border-b border-gray-200' : '' }}">
                <div class="flex-1 min-w-0">
                    <p class="font-black uppercase text-sm">Facture {{ order.orderNumber }}</p>
                    <p class="text-xs font-bold text-gray-500">{{ order.event.title }} — {{ order.paidAt ? order.paidAt|date('d/m/Y') : order.createdAt|date('d/m/Y') }}</p>
                </div>
                <span class="font-black text-sm text-indigo-600">{{ order.totalHTDecimal|number_format(2, ',', ' ') }} &euro;</span>
                <a href="{{ path('app_order_invoice', {orderNumber: order.orderNumber, token: order.accessToken}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-indigo-600 hover:text-white transition-all" target="_blank">
                    Telecharger
                </a>
            </div>
            {% endfor %}
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucune facture pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% elseif tab == 'events' and stripeReady %}
    <div class="flex flex-wrap items-center justify-between gap-4 mb-6">
        <a href="{{ path('app_account_create_event') }}" class="btn-brutal font-black uppercase text-xs tracking-widest hover:bg-indigo-600 hover:text-white transition-all">
            + Creer un evenement
        </a>
        <form method="get" action="{{ path('app_account') }}" class="flex gap-2">
            <input type="hidden" name="tab" value="events">
            <input type="text" name="q" value="{{ app.request.query.get('q', '') }}" class="form-input max-w-[300px]" placeholder="Rechercher un evenement...">
            <button type="submit" class="px-4 py-2 border-2 border-gray-900 bg-[#fabf04] font-black uppercase text-xs tracking-widest cursor-pointer hover:bg-indigo-600 hover:text-white transition-all">Rechercher</button>
        </form>
    </div>

    {% if isOrganizer and (not app.user.stripeChargesEnabled or not app.user.stripePayoutsEnabled) %}
    <div class="card-brutal-warn mb-6">
        <p class="font-black text-sm">Configuration Stripe ou validation est requise !</p>
    </div>
    {% endif %}

    <div class="card-brutal overflow-hidden">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Mes evenements / Brocantes / Reservations</h2>
        </div>
        {% if events|length > 0 %}
        <div class="overflow-x-auto">
        <table class="w-full border-collapse">
            <thead>
                <tr class="bg-gray-50 border-b-2 border-gray-200">
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Evenement</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Date</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Lieu</th>
                    <th class="px-6 py-3 text-center text-[10px] font-black uppercase tracking-widest text-gray-400">Statut</th>
                    <th class="px-6 py-3 text-right text-[10px] font-black uppercase tracking-widest text-gray-400">Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for event in events %}
                <tr class="border-b border-gray-200 hover:bg-gray-50 transition-colors">
                    <td class="px-6 py-4">
                        <p class="font-black text-sm">{{ event.title }}</p>
                    </td>
                    <td class="px-6 py-4">
                        <p class="text-sm font-bold">{{ event.startAt|date('d/m/Y') }}</p>
                        <p class="text-xs text-gray-400 font-bold">{{ event.startAt|date('H:i') }} - {{ event.endAt|date('H:i') }}</p>
                    </td>
                    <td class="px-6 py-4">
                        <p class="text-sm font-bold">{{ event.city }}</p>
                        <p class="text-xs text-gray-400 font-bold">{{ event.zipcode }}</p>
                    </td>
                    <td class="px-6 py-4 text-center">
                        {% if event.online %}
                            <span class="badge-green text-xs font-black uppercase">En ligne</span>
                        {% else %}
                            <span class="badge-red text-xs font-black uppercase">Hors ligne</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 text-right">
                        {% set evt_url = url('app_event_detail', {orgaSlug: app.user.slug, id: event.id, eventSlug: event.slug}) %}
                        <div class="flex gap-2 justify-end flex-wrap">
                            <a href="{{ path('app_account_edit_event', {id: event.id}) }}" class="px-3 py-1 border-2 border-gray-900 bg-white text-xs font-black uppercase tracking-widest hover:bg-gray-100 transition-all">Modifier</a>
                            <a href="https://twitter.com/intent/tweet?text={{ (event.title)|url_encode }}&url={{ evt_url|url_encode }}" target="_blank" rel="noopener" class="w-8 h-8 flex items-center justify-center border-2 border-gray-900 bg-black text-white hover:opacity-80 transition-all" aria-label="Partager sur X" title="Partager sur X">
                                <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 24 24"><path d="M18.244 2.25h3.308l-7.227 8.26 8.502 11.24H16.17l-5.214-6.817L4.99 21.75H1.68l7.73-8.835L1.254 2.25H8.08l4.713 6.231zm-1.161 17.52h1.833L7.084 4.126H5.117z"/></svg>
                            </a>
                            <a href="https://www.facebook.com/sharer/sharer.php?u={{ evt_url|url_encode }}" target="_blank" rel="noopener" class="w-8 h-8 flex items-center justify-center border-2 border-gray-900 bg-[#1877F2] text-white hover:opacity-80 transition-all" aria-label="Partager sur Facebook" title="Partager sur Facebook">
                                <svg class="w-3 h-3" fill="currentColor" viewBox="0 0 24 24"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
                            </a>
                            <button type="button" class="w-8 h-8 flex items-center justify-center border-2 border-gray-900 bg-white hover:bg-gray-900 hover:text-white transition-all cursor-pointer" aria-label="Copier le lien" title="Copier le lien" data-share-copy="{{ evt_url }}">
                                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"/></svg>
                            </button>
                            <a href="{{ path('app_account_event_qrcode', {id: event.id}) }}" class="w-8 h-8 flex items-center justify-center border-2 border-gray-900 bg-white hover:bg-gray-900 hover:text-white transition-all" aria-label="QR Code" title="Telecharger QR Code">
                                <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v1m6 11h2m-6 0h-2v4m0-11v3m0 0h.01M12 12h4.01M16 20h4M4 12h4m12 0h.01M5 8h2a1 1 0 001-1V5a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1zm12 0h2a1 1 0 001-1V5a1 1 0 00-1-1h-2a1 1 0 00-1 1v2a1 1 0 001 1zM5 20h2a1 1 0 001-1v-2a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1z"/></svg>
                            </a>
                            <form method="post" action="{{ path('app_account_delete_event', {id: event.id}) }}" data-confirm="Etes-vous sur de vouloir supprimer cet evenement ?" class="inline">
                                <button type="submit" class="px-3 py-1 border-2 border-red-800 bg-red-600 text-white text-xs font-black uppercase tracking-widest hover:bg-red-800 transition-all cursor-pointer">Supprimer</button>
                            </form>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucun evenement pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% if events.getTotalItemCount > 10 %}
    <div class="mt-6">
        {{ knp_pagination_render(events) }}
    </div>
    {% endif %}

    {% elseif tab == 'subaccounts' and stripeReady %}

    <div class="card-brutal p-6 mb-8">
        <h2 class="text-sm font-black uppercase tracking-widest mb-4">Creer un sous-compte</h2>
        <form method="post" action="{{ path('app_account_create_subaccount') }}" class="flex flex-col gap-4">
            <div class="flex flex-wrap gap-4">
                <div class="flex-1 min-w-[140px]">
                    <label for="sub_last_name" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Nom</label>
                    <input type="text" id="sub_last_name" name="last_name" required class="w-full px-3 py-2 border-2 border-gray-900 font-bold outline-none" placeholder="Dupont">
                </div>
                <div class="flex-1 min-w-[140px]">
                    <label for="sub_first_name" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Prenom</label>
                    <input type="text" id="sub_first_name" name="first_name" required class="w-full px-3 py-2 border-2 border-gray-900 font-bold outline-none" placeholder="Jean">
                </div>
                <div class="flex-[2] min-w-[200px]">
                    <label for="sub_email" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Email</label>
                    <input type="email" id="sub_email" name="email" required class="w-full px-3 py-2 border-2 border-gray-900 font-bold outline-none" placeholder="collaborateur@exemple.fr">
                </div>
            </div>
            <div>
                <p class="text-[10px] tracking-widest mb-2 font-black uppercase text-gray-400">Permissions</p>
                <div class="flex flex-wrap gap-4">
                    <label class="flex items-center gap-2 cursor-pointer text-sm font-bold">
                        <input type="checkbox" name="permissions[]" value="scanner" class="w-4 h-4"> Scanner (valider les billets)
                    </label>
                    <label class="flex items-center gap-2 cursor-pointer text-sm font-bold">
                        <input type="checkbox" name="permissions[]" value="events" class="w-4 h-4"> Evenements (creer, modifier, supprimer)
                    </label>
                    <label class="flex items-center gap-2 cursor-pointer text-sm font-bold">
                        <input type="checkbox" name="permissions[]" value="tickets" class="w-4 h-4"> Billets (invitations gratuites)
                    </label>
                </div>
            </div>
            <button type="submit" class="px-4 py-2 border-2 border-gray-900 bg-yellow-400 cursor-pointer self-start font-black uppercase text-xs tracking-widest hover:bg-green-500 hover:text-black transition-all">Creer</button>
        </form>
    </div>

    <div class="card-brutal">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Sous-comptes</h2>
        </div>
        {% if subAccounts|length > 0 %}
        <div class="overflow-x-auto">
        <table class="detail-table">
            <thead>
                <tr class="border-b-2 border-gray-200">
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Nom</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Email</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Permissions</th>
                    <th class="px-6 py-3 text-right text-[10px] font-black uppercase tracking-widest text-gray-400">Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for sub in subAccounts %}
                <tr class="border-b border-gray-200 hover:bg-gray-50 transition-all">
                    <td class="px-6 py-3 font-bold text-sm">{{ sub.firstName }} {{ sub.lastName }}</td>
                    <td class="px-6 py-3 text-sm text-gray-600">{{ sub.email }}</td>
                    <td class="px-6 py-3">
                        {% for perm in sub.subAccountPermissions ?? [] %}
                            {% if perm == 'scanner' %}
                                <span class="bg-blue-100 border-2 border-gray-900 px-1.5 py-0.5 text-xs font-black uppercase">Scanner</span>
                            {% elseif perm == 'events' %}
                                <span class="bg-indigo-100 border-2 border-gray-900 px-1.5 py-0.5 text-xs font-black uppercase">Evenements</span>
                            {% elseif perm == 'tickets' %}
                                <span class="bg-amber-100 border-2 border-gray-900 px-1.5 py-0.5 text-xs font-black uppercase">Billets</span>
                            {% endif %}
                        {% endfor %}
                    </td>
                    <td class="px-6 py-3 text-right">
                        <div class="flex gap-2 justify-end">
                            <a href="{{ path('app_account_edit_subaccount_page', {id: sub.id}) }}" class="border-2 border-gray-900 px-2 py-1 bg-yellow-400 inline-block text-xs font-black uppercase tracking-widest hover:bg-indigo-600 hover:text-black transition-all">Editer</a>
                            <form method="post" action="{{ path('app_account_delete_subaccount', {id: sub.id}) }}" data-confirm="Supprimer le sous-compte de {{ sub.firstName }} {{ sub.lastName }} ?" class="inline">
                                <button type="submit" class="border-2 border-red-800 px-2 py-1 bg-red-600 text-white cursor-pointer text-xs font-black uppercase tracking-widest hover:bg-red-800 transition-all">Supprimer</button>
                            </form>
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucun sous-compte pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% elseif tab == 'payouts' and stripeReady %}
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-6">
        <div class="card-brutal p-4 text-center">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">Encaisse</div>
            <div class="text-xl font-black text-green-600">{{ financeStats.paid|number_format(2, ',', ' ') }} &euro;</div>
        </div>
        <div class="card-brutal p-4 text-center">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">En attente</div>
            <div class="text-xl font-black text-yellow-600">{{ financeStats.pending|number_format(2, ',', ' ') }} &euro;</div>
        </div>
        <div class="card-brutal p-4 text-center">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">Rembourse</div>
            <div class="text-xl font-black text-red-600">{{ financeStats.refunded|number_format(2, ',', ' ') }} &euro;</div>
        </div>
        <div class="card-brutal p-4 text-center">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">Com E-Ticket</div>
            <div class="text-xl font-black text-red-500">{{ financeStats.commissionEticket|number_format(2, ',', ' ') }} &euro;</div>
        </div>
        <div class="card-brutal p-4 text-center">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">Com Stripe</div>
            <div class="text-xl font-black text-red-500">{{ financeStats.commissionStripe|number_format(2, ',', ' ') }} &euro;</div>
        </div>
        <div class="card-brutal p-4 text-center bg-green-50">
            <div class="text-[10px] font-black uppercase tracking-widest text-gray-500 mb-1">Net percu</div>
            <div class="text-xl font-black text-green-600">{{ financeStats.net|number_format(2, ',', ' ') }} &euro;</div>
        </div>
    </div>

    <div class="card-brutal overflow-hidden mb-6">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Export mensuel</h2>
        </div>
        <div class="p-6 flex flex-wrap gap-3">
            <a href="{{ path('app_account_export', {year: "now"|date('Y'), month: "now"|date('n')}) }}" class="px-3 py-2 border-2 border-gray-900 bg-[#fabf04] text-xs font-black uppercase hover:bg-yellow-500 transition-all">CSV {{ "now"|date('m/Y') }}</a>
            <a href="{{ path('app_account_export', {year: "now"|date_modify('-1 month')|date('Y'), month: "now"|date_modify('-1 month')|date('n')}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-gray-100 transition-all">CSV {{ "now"|date_modify('-1 month')|date('m/Y') }}</a>
            <a href="{{ path('app_account_export_pdf', {year: "now"|date('Y'), month: "now"|date('n')}) }}" class="px-3 py-2 border-2 border-gray-900 bg-indigo-600 text-white text-xs font-black uppercase hover:bg-indigo-800 transition-all">PDF {{ "now"|date('m/Y') }}</a>
            <a href="{{ path('app_account_export_pdf', {year: "now"|date_modify('-1 month')|date('Y'), month: "now"|date_modify('-1 month')|date('n')}) }}" class="px-3 py-2 border-2 border-gray-900 bg-white text-xs font-black uppercase hover:bg-gray-100 transition-all">PDF {{ "now"|date_modify('-1 month')|date('m/Y') }}</a>
        </div>
    </div>

    <div class="card-brutal">
        <div class="section-header">
            <h2 class="text-[10px] font-black uppercase tracking-widest text-white">Mes virements</h2>
        </div>
        {% if isOrganizer and (not app.user.stripeChargesEnabled or not app.user.stripePayoutsEnabled) %}
        <div class="p-6 bg-amber-100 border-b-2 border-gray-200">
            <p class="font-black text-sm">Configuration Stripe ou validation est requise !</p>
        </div>
        {% endif %}
        {% if payouts|length > 0 %}
        <div class="overflow-x-auto">
        <table class="detail-table">
            <thead>
                <tr class="border-b-2 border-gray-200">
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">ID</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Montant</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Statut</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Destination</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Arrivee</th>
                    <th class="px-6 py-3 text-left text-[10px] font-black uppercase tracking-widest text-gray-400">Date</th>
                    <th class="px-6 py-3 text-right text-[10px] font-black uppercase tracking-widest text-gray-400">Attestation</th>
                </tr>
            </thead>
            <tbody>
                {% for payout in payouts %}
                <tr class="border-b border-gray-200 hover:bg-gray-50 transition-all">
                    <td class="px-6 py-3 text-xs font-mono text-gray-500">{{ payout.stripePayoutId }}</td>
                    <td class="px-6 py-3 text-sm font-bold">{{ payout.amountDecimal|number_format(2, ',', ' ') }} {{ payout.currency|upper }}</td>
                    <td class="px-6 py-3">
                        {% if payout.status == 'paid' %}
                            <span class="bg-green-100 border-2 border-gray-900 px-2 py-0.5 text-xs font-black uppercase">{{ payout.status }}</span>
                        {% elseif payout.status == 'failed' or payout.status == 'canceled' %}
                            <span class="bg-red-100 border-2 border-gray-900 px-2 py-0.5 text-xs font-black uppercase">{{ payout.status }}</span>
                        {% else %}
                            <span class="bg-amber-100 border-2 border-gray-900 px-2 py-0.5 text-xs font-black uppercase">{{ payout.status }}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-3 text-xs font-mono text-gray-500">{{ payout.destination ?? '—' }}</td>
                    <td class="px-6 py-3 text-sm text-gray-500">{{ payout.arrivalDate ? payout.arrivalDate|date('d/m/Y') : '—' }}</td>
                    <td class="px-6 py-3 text-sm text-gray-400">{{ payout.createdAt|date('d/m/Y H:i') }}</td>
                    <td class="px-6 py-3 text-right">
                        {% if payout.status == 'paid' %}
                            <a href="{{ path('app_account_payout_pdf', {id: payout.id}) }}" target="_blank" class="border-2 border-gray-900 px-2 py-1 bg-yellow-400 inline-block text-xs font-black uppercase tracking-widest hover:bg-indigo-600 hover:text-black transition-all">PDF</a>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
        {% else %}
        <div class="p-12 text-center">
            <p class="text-gray-400 font-bold text-sm">Aucun virement pour le moment.</p>
        </div>
        {% endif %}
    </div>

    {% elseif tab == 'settings' %}
    <div class="card-brutal p-6">
        <h2 class="text-sm font-black uppercase tracking-widest mb-4">Parametres du compte</h2>
        <form method="post" action="{{ path('app_account_settings') }}" enctype="multipart/form-data" class="form-col">
            <div class="form-row">
                <div class="form-group">
                    <label for="settings_last_name" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Nom</label>
                    <input type="text" id="settings_last_name" name="last_name" value="{{ app.user.lastName }}" required
                        class="form-input {{ isOrganizer ? 'bg-gray-100' : '' }}"
                        {{ isOrganizer ? 'disabled' : '' }}>
                </div>
                <div class="form-group">
                    <label for="settings_first_name" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Prenom</label>
                    <input type="text" id="settings_first_name" name="first_name" value="{{ app.user.firstName }}" required
                        class="form-input {{ isOrganizer ? 'bg-gray-100' : '' }}"
                        {{ isOrganizer ? 'disabled' : '' }}>
                </div>
            </div>

            <div>
                <label for="settings_email" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Email</label>
                <input type="email" id="settings_email" name="email" value="{{ app.user.email }}" required
                    class="form-input">
            </div>

            <div>
                <label for="settings_phone" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Telephone</label>
                <input type="tel" id="settings_phone" name="phone" value="{{ app.user.phone }}"
                    class="form-input">
            </div>

            {% if not isOrganizer %}
            <div>
                <label for="settings_address" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Adresse</label>
                <input type="text" id="settings_address" name="address" value="{{ app.user.address }}"
                    class="form-input">
            </div>

            <div class="form-row">
                <div class="flex-1 min-w-[120px]">
                    <label for="settings_postal" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Code postal</label>
                    <input type="text" id="settings_postal" name="postal_code" value="{{ app.user.postalCode }}" maxlength="5"
                        class="form-input">
                </div>
                <div class="flex-[2] min-w-[200px]">
                    <label for="settings_city" class="text-[10px] tracking-widest block mb-2 font-black uppercase text-gray-400">Ville</label>
                    <input type="text" id="settings_city" name="city" value="{{ app.user.city }}"
                        class="form-input">
                </div>
            </div>
            {% endif %}

            {% if isOrganizer %}
            <div class="border-4 border-gray-900 bg-yellow-400 p-6">
                <h3 class="text-[10px] tracking-widest mb-4 font-black uppercase">Logo de l'organisation</h3>
                {% if app.user.logoName %}
                <div class="mb-4">
                    <img src="{{ ('/uploads/logos/' ~ app.user.logoName) | imagine_filter('organizer_logo') }}" alt="Logo" class="max-h-20 border-2 border-gray-900">
                </div>
                {% endif %}
                <input type="file" name="logo" accept="image/png,image/jpeg,image/webp"
                    class="p-2 border-2 border-gray-900 bg-white font-bold w-full">
                <p class="text-xs font-bold mt-2">PNG, JPEG ou WebP. Max 2 Mo.</p>
            </div>

            <div class="border-4 border-gray-900 bg-white p-6">
                <h3 class="text-[10px] tracking-widest mb-4 font-black uppercase text-gray-400">Reseaux sociaux & site internet</h3>
                <div class="flex flex-col gap-4">
                    <div>
                        <label for="settings_website" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Site internet</label>
                        <input type="url" id="settings_website" name="website" value="{{ app.user.website ?? '' }}"
                            class="w-full px-3 py-2 border-3 border-gray-900 font-bold outline-none"
                            placeholder="https://mon-association.fr">
                    </div>
                    <div class="flex flex-wrap gap-4">
                        <div class="flex-1 min-w-[200px]">
                            <label for="settings_facebook" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Facebook</label>
                            <input type="url" id="settings_facebook" name="facebook" value="{{ app.user.facebook ?? '' }}"
                                class="w-full px-3 py-2 border-3 border-gray-900 font-bold outline-none"
                                placeholder="https://facebook.com/...">
                        </div>
                        <div class="flex-1 min-w-[200px]">
                            <label for="settings_instagram" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">Instagram</label>
                            <input type="url" id="settings_instagram" name="instagram" value="{{ app.user.instagram ?? '' }}"
                                class="w-full px-3 py-2 border-3 border-gray-900 font-bold outline-none"
                                placeholder="https://instagram.com/...">
                        </div>
                    </div>
                    <div class="flex flex-wrap gap-4">
                        <div class="flex-1 min-w-[200px]">
                            <label for="settings_twitter" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">X (Twitter)</label>
                            <input type="url" id="settings_twitter" name="twitter" value="{{ app.user.twitter ?? '' }}"
                                class="w-full px-3 py-2 border-3 border-gray-900 font-bold outline-none"
                                placeholder="https://x.com/...">
                        </div>
                        <div class="flex-1 min-w-[200px]">
                            <label for="settings_tiktok" class="text-[10px] tracking-widest block mb-1 font-black uppercase text-gray-400">TikTok</label>
                            <input type="url" id="settings_tiktok" name="tiktok" value="{{ app.user.tiktok ?? '' }}"
                                class="w-full px-3 py-2 border-3 border-gray-900 font-bold outline-none"
                                placeholder="https://tiktok.com/@...">
                        </div>
                    </div>
                </div>
            </div>

            <div class="border-4 border-gray-900 bg-gray-50 px-6 py-4">
                <p class="text-xs font-bold text-gray-500">Les informations de votre organisation (raison sociale, SIRET, adresse) ne peuvent etre modifiees que par l'equipe E-Ticket. Contactez <a href="mailto:contact@e-cosplay.fr" class="text-indigo-800 underline hover:text-indigo-950">contact@e-cosplay.fr</a> pour toute modification.</p>
            </div>
            {% endif %}

            <div>
                <button type="submit"
                    class="btn-brutal font-black uppercase text-sm tracking-widest hover:bg-green-500 hover:text-black transition-all">
                    Enregistrer
                </button>
            </div>
        </form>
    </div>
    {% endif %}
</div>
{% endif %}
{% endblock %}
