<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_login();
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="permisos.csv"');
$out = fopen('php://output', 'w');
fputcsv($out, ['ID','Empleado','Tipo','Desde','Hasta','Pagado','Estado','Motivo']);
$stmt = db()->query("SELECT l.id, e.full_name, l.leave_type, l.start_date, l.end_date, l.paid_leave, l.status, l.reason FROM leaves_requests l JOIN employees e ON e.id = l.employee_id ORDER BY l.id DESC");
foreach ($stmt as $row) {
    fputcsv($out, [$row['id'], $row['full_name'], $row['leave_type'], $row['start_date'], $row['end_date'], $row['paid_leave'], $row['status'], $row['reason']]);
}
fclose($out);
exit;
