<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$gross = static function (array $s) use ($fmt): string {
    $total = (float) $s['basic_salary'] + (float) $s['social_allowance']
        + (float) $s['housing_allowance'] + (float) $s['transport_allowance']
        + (float) $s['other_allowances'];
    return $fmt($total);
};
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Salary Structures',
    'workspaceTitle'      => 'Employee Salary Assignment',
    'workspaceDesc'       => 'Basic salary and allowances per employee. SIO and EOSB are calculated on basic + social allowance.',
    'workspaceAction'     => [
        'href'  => base_url('payroll/salary/create'),
        'label' => 'Assign Salary',
        'icon'  => 'fa-solid fa-plus',
    ],
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Current Salary Structures</h2>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/salary') ?>" 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" placeholder="Employee name or ID"
                           value="<?= esc($filters['search']) ?>">
                </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>Basic (BHD)</th>
                        <th>Social</th>
                        <th>Housing</th>
                        <th>Transport</th>
                        <th>Gross</th>
                        <th>Effective From</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($structures as $s): ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($s['full_name']) ?></span>
                                <div class="text-muted small"><?= esc($s['employee_number']) ?></div>
                            </td>
                            <td><?= $fmt($s['basic_salary']) ?></td>
                            <td><?= $fmt($s['social_allowance']) ?></td>
                            <td><?= $fmt($s['housing_allowance']) ?></td>
                            <td><?= $fmt($s['transport_allowance']) ?></td>
                            <td class="fw-semibold"><?= $gross($s) ?></td>
                            <td><?= esc($s['effective_from']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('payroll/salary/' . $s['id'] . '/edit') ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="fa-solid fa-pen"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($structures) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No salary structures assigned yet.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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