{#-
SPDX-License-Identifier: Apache-2.0
SPDX-FileCopyrightText: 2025 - 2026 BMO Soluciones, S.A.
-#}

{% extends 'base.html' %}
{% from 'macros.html' import render_pagination %}

{% block content %}
<div class="container">
    
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="bi bi-person-gear me-2"></i>{{ _('Usuarios') }}</h2>
        <a href="{{ url_for('user.new') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i>{{ _('Nuevo Usuario') }}
        </a>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{{ _('Usuario') }}</th>
                            <th>{{ _('Nombre') }}</th>
                            <th>{{ _('Correo') }}</th>
                            <th>{{ _('Tipo') }}</th>
                            <th>{{ _('Estado') }}</th>
                            <th>{{ _('Acciones') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for user in users %}
                        <tr>
                            <td>{{ user.usuario }}</td>
                            <td>{{ user.nombre }} {{ user.apellido }}</td>
                            <td>{{ user.correo_electronico or '-' }}</td>
                            <td>
                                {% if user.tipo == 'admin' %}
                                    <span class="badge bg-danger">{{ _('Administrador') }}</span>
                                {% elif user.tipo == 'hhrr' %}
                                    <span class="badge bg-primary">{{ _('Recursos Humanos') }}</span>
                                {% elif user.tipo == 'audit' %}
                                    <span class="badge bg-secondary">{{ _('Auditoría') }}</span>
                                {% else %}
                                    <span class="badge bg-light text-dark">{{ user.tipo }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if user.activo %}
                                    <span class="badge bg-success">{{ _('Activo') }}</span>
                                {% else %}
                                    <span class="badge bg-warning">{{ _('Inactivo') }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ url_for('user.edit', id_=user.id) }}" class="btn btn-sm btn-outline-primary" title="{{ _('Editar') }}">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form action="{{ url_for('user.delete', id_=user.id) }}" method="POST" class="d-inline" onsubmit="return confirm('{{ _('¿Está seguro de eliminar este usuario?') }}');">
                                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="{{ _('Eliminar') }}">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {% else %}
                        <tr>
                            <td colspan="6" class="text-center text-muted">{{ _('No hay usuarios registrados.') }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {{ render_pagination(pagination, 'user.index') }}
        </div>
    </div>
</div>
{% endblock %}
