<?php
include __DIR__ . '/../includes/header.php';
ensure_can_manage_payroll();

$id = (int)($_GET['id'] ?? 0);

$stmt = db()->prepare("SELECT * FROM payroll_periods WHERE id = ?");
$stmt->execute([$id]);
$period = $stmt->fetch();

if (!$period) {
    set_flash('danger', 'Período no encontrado.');
    redirect('payroll/index.php');
}

$sql = "SELECT 
    pi.*, 
    e.full_name, 
    e.document_number
FROM payroll_items pi
JOIN employees e ON e.id = pi.employee_id
WHERE pi.period_id = ?
ORDER BY e.full_name";

$stmt = db()->prepare($sql);
$stmt->execute([$id]);
$rows = $stmt->fetchAll();
?>

<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">Nómina <?= e(month_name_es($period['month'])) ?>/<?= e($period['year']) ?></h4>
            <div class="text-muted small">
                Detalle por colaborador. Los adelantos pueden cargarse en cualquier momento y se descuentan al generar o cerrar.
            </div>
        </div>

        <div class="d-flex gap-2">
            <a class="btn btn-outline-secondary" href="<?= e(base_url('advances/index.php?year=' . $period['year'] . '&month=' . $period['month'])) ?>">
                Adelantos del período
            </a>

            <?php if ($period['status'] !== 'Cerrado'): ?>
                <a class="btn btn-dark" href="<?= e(base_url('payroll/close.php?id=' . $id)) ?>" onclick="return confirm('¿Cerrar nómina y recalcular adelantos?')">
                    Cerrar nómina
                </a>
            <?php endif; ?>

            <a class="btn btn-outline-primary" href="<?= e(base_url('payroll/export.php?id=' . $id)) ?>">
                Exportar CSV
            </a>
            <a class="btn btn-secondary" href="<?= e(base_url('payroll/index.php')) ?>">
                Volver
            </a>
        </div>
    </div>

    <div class="mb-3">
        <span class="badge text-bg-<?= e(status_badge_class($period['status'])) ?>">
            Estado: <?= e($period['status']) ?>
        </span>
    </div>

    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>Empleado</th>
                    <th>Salario base</th>
                    <th>Adelantos del mes</th>
                    <th>Extras</th>
                    <th>Descuentos</th>
                    <th>Neto</th>
                    <th>Recibo</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $row): ?>
                    <tr>
                        <td><?= e($row['full_name']) ?></td>
                        <td><?= e(number_format((float)$row['base_salary'], 0, ',', '.')) ?></td>
                        <td><?= e(number_format((float)$row['advances'], 0, ',', '.')) ?></td>
                        <td><?= e(number_format((float)$row['extras'], 0, ',', '.')) ?></td>
                        <td><?= e(number_format((float)$row['discounts'], 0, ',', '.')) ?></td>
                        <td><strong><?= e(number_format((float)$row['net_pay'], 0, ',', '.')) ?></strong></td>
                        <td>
                            <a class="btn btn-sm btn-outline-primary" href="<?= e(base_url('payroll/payslip.php?id=' . $row['id'])) ?>" target="_blank">
                                Imprimir
                            </a>
                        </td>
                    </tr>
                <?php endforeach; ?>

                <?php if (!$rows): ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted">No hay registros para este período.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>