{#-
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-people me-2"></i>{{ _('Empleados') }}</h2>
        <a href="{{ url_for('employee.new') }}" class="btn btn-primary">
            <i class="bi bi-plus-circle me-1"></i>{{ _('Nuevo Empleado') }}
        </a>
    </div>

    <!-- Filter Form -->
    <div class="card mb-3">
        <div class="card-header">
            <h5 class="mb-0">
                <i class="bi bi-funnel me-1"></i>{{ _('Filtros') }}
            </h5>
        </div>
        <div class="card-body">
            <form method="GET" action="{{ url_for('employee.index') }}">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label for="buscar" class="form-label">{{ _('Buscar') }}</label>
                        <input type="text" class="form-control" id="buscar" name="buscar" 
                               value="{{ buscar or '' }}" 
                               placeholder="{{ _('Nombre, código o identificación...') }}">
                    </div>
                    <div class="col-md-3">
                        <label for="estado" class="form-label">{{ _('Estado') }}</label>
                        <select class="form-select" id="estado" name="estado">
                            <option value="">{{ _('Todos') }}</option>
                            <option value="activo" {% if estado == 'activo' %}selected{% endif %}>{{ _('Activo') }}</option>
                            <option value="inactivo" {% if estado == 'inactivo' %}selected{% endif %}>{{ _('Inactivo') }}</option>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label for="area" class="form-label">{{ _('Área') }}</label>
                        <input type="text" class="form-control" id="area" name="area" 
                               value="{{ area or '' }}" 
                               placeholder="{{ _('Filtrar por área...') }}">
                    </div>
                    <div class="col-md-2">
                        <label for="cargo" class="form-label">{{ _('Cargo') }}</label>
                        <input type="text" class="form-control" id="cargo" name="cargo" 
                               value="{{ cargo or '' }}" 
                               placeholder="{{ _('Filtrar por cargo...') }}">
                    </div>
                </div>
                <div class="row mt-3">
                    <div class="col-12">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-search me-1"></i>{{ _('Buscar') }}
                        </button>
                        <a href="{{ url_for('employee.index') }}" class="btn btn-secondary">
                            <i class="bi bi-x-circle me-1"></i>{{ _('Limpiar') }}
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>{{ _('Código') }}</th>
                            <th>{{ _('Identificación') }}</th>
                            <th>{{ _('Nombre Completo') }}</th>
                            <th>{{ _('Cargo') }}</th>
                            <th>{{ _('Área') }}</th>
                            <th>{{ _('Salario Base') }}</th>
                            <th>{{ _('Estado') }}</th>
                            <th>{{ _('Acciones') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for employee in employees %}
                        <tr>
                            <td><a href="{{ url_for('employee.edit', id_=employee.id) }}" class="text-decoration-none">{{ employee.codigo_empleado }}</a></td>
                            <td><a href="{{ url_for('employee.edit', id_=employee.id) }}" class="text-decoration-none">{{ employee.identificacion_personal }}</a></td>
                            <td><a href="{{ url_for('employee.edit', id_=employee.id) }}" class="text-decoration-none">{{ employee.primer_nombre }} {{ employee.segundo_nombre or '' }} {{ employee.primer_apellido }} {{ employee.segundo_apellido or '' }}</a></td>
                            <td>{{ employee.cargo or '-' }}</td>
                            <td>{{ employee.area or '-' }}</td>
                            <td>
                                {% if employee.moneda %}
                                    {{ employee.moneda.simbolo or employee.moneda.codigo }} {{ employee.salario_base }}
                                {% else %}
                                    {{ employee.salario_base }}
                                {% endif %}
                            </td>
                            <td>
                                {% if employee.activo %}
                                    <span class="badge bg-success">{{ _('Activo') }}</span>
                                {% else %}
                                    <span class="badge bg-warning">{{ _('Inactivo') }}</span>
                                {% endif %}
                            </td>
                            <td>
                                <a href="{{ url_for('employee.edit', id_=employee.id) }}" class="btn btn-sm btn-outline-primary" title="{{ _('Editar') }}">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <form action="{{ url_for('employee.delete', id_=employee.id) }}" method="POST" class="d-inline" onsubmit="return confirm('{{ _('¿Está seguro de eliminar este empleado?') }}');">
                                    <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="8" class="text-center text-muted">{{ _('No hay empleados registrados.') }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {{ render_pagination(pagination, 'employee.index') }}
        </div>
    </div>
</div>
{% endblock %}
