{% extends "base.html" %}

{% block title %}Gestion utilisateurs - GMAO Collège{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row mb-4">
        <div class="col">
            <h1 class="h3"><i class="bi bi-people"></i> Gestion des utilisateurs</h1>
            <p class="text-muted">Gérer les utilisateurs et leurs rôles</p>
        </div>
        <div class="col-auto">
            <a href="{{ url_for('auth.create_user') }}" class="btn btn-primary">
                <i class="bi bi-person-plus"></i> Nouvel utilisateur
            </a>
            <a href="{{ url_for('admin.permissions') }}" class="btn btn-outline-primary">
                <i class="bi bi-shield-check"></i> Permissions
            </a>
        </div>
    </div>

    <!-- Tableau des utilisateurs -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">Liste des utilisateurs</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>Nom complet</th>
                            <th>Nom d'utilisateur</th>
                            <th>Email</th>
                            <th>Rôle</th>
                            <th>Statut</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>
                                <div class="d-flex align-items-center">
                                    {% if user.is_active %}
                                    <i class="bi bi-person-circle text-success me-2"></i>
                                    {% else %}
                                    <i class="bi bi-person-circle text-secondary me-2"></i>
                                    {% endif %}
                                    {{ user.full_name }}
                                </div>
                            </td>
                            <td>{{ user.username }}</td>
                            <td>{{ user.email or 'Non défini' }}</td>
                            <td>
                                <span class="badge 
                                    {% if user.is_admin() %}bg-danger
                                    {% elif user.has_permission('patrimoine.manage') %}bg-success
                                    {% else %}bg-secondary{% endif %}">
                                    {{ user.role_label }}
                                </span>
                            </td>
                            <td>
                                {% if user.is_active %}
                                <span class="badge bg-success">Actif</span>
                                {% else %}
                                <span class="badge bg-secondary">Inactif</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="btn-group" role="group">
                                    <a href="{{ url_for('admin.user_access', user_id=user.id) }}"
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-shield-check"></i>
                                    </a>
                                    <a href="{{ url_for('auth.edit_user', id=user.id) }}" 
                                       class="btn btn-sm btn-outline-warning">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    {% if user.id != current_user.id %}
                                    <form method="post" action="{{ url_for('auth.delete_user', id=user.id) }}" 
                                          class="d-inline" onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur ?')">
                                        <button type="submit" class="btn btn-sm btn-outline-danger">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                    {% endif %}
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Résumé des rôles -->
    <div class="row mt-4">
        <div class="col-md-3">
            <div class="text-center">
                <h4 class="text-danger">{{ users|selectattr('role', 'equalto', 'admin')|list|length }}</h4>
                <p class="text-muted mb-0">Administrateurs</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="text-center">
                <h4 class="text-warning">{{ users|selectattr('role', 'equalto', 'chef')|list|length }}</h4>
                <p class="text-muted mb-0">Chefs d'établissement</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="text-center">
                <h4 class="text-success">{{ users|selectattr('role', 'equalto', 'technicien')|list|length }}</h4>
                <p class="text-muted mb-0">Techniciens</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="text-center">
                <h4 class="text-info">{{ users|selectattr('role', 'equalto', 'demandeur')|list|length }}</h4>
                <p class="text-muted mb-0">Demandeurs</p>
            </div>
        </div>
    </div>
</div>
{% endblock %}
