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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Final Settlements',
    'workspaceTitle'      => 'Employee Final Settlements',
    'workspaceDesc'       => 'Termination settlements including leave encashment and legacy EOSB (pre-March 2024 service).',
    'workspaceAction'     => [
        'href'  => base_url('payroll/settlements/create'),
        'label' => 'New Settlement',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/settlements') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div><label class="enterprise-field-label">Search</label><input type="text" name="q" class="form-control form-control-sm" value="<?= esc($filters['search']) ?>"></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', '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>Employee</th><th>Termination</th><th>Years</th><th>Total (BHD)</th><th>Status</th><th class="text-end">Actions</th></tr></thead>
                <tbody>
                    <?php foreach ($settlements as $s): ?>
                        <tr>
                            <td><?= esc($s['full_name']) ?> <span class="text-muted small">(<?= esc($s['employee_number']) ?>)</span></td>
                            <td><?= esc($s['termination_date']) ?></td>
                            <td><?= number_format((float) $s['years_of_service'], 1) ?></td>
                            <td class="fw-semibold"><?= $fmt($s['total_settlement']) ?></td>
                            <td><?= $statusPill($s['status']) ?></td>
                            <td class="text-end"><a href="<?= base_url('payroll/settlements/' . $s['id']) ?>" class="btn btn-sm btn-outline-primary">View</a></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($settlements) === 0): ?>
                        <tr><td colspan="6" class="text-center text-muted py-5">No settlements recorded.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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