<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_login();
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="empleados.csv"');
$out = fopen('php://output', 'w');
fputcsv($out, ['ID','Nombre','Cédula','Departamento','Cargo','Ingreso','Salario','Estado']);
$stmt = db()->query("SELECT e.id, e.full_name, e.document_number, COALESCE(d.name,'') department_name, COALESCE(p.name,'') position_name, e.hire_date, e.salary, e.status FROM employees e LEFT JOIN departments d ON d.id = e.department_id LEFT JOIN positions p ON p.id = e.position_id ORDER BY e.id DESC");
foreach ($stmt as $row) {
    fputcsv($out, [$row['id'], $row['full_name'], $row['document_number'], $row['department_name'], $row['position_name'], $row['hire_date'], $row['salary'], $row['status']]);
}
fclose($out);
exit;
