<!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>Ajouter un Utilisateur - 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 Content -->
    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        <!-- Header -->
        <header class="h-16 bg-white border-b border-gray-200 flex items-center justify-between px-6">
            <h2 class="text-lg font-semibold text-gray-800">Ajouter un Utilisateur</h2>
            <a href="/utilisateurs" class="text-sm font-medium text-gray-600 hover:text-gray-900">
                &larr; Retour à la liste
            </a>
        </header>

        <!-- Main section -->
        <div class="flex-1 overflow-auto p-6 bg-gray-50/50 flex justify-center items-start">
            
            <div class="w-full max-w-lg bg-white rounded-xl shadow-sm border border-gray-100 p-8 mt-4">
                <div th:if="${error}" class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg mb-6 text-sm">
                    <span th:text="${error}"></span>
                </div>

                <form th:action="@{/utilisateurs/ajouter}" method="post" class="space-y-6">
                    <div>
                        <label for="username" class="block text-sm font-medium text-gray-700 mb-1">Nom d'utilisateur</label>
                        <input type="text" id="username" name="username" th:value="${username}" class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" required placeholder="Ex: JeanDupont" autofocus>
                    </div>

                    <div>
                        <label for="roleId" class="block text-sm font-medium text-gray-700 mb-1">Rôle</label>
                        <select id="roleId" name="roleId" class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors bg-white" required>
                            <option value="">Sélectionnez un rôle</option>
                            <option th:each="r : ${roles}" th:value="${r.id}" 
                                    th:text="${r.name == 'ROLE_ADMIN' ? 'Administrateur' : (r.name == 'ROLE_TRESORERIE' ? 'Trésorerie' : (r.name == 'ROLE_AGENT_SAISIE' ? 'Agent de Saisie' : r.name))}">
                            </option>
                        </select>
                    </div>

                    <div>
                        <label for="password" class="block text-sm font-medium text-gray-700 mb-1">Mot de passe temporaire</label>
                        <input type="text" id="password" name="password" th:value="${defaultPassword}" class="w-full px-4 py-3 rounded-lg border border-gray-300 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors" required>
                        <p class="text-xs text-gray-400 mt-1">Le mot de passe par défaut est AsTalange123. L'utilisateur devra le modifier lors de sa première connexion.</p>
                    </div>

                    <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-4 rounded-lg transition-colors duration-200">
                        Créer l'utilisateur
                    </button>
                </form>
            </div>

        </div>
    </main>

</body>
</html>
