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

{% block title %}Logs - 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">Logs d'activite</h1>
        {% if is_granted('ROLE_ROOT') %}
            <form method="post" action="{{ path('app_admin_logs_purge') }}" data-confirm="Supprimer tous les logs ? Cette action est irreversible.">
                <button type="submit" class="btn-glass px-4 py-2 text-red-600 text-xs font-bold uppercase tracking-wider" style="border-color: rgba(220,38,38,0.3);">Supprimer tous les logs</button>
            </form>
        {% endif %}
    </div>

    <div class="glass overflow-hidden">
        <table class="w-full text-xs">
            <thead>
                <tr class="glass-dark" >
                    <th class="px-3 py-2 text-left font-bold uppercase tracking-wider text-white/80">Date</th>
                    <th class="px-3 py-2 text-left font-bold uppercase tracking-wider text-white/80">Utilisateur</th>
                    <th class="px-3 py-2 text-center font-bold uppercase tracking-wider text-white/80">Methode</th>
                    <th class="px-3 py-2 text-left font-bold uppercase tracking-wider text-white/80">Action</th>
                    <th class="px-3 py-2 text-left font-bold uppercase tracking-wider text-white/80">URL</th>
                    <th class="px-3 py-2 text-left font-bold uppercase tracking-wider text-white/80">IP</th>
                    <th class="px-3 py-2 text-center font-bold uppercase tracking-wider text-white/80">HMAC</th>
                    <th class="px-3 py-2 text-center font-bold uppercase tracking-wider text-white/80">PDF</th>
                </tr>
            </thead>
            <tbody>
                {% for log in pagination %}
                    <tr class="border-b border-white/10 hover:bg-white/30 transition-colors">
                        <td class="px-3 py-2 text-gray-500 whitespace-nowrap">{{ log.createdAt|date('d/m/Y H:i:s') }}</td>
                        <td class="px-3 py-2 font-bold">
                            {% if log.user %}
                                {{ log.user.fullName }}
                            {% else %}
                                <span class="text-gray-400">—</span>
                            {% endif %}
                        </td>
                        <td class="px-3 py-2 text-center">
                            <span class="px-1.5 py-0.5text-[9px] font-bold
                                {% if log.method == 'POST' %}bg-orange-500/20 text-orange-700
                                {% elseif log.method == 'DELETE' %}bg-red-500/20 text-red-700
                                {% elseif log.method == 'PUT' or log.method == 'PATCH' %}bg-blue-500/20 text-blue-700
                                {% else %}bg-gray-500/20 text-gray-600{% endif %}">
                                {{ log.method }}
                            </span>
                        </td>
                        <td class="px-3 py-2">{{ log.action }}</td>
                        <td class="px-3 py-2 text-gray-500 font-mono text-[10px] max-w-[200px] truncate" title="{{ log.url }}">{{ log.url }}</td>
                        <td class="px-3 py-2 text-gray-400 font-mono text-[10px]">{{ log.ip }}</td>
                        <td class="px-3 py-2 text-center">
                            {% if hmacResults[log.id] %}
                                <span class="inline-block w-5 h-5 bg-green-500/20 text-green-600 font-bold leading-5 text-[10px]" title="Integrite verifiee">&#10003;</span>
                            {% else %}
                                <span class="inline-block w-5 h-5 bg-red-500/20 text-red-600 font-bold leading-5 text-[10px]" title="Donnees alterees !">&#10007;</span>
                            {% endif %}
                        </td>
                        <td class="px-3 py-2 text-center">
                            <div class="flex items-center justify-center gap-1">
                                <a href="{{ path('app_admin_logs_pdf', {id: log.id}) }}" class="btn-glass px-2 py-1 text-[9px] font-bold uppercase tracking-wider text-gray-600" target="_blank">PDF</a>
                                <form method="post" action="{{ path('app_admin_logs_delete', {id: log.id}) }}" data-confirm="Supprimer le log #{{ log.id }} ?">
                                    <button type="submit" class="btn-glass px-2 py-1 text-[9px] font-bold uppercase tracking-wider text-red-600" style="border-color: rgba(220,38,38,0.2);">&#10007;</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                {% else %}
                    <tr>
                        <td colspan="9" class="px-4 py-8 text-center text-gray-400 font-bold">Aucun log enregistre.</td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>

    {% if pagination.totalItemCount > 20 %}
        <div class="mt-6 flex justify-center">
            {{ knp_pagination_render(pagination) }}
        </div>
    {% endif %}
</div>
{% endblock %}
