<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <script src="https://unpkg.com/htmx.org@1.9.11"></script>
    <meta charset="UTF-8">
    <title>Catégorie - AS Talange</title>
    <script src="https://cdn.tailwindcss.com"></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>
</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" th:text="${categorie.id == null ? 'Nouvelle Catégorie' : 'Modifier Catégorie'}">Catégorie</h2>
        </header>

        <div class="flex-1 overflow-auto p-6">
            <div class="max-w-xl mx-auto bg-white rounded-xl shadow-sm border border-gray-100 p-8">
                <form th:action="@{/categories}" th:object="${categorie}" method="post" class="space-y-6">
                    <input type="hidden" th:field="*{id}" />

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Nom (ex: U15)</label>
                        <input type="text" th:field="*{nom}" required class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                    </div>

                    <div class="grid grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Âge Min</label>
                            <input type="number" th:field="*{anneeMin}" class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-gray-700 mb-1">Âge Max</label>
                            <input type="number" th:field="*{anneeMax}" class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                        </div>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                        <div>
                            <label for="tarifBase" class="block text-sm font-medium text-gray-700 mb-1">Tarif de base (Talangeois) (€) <span class="text-red-500">*</span></label>
                            <input type="number" step="0.01" id="tarifBase" th:field="*{tarifBase}" required
                                class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-colors">
                        </div>
                        <div>
                            <label for="tarifExterieur" class="block text-sm font-medium text-gray-700 mb-1">Tarif Extérieur (hors commune de Talange) (€) <span class="text-red-500">*</span></label>
                            <input type="number" step="0.01" id="tarifExterieur" th:field="*{tarifExterieur}" required
                                class="w-full border border-gray-300 rounded-lg px-4 py-2 text-sm focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-colors">
                        </div>
                    </div>

                    <div class="pt-4 border-t border-gray-100">
                        <label class="block text-sm font-medium text-gray-700 mb-2">Équipements liés à la catégorie</label>
                        <div class="space-y-3 max-h-64 overflow-y-auto">
                            <th:block th:each="equi : ${allEquipements}">
                                <div class="flex items-center space-x-4 bg-gray-50 p-2 rounded-lg border border-gray-100">
                                    <div class="w-1/3">
                                        <span class="block text-sm font-medium text-gray-900" th:text="${equi.nom}"></span>
                                    </div>
                                    <div class="w-1/3">
                                        <select th:name="'etatEquipement_' + ${equi.id}" class="w-full border border-gray-300 rounded-lg px-3 py-1.5 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                                            <option value="NON_LIE" th:selected="${!categorie.hasEquipement(equi.id)}">Non lié</option>
                                            <option value="OBLIGATOIRE" th:selected="${categorie.hasEquipement(equi.id) and categorie.isEquipementObligatoire(equi.id)}">Obligatoire (inclus)</option>
                                            <option value="OPTIONNEL" th:selected="${categorie.hasEquipement(equi.id) and !categorie.isEquipementObligatoire(equi.id)}">Optionnel (payant)</option>
                                        </select>
                                    </div>
                                    <div class="w-1/3 flex items-center space-x-2">
                                        <label class="text-xs text-gray-500">Prix (€)</label>
                                        <input type="number" step="0.01" th:name="'prixEquipement_' + ${equi.id}" 
                                               th:value="${categorie.getEquipementPrix(equi.id)}"
                                               class="w-full border border-gray-300 rounded-lg px-3 py-1.5 text-sm focus:ring-2 focus:ring-blue-500 outline-none">
                                    </div>
                                </div>
                            </th:block>
                        </div>
                    </div>

                    <div class="pt-4 flex justify-end space-x-3 border-t border-gray-100">
                        <a th:href="@{/categories}" class="px-4 py-2 text-sm font-medium text-gray-700 border border-gray-300 rounded-lg hover:bg-gray-50">Annuler</a>
                        <button type="submit" class="px-4 py-2 text-sm font-medium text-white bg-blue-600 rounded-lg hover:bg-blue-700">Enregistrer</button>
                    </div>
                </form>
            </div>
        </div>
    </main>
</body>
</html>
