<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Pré-inscriptions - AS Talange</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/htmx.org@1.9.11"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style> body { font-family: 'Inter', sans-serif; } </style>
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body class="bg-gray-50 text-gray-900 flex h-screen overflow-hidden">
    <!-- Sidebar -->
    <div th:replace="~{fragments/sidebar :: sidebar}"></div>

    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        <header class="h-16 bg-white border-b border-gray-200 flex items-center px-6">
            <h2 class="text-lg font-semibold text-gray-800">Dossiers en attente</h2>
        </header>

        <div class="flex-1 overflow-auto p-6">
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse min-w-[800px]">
                    <thead>
                        <tr class="bg-gray-50 text-gray-500 text-sm uppercase tracking-wider border-b border-gray-200">
                            <th class="py-3 px-6 font-medium">Date</th>
                            <th class="py-3 px-6 font-medium">Adhérent</th>
                            <th class="py-3 px-6 font-medium">Contact</th>
                            <th class="py-3 px-6 font-medium text-right">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 text-sm">
                        <tr th:if="${#lists.isEmpty(preInscriptions)}">
                            <td colspan="4" class="py-8 text-center text-gray-500">Aucune pré-inscription en attente.</td>
                        </tr>
                        <tr th:each="pre : ${preInscriptions}" th:id="'pre-' + ${pre.id}" class="hover:bg-gray-50 transition-colors">
                            <td class="py-4 px-6 text-gray-600" th:text="${#temporals.format(pre.dateCreation, 'dd/MM/yyyy HH:mm')}"></td>
                            <td class="py-4 px-6">
                                <div class="font-medium text-gray-900" th:text="${pre.nom + ' ' + pre.prenom}"></div>
                                <div class="text-xs text-gray-500" th:text="'Né(e) le ' + ${#temporals.format(pre.dateNaissance, 'dd/MM/yyyy')}"></div>
                                <div th:if="${pre.representantLegal != null}" class="text-[10px] text-blue-600 font-semibold mt-1" th:text="'Parent : ' + ${pre.representantLegal}"></div>
                            </td>
                            <td class="py-4 px-6">
                                <div class="text-xs text-gray-500" th:text="${pre.email}"></div>
                            </td>
                            <td class="py-4 px-6 text-right space-x-2">
                                <button th:attr="hx-post=@{/admin/pre-inscriptions/{id}/valider(id=${pre.id})}"
                                        class="bg-blue-600 text-white px-3 py-1.5 rounded hover:bg-blue-700 transition-colors">
                                    Créer la fiche
                                </button>
                                <button th:attr="hx-post=@{/admin/pre-inscriptions/{id}/rejeter(id=${pre.id})}, hx-target=|#pre-${pre.id}|"
                                        hx-swap="outerHTML"
                                        hx-confirm="Confirmer le rejet du dossier ?"
                                        class="bg-red-50 text-red-600 border border-red-200 px-3 py-1.5 rounded hover:bg-red-100 transition-colors">
                                    Rejeter
                                </button>
                            </td>
                        </tr>
                    </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>

    <script>
        document.body.addEventListener('htmx:configRequest', function(evt) {
            evt.detail.headers[document.querySelector('meta[name="_csrf_header"]').content] = 
                document.querySelector('meta[name="_csrf"]').content;
        });
    </script>
</body>
</html>
