{% extends "base.html" %}
{% block title %}{{ title }} — GMAO Collège{% endblock %}

{% block content %}
<h1 class="mb-4">{{ title }}</h1>

<div class="card">
    <div class="card-body">
        <form method="POST">
            {{ form.hidden_tag() }}
            <div class="row">
                <div class="col-md-6 mb-3">
                    {{ form.username.label(class="form-label") }}
                    {{ form.username(class="form-control") }}
                    {% for error in form.username.errors %}<div class="text-danger">{{ error }}</div>{% endfor %}
                </div>
                <div class="col-md-6 mb-3">
                    {{ form.email.label(class="form-label") }}
                    {{ form.email(class="form-control") }}
                    {% for error in form.email.errors %}<div class="text-danger">{{ error }}</div>{% endfor %}
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    {{ form.full_name.label(class="form-label") }}
                    {{ form.full_name(class="form-control") }}
                    {% for error in form.full_name.errors %}<div class="text-danger">{{ error }}</div>{% endfor %}
                </div>
                <div class="col-md-6 mb-3">
                    {{ form.password.label(class="form-label") }}
                    {{ form.password(class="form-control", placeholder="Laisser vide si pas de changement") }}
                    {% for error in form.password.errors %}<div class="text-danger">{{ error }}</div>{% endfor %}
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="form-label">Rôles</label>
                    <input type="hidden" name="role" value="{{ form.role.data or 'demandeur' }}">
                    <div class="row g-1">
                    {% for role in roles_list|default([]) %}
                      <div class="col-6"><label class="border rounded p-1 d-block small">
                        <input type="checkbox" name="role_ids" value="{{ role.slug }}" {% if form.role.data == role.slug %}checked{% endif %}> {{ role.name }}
                      </label></div>
                    {% endfor %}
                    </div>
                    <small class="text-muted">Plusieurs rôles peuvent être sélectionnés.</small>
                </div>
                <div class="col-md-6 mb-3 d-flex align-items-end">
                    <div class="form-check">
                        {{ form.is_active(class="form-check-input") }}
                        {{ form.is_active.label(class="form-check-label") }}
                    </div>
                </div>
            </div>
            <div class="mt-3">
                <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> Enregistrer</button>
                <a href="{{ url_for('auth.list_users') }}" class="btn btn-outline-secondary">Annuler</a>
                {% if user %}
                <a href="{{ url_for('auth.change_user_password', id=user.id) }}" class="btn btn-outline-warning ms-2">
                    <i class="bi bi-key"></i> Changer le mot de passe
                </a>
                {% endif %}
                {% if user and user.id != current_user.id %}
                <form method="POST" action="{{ url_for('auth.delete_user', id=user.id) }}" class="d-inline ms-2" onsubmit="return confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur ?')">
                    <button type="submit" class="btn btn-danger">
                        <i class="bi bi-trash"></i> Supprimer
                    </button>
                </form>
                {% endif %}
            </div>
        </form>
    </div>
</div>
{% endblock %}
