<?php
$statusPill = static function (string $status): string {
    $map = ['draft' => 'inactive', 'calculated' => 'on_leave', 'approved' => 'active', 'paid' => 'active', 'locked' => 'terminated'];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$fmt = static fn ($v) => number_format((float) $v, 3);
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Payroll Runs',
    'workspaceTitle'      => 'Monthly Payroll Processing',
    'workspaceDesc'       => 'Create, calculate, approve, and pay monthly payroll runs.',
    'workspaceAction'     => [
        'href'  => base_url('payroll/runs/create'),
        'label' => 'New Payroll Run',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/runs') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Year</label>
                    <input type="number" name="year" class="form-control form-control-sm" value="<?= esc($filters['year']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Status</label>
                    <select name="status" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach (['draft', 'calculated', 'approved', 'paid'] as $s): ?>
                            <option value="<?= $s ?>" <?= $filters['status'] === $s ? 'selected' : '' ?>><?= ucfirst($s) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                </div>
            </div>
        </form>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Period</th>
                        <th>Title</th>
                        <th>Status</th>
                        <th>Run Date</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($runs as $run): ?>
                        <tr>
                            <td><?= esc(sprintf('%02d/%d', $run['period_month'], $run['period_year'])) ?></td>
                            <td><a href="<?= base_url('payroll/runs/' . $run['id']) ?>"><?= esc($run['title']) ?></a></td>
                            <td><?= $statusPill($run['status']) ?></td>
                            <td><?= esc($run['run_date'] ?? '—') ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('payroll/runs/' . $run['id']) ?>" class="btn btn-sm btn-outline-primary">Open</a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($runs) === 0): ?>
                        <tr><td colspan="5" class="text-center text-muted py-5">No payroll runs yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
