<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$periodLabel = $run ? date('F Y', mktime(0, 0, 0, (int) $run['period_month'], 1, (int) $run['period_year'])) : '';
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'Reports',
    'workspaceCrumbCurrent' => 'Payroll Summary',
    'workspaceTitle'      => 'Payroll Summary Report',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/reports/summary') ?>" 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/summary',
                    'query'      => $filters,
                ]) ?>
                </div>
            </div>
        </form>
    </div>
    <?php if ($run && $totals): ?>
        <div class="enterprise-data-panel__body">
            <h3 class="h6 mb-3"><?= esc($periodLabel) ?> — <?= esc(ucfirst($run['status'])) ?></h3>
            <div class="row g-3 mb-4">
                <div class="col-md-4"><div class="p-3 border rounded"><div class="text-muted small">Total Gross</div><div class="fw-bold"><?= $fmt($totals['total_gross']) ?> BHD</div></div></div>
                <div class="col-md-4"><div class="p-3 border rounded"><div class="text-muted small">Total Net</div><div class="fw-bold"><?= $fmt($totals['total_net']) ?> BHD</div></div></div>
                <div class="col-md-4"><div class="p-3 border rounded"><div class="text-muted small">Employer Total Cost</div><div class="fw-bold"><?= $fmt($totals['total_cost']) ?> BHD</div></div></div>
            </div>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead><tr><th>Employee</th><th>Gross</th><th>Deductions</th><th>Net</th><th>Employer Cost</th></tr></thead>
                    <tbody>
                        <?php foreach ($items as $item): ?>
                            <tr>
                                <td><?= esc($item['full_name']) ?></td>
                                <td><?= $fmt($item['gross_earnings']) ?></td>
                                <td><?= $fmt($item['total_deductions']) ?></td>
                                <td><?= $fmt($item['net_pay']) ?></td>
                                <td><?= $fmt($item['employer_total_cost']) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php else: ?>
        <div class="enterprise-data-panel__body"><p class="text-muted text-center py-5">No payroll run for selected period.</p></div>
    <?php endif; ?>
</div>

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