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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Payslip',
    'workspaceTitle'      => 'Payslip — ' . esc($item['full_name']),
    'workspaceAction'     => [
        'href'  => 'javascript:window.print()',
        'label' => 'Print',
        'icon'  => 'fa-solid fa-print',
    ],
]) ?>

<article class="enterprise-settings-card" id="payslip-print">
    <div class="text-center mb-4">
        <h3 class="mb-1"><?= esc($company['legal_name'] ?? $company['name']) ?></h3>
        <p class="text-muted small mb-0">Payslip for <?= esc($period) ?></p>
    </div>

    <dl class="row small mb-4">
        <dt class="col-sm-3 text-muted">Employee</dt>
        <dd class="col-sm-9 fw-semibold"><?= esc($item['full_name']) ?></dd>
        <dt class="col-sm-3 text-muted">Employee ID</dt>
        <dd class="col-sm-9"><?= esc($item['employee_number']) ?></dd>
        <dt class="col-sm-3 text-muted">CPR</dt>
        <dd class="col-sm-9"><?= esc($item['cpr_number'] ?: '—') ?></dd>
        <dt class="col-sm-3 text-muted">Job Title</dt>
        <dd class="col-sm-9"><?= esc($item['job_title'] ?: '—') ?></dd>
        <dt class="col-sm-3 text-muted">Bank / IBAN</dt>
        <dd class="col-sm-9"><?= esc(($item['bank_name'] ?: '—') . ' / ' . ($item['bank_account'] ?: '—')) ?></dd>
    </dl>

    <div class="row g-4">
        <div class="col-md-6">
            <h6 class="text-uppercase text-muted small">Earnings</h6>
            <table class="table table-sm">
                <tbody>
                    <?php foreach ($earnings as $line): ?>
                        <tr>
                            <td><?= esc($line['name']) ?></td>
                            <td class="text-end"><?= $fmt($line['amount']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr class="fw-bold border-top">
                        <td>Gross Earnings</td>
                        <td class="text-end"><?= $fmt($item['gross_earnings']) ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="col-md-6">
            <h6 class="text-uppercase text-muted small">Deductions</h6>
            <table class="table table-sm">
                <tbody>
                    <?php foreach ($deductions as $line): ?>
                        <tr>
                            <td><?= esc($line['name']) ?></td>
                            <td class="text-end"><?= $fmt(abs((float) $line['amount'])) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr class="fw-bold border-top">
                        <td>Total Deductions</td>
                        <td class="text-end"><?= $fmt($item['total_deductions']) ?></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="bg-light rounded p-3 mt-3 d-flex justify-content-between align-items-center">
        <span class="fw-bold">Net Pay (BHD)</span>
        <span class="fs-4 fw-bold"><?= $fmt($item['net_pay']) ?></span>
    </div>

    <div class="mt-3 small text-muted">
        Days worked: <?= number_format((float) $item['days_worked'], 1) ?> |
        Absent: <?= number_format((float) $item['days_absent'], 1) ?> |
        Unpaid leave: <?= number_format((float) $item['unpaid_leave_days'], 1) ?>
    </div>
</article>

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