{% extends 'admin/base.html' %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        {% if user %}
            <h1><i class="fas fa-edit me-2"></i> Modifier un utilisateur</h1>
        {% else %}
            <h1><i class="fas fa-user-plus me-2"></i> Ajouter un utilisateur</h1>
        {% endif %}
        <a href="{{ url_for('admin.users') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-2"></i> Retour à la liste
        </a>
    </div>
    
    <div class="card border-0 shadow-sm">
        <div class="card-body p-4">
            <form method="post" class="needs-validation" novalidate>
                {{ form.hidden_tag() }}
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <div class="form-group">
                            {{ form.username.label(class="form-label") }}
                            {{ form.username(class="form-control" + (" is-invalid" if form.username.errors else "")) }}
                            {% if form.username.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.username.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <div class="form-group">
                            {{ form.email.label(class="form-label") }}
                            {{ form.email(class="form-control" + (" is-invalid" if form.email.errors else "")) }}
                            {% if form.email.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.email.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <div class="form-group">
                            {{ form.password.label(class="form-label") }}
                            {{ form.password(class="form-control" + (" is-invalid" if form.password.errors else "")) }}
                            {% if form.password.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.password.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                            {% if user %}
                                <div class="form-text">Laissez vide pour conserver le mot de passe actuel</div>
                            {% endif %}
                        </div>
                    </div>
                    
                    <div class="col-md-6 mb-3">
                        <div class="form-group">
                            {{ form.confirm_password.label(class="form-label") }}
                            {{ form.confirm_password(class="form-control" + (" is-invalid" if form.confirm_password.errors else "")) }}
                            {% if form.confirm_password.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.confirm_password.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                
                <div class="form-group mb-3">
                    {{ form.role.label(class="form-label") }}
                    {{ form.role(class="form-select" + (" is-invalid" if form.role.errors else "")) }}
                    {% if form.role.errors %}
                        <div class="invalid-feedback">
                            {% for error in form.role.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
                
                {% if user and user.id == current_user.id and user.role == 'admin' and form.role.data == 'user' %}
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle me-2"></i> Attention : Si vous changez votre rôle d'administrateur à utilisateur, vous perdrez l'accès au panneau d'administration.
                    </div>
                {% endif %}
                
                <div class="d-flex justify-content-between mt-4">
                    <a href="{{ url_for('admin.users') }}" class="btn btn-secondary">Annuler</a>
                    {{ form.submit(class="btn btn-primary") }}
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
