<?php include __DIR__ . '/../includes/header.php';
$status = trim($_GET['status'] ?? '');
$where = '';
$params = [];
if ($status !== '') { $where = ' WHERE l.status = ? '; $params[] = $status; }
$sql = "SELECT l.*, e.full_name, e.hire_date FROM leaves_requests l JOIN employees e ON e.id = l.employee_id {$where} ORDER BY l.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">Permisos, licencias y vacaciones</h4><div class="text-muted small">Incluye seguimiento de solicitudes, aprobación rápida y saldo acumulado de vacaciones.</div></div><div class="d-flex gap-2"><a class="btn btn-outline-primary" href="<?= e(base_url('reports/export_leaves.php')) ?>">Exportar CSV</a><a class="btn btn-primary" href="<?= e(base_url('leaves/create.php')) ?>">Nueva solicitud</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="Pendiente" <?= $status==='Pendiente' ? 'selected' : '' ?>>Pendiente</option><option value="Aprobado" <?= $status==='Aprobado' ? 'selected' : '' ?>>Aprobado</option><option value="Rechazado" <?= $status==='Rechazado' ? 'selected' : '' ?>>Rechazado</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>Empleado</th><th>Tipo</th><th>Desde</th><th>Hasta</th><th>Días</th><th>Estado</th><th>Saldo vacaciones</th><th width="240">Acciones</th></tr></thead><tbody><?php foreach ($stmt as $row): $vac = vacation_balance(['id'=>$row['employee_id'],'hire_date'=>$row['hire_date']]); $days = leave_days_between($row['start_date'], $row['end_date']); ?><tr><td><?= e($row['full_name']) ?></td><td><?= e($row['leave_type']) ?></td><td><?= e($row['start_date']) ?></td><td><?= e($row['end_date']) ?></td><td><?= e((int)$days) ?></td><td><span class="badge text-bg-<?= e(status_badge_class($row['status'])) ?>"><?= e($row['status']) ?></span></td><td><?= e($vac['pending']) ?> días</td><td><?php if (can_approve_leaves() && $row['status'] === 'Pendiente'): ?><a class="btn btn-sm btn-success" href="<?= e(base_url('leaves/approve.php?id=' . $row['id'])) ?>">Aprobar</a> <a class="btn btn-sm btn-outline-danger" href="<?= e(base_url('leaves/reject.php?id=' . $row['id'])) ?>">Rechazar</a><?php endif; ?> <?php if (can_manage_master_data()): ?><a class="btn btn-sm btn-warning" href="<?= e(base_url('leaves/edit.php?id=' . $row['id'])) ?>">Editar</a> <a class="btn btn-sm btn-danger" href="<?= e(base_url('leaves/delete.php?id=' . $row['id'])) ?>" onclick="return confirm('¿Eliminar solicitud?')">Eliminar</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div></div>
<?php include __DIR__ . '/../includes/footer.php'; ?>
