<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <script src="https://unpkg.com/htmx.org@1.9.11"></script>
    <meta charset="UTF-8">
    <title>Gestion des Utilisateurs - 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">Gestion des Utilisateurs</h2>
            <a href="/utilisateurs/ajouter" class="bg-blue-600 hover:bg-blue-700 text-white font-medium py-2 px-4 rounded-lg transition-colors text-sm">
                Ajouter un utilisateur
            </a>
        </header>

        <!-- Main section -->
        <div class="flex-1 overflow-auto p-6 bg-gray-50/50">

            <!-- Success / Error messages -->
            <div th:if="${success}" class="bg-green-50 border border-green-200 text-green-700 px-4 py-3 rounded-lg mb-6 text-sm">
                <span th:text="${success}"></span>
            </div>
            <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>

            <!-- Users Table -->
            <div class="bg-white rounded-xl shadow-sm border border-gray-100 overflow-hidden">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-gray-50/50 text-gray-500 text-xs uppercase tracking-wider border-b border-gray-100">
                            <th class="py-3 px-6 font-medium">Nom d'utilisateur</th>
                            <th class="py-3 px-6 font-medium">Rôle</th>
                            <th class="py-3 px-6 font-medium">Statut</th>
                            <th class="py-3 px-6 font-medium text-right">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 text-sm">
                        <tr th:each="user : ${users}" class="hover:bg-gray-50 transition-colors">
                            <td class="py-4 px-6 font-medium text-gray-900" th:text="${user.username}">
                                Ucef
                            </td>
                            <td class="py-4 px-6 text-gray-600">
                                <span th:each="role : ${user.roles}" 
                                      th:text="${role.name == 'ROLE_ADMIN' ? 'Administrateur' : (role.name == 'ROLE_TRESORERIE' ? 'Trésorerie' : (role.name == 'ROLE_AGENT_SAISIE' ? 'Agent de Saisie' : role.name))}"
                                      class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800 mr-1">
                                    Admin
                                </span>
                            </td>
                            <td class="py-4 px-6">
                                <span th:if="${user.mustChangePassword}" class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800">
                                    Mot de passe temporaire
                                </span>
                                <span th:unless="${user.mustChangePassword}" class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                                    Actif
                                </span>
                            </td>
                            <td class="py-4 px-6 text-right">
                                <!-- Don't show delete button for the current user -->
                                <form th:if="${#authentication.name != user.username}" 
                                      th:action="@{/utilisateurs/{id}/delete(id=${user.id})}" 
                                      method="post" 
                                      class="inline"
                                      onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur ?');">
                                    <button type="submit" class="text-red-600 hover:text-red-800 font-medium">
                                        Supprimer
                                    </button>
                                </form>
                                <span th:if="${#authentication.name == user.username}" class="text-gray-400 italic text-xs">
                                    Connecté
                                </span>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>

        </div>
    </main>

</body>
</html>
