<?php include __DIR__ . '/../includes/header.php';
$status = trim($_GET['status'] ?? '');
$where = '';
$params = [];
if ($status !== '') { $where = ' WHERE e.status = ? '; $params[] = $status; }
$sql = "SELECT e.*, d.name department_name, p.name position_name FROM employees e LEFT JOIN departments d ON d.id = e.department_id LEFT JOIN positions p ON p.id = e.position_id {$where} ORDER BY e.id DESC";
$stmt = db()->prepare($sql);
$stmt->execute($params);
?>
<div class="table-wrap"><div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2"><div><h4 class="mb-0">Empleados</h4><div class="text-muted small">Legajo, datos de contacto y saldo estimado de vacaciones.</div></div><div class="d-flex gap-2"><?= can_manage_master_data() ? '<a class="btn btn-outline-primary" href="' . e(base_url('reports/export_employees.php')) . '">Exportar CSV</a><a class="btn btn-primary" href="' . e(base_url('employees/create.php')) . '">Nuevo empleado</a>' : '' ?></div></div><form class="row g-2 mb-3" method="get"><div class="col-md-3"><select name="status" class="form-select"><option value="">Todos los estados</option><option value="active" <?= $status==='active' ? 'selected' : '' ?>>Activo</option><option value="inactive" <?= $status==='inactive' ? 'selected' : '' ?>>Inactivo</option></select></div><div class="col-auto"><button class="btn btn-outline-secondary">Filtrar</button></div></form><div class="table-responsive"><table class="table table-bordered table-hover align-middle"><thead><tr><th>Nombre</th><th>Cédula</th><th>Departamento</th><th>Cargo</th><th>Ingreso</th><th>Salario</th><th>Vacaciones</th><th>Estado</th><th width="160">Acciones</th></tr></thead><tbody><?php foreach ($stmt as $row): $vac = vacation_balance($row); ?><tr><td><?= e($row['full_name']) ?></td><td><?= e($row['document_number']) ?></td><td><?= e($row['department_name']) ?></td><td><?= e($row['position_name']) ?></td><td><?= e($row['hire_date']) ?></td><td><?= money_gs($row['salary']) ?></td><td><?= e($vac['pending']) ?> días</td><td><span class="badge text-bg-<?= e(status_badge_class($row['status'])) ?>"><?= e($row['status']) ?></span></td><td><?php if (can_manage_master_data()): ?><a class="btn btn-sm btn-warning" href="<?= e(base_url('employees/edit.php?id=' . $row['id'])) ?>">Editar</a> <a class="btn btn-sm btn-danger" href="<?= e(base_url('employees/delete.php?id=' . $row['id'])) ?>" onclick="return confirm('¿Eliminar empleado?')">Eliminar</a><?php else: ?><span class="text-muted small">Solo lectura</span><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div></div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
