{% extends "base.html" %}

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

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h1 class="h3 mb-0">Editar Usuario</h1>
                <a href="{{ url_for('admin_users') }}" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Volver a Usuarios
                </a>
            </div>

            <div class="card">
                <div class="card-body">
                    <form method="POST">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="username" class="form-label">Nombre de Usuario *</label>
                                    <input type="text" class="form-control" id="username" name="username"
                                           value="{{ user.username }}" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="email" class="form-label">Email *</label>
                                    <input type="email" class="form-control" id="email" name="email"
                                           value="{{ user.email }}" required>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="full_name" class="form-label">Nombre Completo *</label>
                                    <input type="text" class="form-control" id="full_name" name="full_name"
                                           value="{{ user.full_name }}" required>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="department" class="form-label">Departamento</label>
                                    <input type="text" class="form-control" id="department" name="department"
                                           value="{{ user.department or '' }}">
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="password" class="form-label">Nueva Contraseña</label>
                                    <input type="password" class="form-control" id="password" name="password"
                                           placeholder="Dejar vacío para mantener la actual">
                                    <div class="form-text">Solo completa si quieres cambiar la contraseña</div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">Permisos</label>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="is_admin" name="is_admin"
                                               {% if user.is_admin %}checked{% endif %}>
                                        <label class="form-check-label" for="is_admin">
                                            Administrador
                                        </label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" id="is_active" name="is_active"
                                               {% if user.is_active %}checked{% endif %}>
                                        <label class="form-check-label" for="is_active">
                                            Usuario Activo
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="d-flex justify-content-between">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> Actualizar Usuario
                            </button>
                            <div>
                                <a href="{{ url_for('admin_users') }}" class="btn btn-secondary me-2">Cancelar</a>
                                <button type="button" class="btn btn-danger" onclick="confirmDelete()">
                                    <i class="fas fa-trash"></i> Eliminar Usuario
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Información adicional -->
            <div class="row mt-4">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            <h5>Información del Usuario</h5>
                        </div>
                        <div class="card-body">
                            <p><strong>Creado:</strong> {{ user.created_at.strftime('%d/%m/%Y %H:%M') }}</p>
                            {% if user.last_login %}
                            <p><strong>Último acceso:</strong> {{ user.last_login.strftime('%d/%m/%Y %H:%M') }}</p>
                            {% else %}
                            <p><strong>Último acceso:</strong> Nunca</p>
                            {% endif %}
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-header">
                            <h5>Estadísticas</h5>
                        </div>
                        <div class="card-body">
                            <p><strong>Favoritos:</strong> {{ user.favorites.count() }}</p>
                            <p><strong>Accesos registrados:</strong> {{ user.access_logs.count() }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal de confirmación para eliminar -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Confirmar Eliminación</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>¿Estás seguro de que quieres eliminar el usuario <strong>{{ user.username }}</strong>?</p>
                <p class="text-danger">Esta acción no se puede deshacer.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
                <form method="POST" action="{{ url_for('admin_delete_user', user_id=user.id) }}" style="display: inline;">
                    <button type="submit" class="btn btn-danger">Eliminar</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function confirmDelete() {
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}
</script>
{% endblock %}
