{% extends "base.html" %}
{% block title %}Empleados{% endblock %}
{% block page_title %}Empleados{% endblock %}

{% block topbar_actions %}
<a href="{{ url_for('nominas.index') }}" class="btn btn-outline-secondary btn-sm">
  <i class="bi bi-arrow-left me-1"></i>Volver a nóminas
</a>
<a href="{{ url_for('nominas.empleado_nuevo') }}" class="btn btn-primary btn-sm">
  <i class="bi bi-person-plus me-1"></i>Nuevo empleado
</a>
{% endblock %}

{% block content %}

<!-- Stats -->
<div class="row g-3 mb-4">
  {% set activos = empleados|selectattr('activo','eq',1)|list %}
  {% set inactivos = empleados|selectattr('activo','eq',0)|list %}
  <div class="col-sm-4">
    <div class="stat-card" style="background:linear-gradient(135deg,#0f766e,#0d9488)">
      <div class="small opacity-75 mb-1">Activos</div>
      <div class="fs-3 fw-bold">{{ activos|length }}</div>
    </div>
  </div>
  <div class="col-sm-4">
    <div class="stat-card" style="background:linear-gradient(135deg,#7c3aed,#8b5cf6)">
      <div class="small opacity-75 mb-1">Masa salarial bruta/mes</div>
      <div class="fs-3 fw-bold">
        {{ '%.0f'|format(activos|sum(attribute='salario_bruto')) }} €
      </div>
    </div>
  </div>
  <div class="col-sm-4">
    <div class="stat-card" style="background:linear-gradient(135deg,#475569,#64748b)">
      <div class="small opacity-75 mb-1">Bajas</div>
      <div class="fs-3 fw-bold">{{ inactivos|length }}</div>
    </div>
  </div>
</div>

<div class="card border-0 shadow-sm">
  <div class="card-header border-0" style="background:linear-gradient(135deg,#ecfdf5,#d1fae5)">
    <i class="bi bi-people me-2 text-success"></i>
    <span class="fw-semibold" style="color:#065f46">Plantilla</span>
  </div>
  <div class="card-body p-0">
    {% if empleados %}
    <table class="table table-hover mb-0" style="font-size:.875rem">
      <thead>
        <tr>
          <th>Nombre</th>
          <th>NIF</th>
          <th>Cargo</th>
          <th class="text-end">S. Bruto/mes</th>
          <th class="text-center">IRPF</th>
          <th class="text-center">SS Trab.</th>
          <th>Alta</th>
          <th class="text-center">Estado</th>
          <th class="text-center no-print">Acc.</th>
        </tr>
      </thead>
      <tbody>
        {% for e in empleados %}
        <tr {% if not e.activo %}class="opacity-60"{% endif %}>
          <td>
            <div class="fw-semibold">{{ e.apellidos }}{% if e.apellidos and e.nombre %}, {% endif %}{{ e.nombre }}</div>
            {% if e.email %}
            <div class="text-muted" style="font-size:.73rem">{{ e.email }}</div>
            {% endif %}
          </td>
          <td class="text-muted">{{ e.nif or '—' }}</td>
          <td class="text-muted">{{ e.cargo or '—' }}</td>
          <td class="text-end fw-semibold">{{ '%.2f'|format(e.salario_bruto) }} €</td>
          <td class="text-center">
            <span class="badge bg-danger bg-opacity-10 text-danger">{{ e.irpf_pct }}%</span>
          </td>
          <td class="text-center">
            <span class="badge bg-warning bg-opacity-10 text-warning">{{ e.ss_trab_pct }}%</span>
          </td>
          <td class="text-muted small">{{ e.fecha_alta or '—' }}</td>
          <td class="text-center">
            {% if e.activo %}
            <span class="badge" style="background:#dcfce7;color:#14532d;border:1px solid #86efac">Activo</span>
            {% else %}
            <span class="badge" style="background:#f1f5f9;color:#64748b;border:1px solid #cbd5e1">Baja</span>
            {% endif %}
          </td>
          <td class="text-center no-print">
            <a href="{{ url_for('nominas.empleado_editar', eid=e.id) }}"
               class="btn btn-sm btn-outline-primary btn-action" title="Editar">
              <i class="bi bi-pencil"></i>
            </a>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
    {% else %}
    <div class="text-center py-5 text-muted">
      <i class="bi bi-people" style="font-size:2.5rem;opacity:.3"></i>
      <div class="mt-2">No hay empleados registrados.</div>
      <a href="{{ url_for('nominas.empleado_nuevo') }}" class="btn btn-primary mt-3">
        <i class="bi bi-person-plus me-1"></i>Añadir primer empleado
      </a>
    </div>
    {% endif %}
  </div>
</div>
{% endblock %}
