<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$statusPill = static function (string $status): string {
    $map = ['draft' => 'inactive', 'approved' => 'active', 'paid' => 'active'];
    $class = $map[$status] ?? 'inactive';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'Settlement #' . $settlement['id'],
    'workspaceTitle'      => 'Final Settlement Detail',
]) ?>

<div class="row g-4">
    <div class="col-lg-8">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-user-xmark"></i>
                <h2 class="enterprise-settings-card__title">Settlement Breakdown</h2>
            </div>
            <dl class="row mb-0 small">
                <dt class="col-sm-4 text-muted">Employee</dt>
                <dd class="col-sm-8 fw-semibold"><?= esc($settlement['full_name']) ?> (<?= esc($settlement['employee_number']) ?>)</dd>
                <dt class="col-sm-4 text-muted">Termination Date</dt>
                <dd class="col-sm-8"><?= esc($settlement['termination_date']) ?></dd>
                <dt class="col-sm-4 text-muted">Last Working Day</dt>
                <dd class="col-sm-8"><?= esc($settlement['last_working_day']) ?></dd>
                <dt class="col-sm-4 text-muted">Years of Service</dt>
                <dd class="col-sm-8"><?= number_format((float) $settlement['years_of_service'], 2) ?></dd>
                <dt class="col-sm-4 text-muted">Status</dt>
                <dd class="col-sm-8"><?= $statusPill($settlement['status']) ?></dd>
                <dt class="col-sm-4 text-muted">Last Salary</dt>
                <dd class="col-sm-8"><?= $fmt($settlement['last_salary']) ?> BHD</dd>
                <dt class="col-sm-4 text-muted">Leave Encashment</dt>
                <dd class="col-sm-8"><?= $fmt($settlement['leave_encashment']) ?> BHD (<?= number_format((float) $settlement['unused_leave_days'], 1) ?> days)</dd>
                <dt class="col-sm-4 text-muted">Legacy EOSB</dt>
                <dd class="col-sm-8"><?= $fmt($settlement['legacy_eosb']) ?> BHD</dd>
                <dt class="col-sm-4 text-muted">Notice Pay</dt>
                <dd class="col-sm-8"><?= $fmt($settlement['notice_pay']) ?> BHD</dd>
                <dt class="col-sm-4 text-muted">Other Entitlements</dt>
                <dd class="col-sm-8"><?= $fmt($settlement['other_entitlements']) ?> BHD</dd>
                <dt class="col-sm-4 text-muted">Deductions</dt>
                <dd class="col-sm-8 text-danger">-<?= $fmt($settlement['total_deductions']) ?> BHD</dd>
                <dt class="col-sm-4 text-muted fw-bold">Total Settlement</dt>
                <dd class="col-sm-8 fs-5 fw-bold"><?= $fmt($settlement['total_settlement']) ?> BHD</dd>
                <?php if ($settlement['notes']): ?>
                    <dt class="col-sm-4 text-muted">Notes</dt>
                    <dd class="col-sm-8"><?= esc($settlement['notes']) ?></dd>
                <?php endif; ?>
            </dl>
        </article>
    </div>
    <div class="col-lg-4">
        <article class="enterprise-settings-card">
            <div class="enterprise-settings-card__head">
                <i class="fa-solid fa-gavel"></i>
                <h2 class="enterprise-settings-card__title">Actions</h2>
            </div>
            <?php if ($settlement['status'] === 'draft'): ?>
                <form action="<?= base_url('payroll/settlements/' . $settlement['id'] . '/approve') ?>" method="post" class="mb-2" data-tpin-confirm>
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success w-100"><i class="fa-solid fa-check me-1"></i> Approve</button>
                </form>
            <?php elseif ($settlement['status'] === 'approved'): ?>
                <form action="<?= base_url('payroll/settlements/' . $settlement['id'] . '/paid') ?>" method="post" data-tpin-confirm>
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary w-100"><i class="fa-solid fa-money-bill me-1"></i> Mark Paid</button>
                </form>
            <?php else: ?>
                <p class="text-muted small mb-0">This settlement has been paid.</p>
            <?php endif; ?>
            <hr>
            <dl class="small mb-0">
                <dt class="text-muted">Bank / IBAN</dt>
                <dd><?= esc($settlement['bank_name'] ?: '—') ?></dd>
                <dd class="font-monospace"><?= esc($settlement['bank_account'] ?: '—') ?></dd>
            </dl>
        </article>
    </div>
</div>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>

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