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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'EOSB Report',
    'workspaceTitle'      => 'End-of-Service Benefit (EOSB) Report',
    'workspaceDesc'       => 'Monthly employer EOSB contributions for expatriate employees (SIO fund, post March 2024).',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/reports/eosb') ?>" 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>
                    <?= view('Modules\Reports\Views\partials\export_buttons', ['actionPath' => 'payroll/reports/eosb', 'query' => $filters]) ?>
                </div>
            </div>
        </form>
    </div>
    <div class="enterprise-data-panel__body">
        <?php if (! $run): ?>
            <p class="text-muted text-center py-5">No payroll run for selected period.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead><tr><th>Employee</th><th>Nationality</th><th>Monthly EOSB (Employer)</th></tr></thead>
                    <tbody>
                        <?php $total = 0; foreach ($items as $item): $total += (float) $item['employer_eosb']; ?>
                            <tr>
                                <td><?= esc($item['full_name']) ?></td>
                                <td><?= esc($item['nationality'] ?: '—') ?></td>
                                <td><?= $fmt($item['employer_eosb']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <tr class="fw-bold border-top"><td colspan="2">Total EOSB Payable to SIO</td><td><?= $fmt($total) ?> BHD</td></tr>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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