<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$isBahraini = static function (array $item): bool {
    $n = strtolower(trim((string) ($item['nationality'] ?? '')));
    return in_array($n, ['bahrain', 'bahraini', 'bh', 'kingdom of bahrain'], true) || str_contains($n, 'bahrain');
};
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'SIO Report',
    'workspaceTitle'      => 'Social Insurance (SIO) Report',
    'workspaceDesc'       => 'Employee and employer SIO contributions for Bahraini nationals and expatriates.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/reports/sio') ?>" 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/sio', '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>Type</th>
                            <th>Employer SIO</th>
                            <th>Employee Deduction</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $totalEr = 0;
                        $totalEe = 0;
                        foreach ($items as $item):
                            $bh = $isBahraini($item);
                            $eeSio = (float) ($item['employee_sio'] ?? 0);
                            $totalEr += (float) $item['employer_sio'];
                            $totalEe += $eeSio;
                        ?>
                            <tr>
                                <td><?= esc($item['full_name']) ?></td>
                                <td><?= esc($item['nationality'] ?: '—') ?></td>
                                <td><?= $bh ? 'Bahraini' : 'Expatriate' ?></td>
                                <td><?= $fmt($item['employer_sio']) ?></td>
                                <td><?= $fmt($eeSio) ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <tr class="fw-bold border-top">
                            <td colspan="3">Totals</td>
                            <td><?= $fmt($totalEr) ?></td>
                            <td><?= $fmt($totalEe) ?></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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