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

{% block title %}Attestations - Association E-Cosplay{% endblock %}

{% block admin_content %}
<div class="page-container">
    <div class="flex items-center justify-between mb-6">
        <h1 class="text-2xl font-bold heading-page">Attestations</h1>
        <button type="button" data-modal-open="modal-attestation" class="btn-gold px-4 py-2 font-bold uppercase text-[10px] tracking-wider text-gray-900">Creer une attestation</button>
    </div>

    {% for type, messages in app.flashes %}
        {% for message in messages %}
            <div class="mb-4 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 %}

    {% if attestations|length > 0 %}
        <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">Reference</th>
                        <th class="px-4 py-3 text-left font-bold uppercase text-xs tracking-widest">Titre</th>
                        <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Elements</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">Date</th>
                        <th class="px-4 py-3 text-center font-bold uppercase text-xs tracking-widest">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for att in attestations %}
                        <tr class="border-b border-white/20 hover:bg-white/50">
                            <td class="px-4 py-3 font-mono font-bold text-[10px]">{{ att.reference }}</td>
                            <td class="px-4 py-3 font-bold text-xs">{{ att.title|length > 50 ? att.title[:50] ~ '...' : att.title }}</td>
                            <td class="px-4 py-3 text-center text-xs">{{ att.items|length }}</td>
                            <td class="px-4 py-3 text-center">
                                {% if att.state == 'signed' %}
                                    <span class="px-2 py-0.5 bg-green-500/20 text-green-700 font-bold uppercase text-[10px]">Signee</span>
                                {% else %}
                                    <span class="px-2 py-0.5 bg-yellow-100 text-yellow-800 font-bold uppercase text-[10px]">Brouillon</span>
                                {% endif %}
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-500">{{ att.createdAt|date('d/m/Y H:i') }}</td>
                            <td class="px-4 py-3 text-center">
                                <a href="{{ path('app_admin_attestation_custom_show', {id: att.id}) }}" class="px-3 py-1 bg-gray-900 text-white hover:bg-[#fabf04] hover:text-gray-900 font-bold uppercase text-[10px] transition-all">Voir</a>
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    {% else %}
        <div class="glass p-8 text-center text-gray-400 font-bold">Aucune attestation.</div>
    {% endif %}

    {# Modal creation #}
    <div id="modal-attestation" class="hidden fixed inset-0 z-50 flex items-center justify-center bg-black/50">
        <div class="glass-heavy p-6 w-full max-w-lg max-h-[90vh] overflow-y-auto">
            <h2 class="text-lg font-bold uppercase mb-4">Nouvelle attestation</h2>
            <form method="post" action="{{ path('app_admin_attestation_custom_create') }}">
                <div class="mb-4">
                    <label for="att-title" class="block text-[9px] font-bold uppercase tracking-wider text-gray-400 mb-1">Titre de l'attestation *</label>
                    <input type="text" id="att-title" name="title" required class="input-glass w-full px-3 py-2 text-xs font-bold" placeholder="Ex: Attestation de conformite des services">
                </div>

                <div class="mb-4">
                    <label class="block text-[9px] font-bold uppercase tracking-wider text-gray-400 mb-2">Elements a attester *</label>
                    <div id="items-container" class="space-y-2">
                        <div class="flex gap-2">
                            <input type="text" name="items[]" required class="input-glass flex-1 px-3 py-2 text-xs font-bold" placeholder="Element 1">
                            <button type="button" class="remove-item px-2 py-1 bg-red-500/20 text-red-700 font-bold text-xs hidden">X</button>
                        </div>
                    </div>
                    <button type="button" id="add-item-btn" class="mt-2 px-3 py-1 glass text-xs font-bold uppercase tracking-wider hover:bg-gray-900 hover:text-white transition-all">+ Ajouter un element</button>
                </div>

                <div class="glass p-3 mb-4 text-[10px] text-gray-400 leading-relaxed">
                    <strong>Apercu :</strong> "Je soussigne(e), President(e) de l'Association E-Cosplay et le bureau de l'association, atteste les elements suivants : [vos elements]. La presente attestation est etablie pour faire valoir les droits de l'association. Les elements presentes sont conformes et veridiques."
                </div>

                <div class="flex justify-end gap-2">
                    <button type="button" data-modal-close="modal-attestation" class="px-4 py-2 glass font-bold uppercase text-[10px] tracking-widest">Annuler</button>
                    <button type="submit" class="btn-gold px-4 py-2 font-bold uppercase text-[10px] tracking-wider text-gray-900">Creer</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script nonce="{{ csp_nonce('script') }}">
(function() {
    var container = document.getElementById('items-container');
    var addBtn = document.getElementById('add-item-btn');
    var count = 1;

    function updateRemoveButtons() {
        var items = container.querySelectorAll('.flex');
        items.forEach(function(item) {
            var btn = item.querySelector('.remove-item');
            if (btn) btn.classList.toggle('hidden', items.length <= 1);
        });
    }

    addBtn.addEventListener('click', function() {
        count++;
        var div = document.createElement('div');
        div.className = 'flex gap-2';
        div.innerHTML = '<input type="text" name="items[]" required class="input-glass flex-1 px-3 py-2 text-xs font-bold" placeholder="Element ' + count + '">'
            + '<button type="button" class="remove-item px-2 py-1 bg-red-500/20 text-red-700 font-bold text-xs">X</button>';
        container.appendChild(div);
        div.querySelector('.remove-item').addEventListener('click', function() { div.remove(); updateRemoveButtons(); });
        updateRemoveButtons();
    });

    container.addEventListener('click', function(e) {
        if (e.target.classList.contains('remove-item')) { e.target.parentElement.remove(); updateRemoveButtons(); }
    });
})();
</script>
{% endblock %}
