{% extends "base.html" %}

{% block title %}Editar Usuario - ARCH{% endblock %}

{% block content %}
<div class="container my-5">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ url_for('users.list') }}">Usuarios</a></li>
            <li class="breadcrumb-item active" aria-current="page">Editar: {{ username }}</li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">Editar Usuario: {{ username }}</h5>
                </div>
                <div class="card-body">
                    <form method="POST" action="{{ url_for('users.edit', username=username) }}">
                        {{ form.csrf_token }}

                        <div class="mb-3">
                            <label for="nombre" class="form-label">Nombre completo</label>
                            {{ form.nombre(class="form-control", id="nombre") }}
                            {% if form.nombre.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.nombre.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="email" class="form-label">Email</label>
                            {{ form.email(class="form-control", id="email", type="email") }}
                            {% if form.email.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.email.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="password" class="form-label">Contraseña (Dejar en blanco para mantener)</label>
                            {{ form.password(class="form-control", id="password", type="password") }}
                            {% if form.password.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.password.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="password_confirm" class="form-label">Confirmar contraseña</label>
                            {{ form.password_confirm(class="form-control", id="password_confirm", type="password") }}
                            {% if form.password_confirm.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.password_confirm.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="nivel" class="form-label">Nivel de acceso</label>
                            {{ form.nivel(class="form-control", id="nivel", type="number") }}
                            {% if form.nivel.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.nivel.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                            <div class="form-text">
                                0-999: Usuario básico, 1000-4999: Editor, 5000-8999: Gestor, 9000-9999: Administrador
                            </div>
                        </div>

                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="idioma" class="form-label">Idioma</label>
                                {{ form.idioma(class="form-select", id="idioma") }}
                                {% if form.idioma.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.idioma.errors %}
                                            {{ error }}
                                        {% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                            <div class="col-md-6">
                                <label for="estado" class="form-label">Estado</label>
                                {{ form.estado(class="form-select", id="estado") }}
                                {% if form.estado.errors %}
                                    <div class="invalid-feedback d-block">
                                        {% for error in form.estado.errors %}
                                            {{ error }}
                                        {% endfor %}
                                    </div>
                                {% endif %}
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="empresa" class="form-label">Empresa</label>
                            {{ form.empresa(class="form-control", id="empresa") }}
                            {% if form.empresa.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.empresa.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="fecha_caducidad" class="form-label">Fecha de caducidad (YYYY-MM-DD)</label>
                            {{ form.fecha_caducidad(class="form-control", id="fecha_caducidad") }}
                            {% if form.fecha_caducidad.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.fecha_caducidad.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                            <div class="form-text">Dejar en blanco para no establecer fecha de caducidad</div>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{{ url_for('users.list') }}" class="btn btn-secondary">Cancelar</a>
                            <button type="submit" class="btn btn-primary">Actualizar Usuario</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
