<?php $fmt = static fn ($v) => number_format((float) $v, 3); ?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Payslips',
    'workspaceTitle'      => 'Employee Payslips',
    'workspaceDesc'       => 'Browse payslips by payroll period.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/payslips') ?>" 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">Month</label>
                    <select name="month" class="form-select form-select-sm">
                        <?php for ($m = 1; $m <= 12; $m++): ?>
                            <option value="<?= $m ?>" <?= (int) $filters['month'] === $m ? 'selected' : '' ?>><?= date('F', mktime(0, 0, 0, $m, 1)) ?></option>
                        <?php endfor; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Load</button>
                </div>
            </div>
        </form>
    </div>
    <div class="enterprise-data-panel__body">
        <?php if (! $run): ?>
            <p class="text-muted text-center py-5">No payroll run found for this period.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Employee</th>
                            <th>Net Pay (BHD)</th>
                            <th>Status</th>
                            <th class="text-end">View</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($items as $item): ?>
                            <tr>
                                <td><?= esc($item['full_name']) ?> <span class="text-muted small">(<?= esc($item['employee_number']) ?>)</span></td>
                                <td class="fw-semibold"><?= $fmt($item['net_pay']) ?></td>
                                <td><?= esc(ucfirst($item['status'])) ?></td>
                                <td class="text-end">
                                    <a href="<?= base_url('payroll/payslips/' . $item['id']) ?>" class="btn btn-sm btn-outline-primary">Payslip</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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