<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$p = $preview ?? null;
$inp = $input ?? [];
?>
<?= $this->extend('layouts/enterprise') ?>
<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'  => 'HRM',
    'workspaceCrumbCurrent' => 'New Settlement',
    'workspaceTitle'      => 'Final Settlement Calculator',
    'workspaceDesc'       => 'Calculate leave encashment, legacy EOSB, and total termination payout.',
]) ?>

<article class="enterprise-settings-card mb-4" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-calculator"></i>
        <h2 class="enterprise-settings-card__title">Step 1 — Select Employee</h2>
    </div>
    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
    <form action="<?= base_url('payroll/settlements/preview') ?>" method="post">
        <?= csrf_field() ?>
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field">
                <label class="enterprise-field-label">Employee</label>
                <select name="employee_id" class="form-select" required>
                    <option value="">— Select —</option>
                    <?php foreach ($employees as $e): ?>
                        <option value="<?= $e['id'] ?>" <?= (string) ($inp['employeeId'] ?? old('employee_id')) === (string) $e['id'] ? 'selected' : '' ?>>
                            <?= esc($e['full_name']) ?> (<?= esc($e['employee_number']) ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Termination Date</label>
                <input type="date" name="termination_date" class="form-control" required value="<?= esc($inp['terminationDate'] ?? old('termination_date', date('Y-m-d'))) ?>">
            </div>
            <div class="enterprise-field">
                <label class="enterprise-field-label">Last Working Day</label>
                <input type="date" name="last_working_day" class="form-control" required value="<?= esc($inp['lastWorkingDay'] ?? old('last_working_day', date('Y-m-d'))) ?>">
            </div>
        </div>
        <div class="enterprise-settings-card__footer">
            <button type="submit" class="btn btn-primary">Calculate Preview</button>
        </div>
    </form>
</article>

<?php if ($p): ?>
<article class="enterprise-settings-card" style="max-width:640px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-file-invoice-dollar"></i>
        <h2 class="enterprise-settings-card__title">Step 2 — Confirm & Save</h2>
    </div>
    <dl class="row small mb-3">
        <dt class="col-sm-5 text-muted">Employee</dt><dd class="col-sm-7"><?= esc($p['employee']['full_name']) ?></dd>
        <dt class="col-sm-5 text-muted">Years of Service</dt><dd class="col-sm-7"><?= number_format($p['years_of_service'], 2) ?></dd>
        <dt class="col-sm-5 text-muted">Nationality Type</dt><dd class="col-sm-7"><?= $p['is_bahraini'] ? 'Bahraini' : 'Expatriate' ?></dd>
        <dt class="col-sm-5 text-muted">Unused Leave Days</dt><dd class="col-sm-7"><?= number_format($p['unused_leave_days'], 1) ?></dd>
    </dl>
    <form action="<?= base_url('payroll/settlements') ?>" method="post" data-tpin-confirm>
        <?= csrf_field() ?>
        <input type="hidden" name="employee_id" value="<?= esc($p['employee']['id']) ?>">
        <input type="hidden" name="termination_date" value="<?= esc($inp['terminationDate']) ?>">
        <input type="hidden" name="last_working_day" value="<?= esc($inp['lastWorkingDay']) ?>">
        <div class="enterprise-settings-fields enterprise-settings-fields--single">
            <div class="enterprise-field"><label class="enterprise-field-label">Last Salary (BHD)</label>
                <input type="number" name="last_salary" class="form-control" step="0.001" value="<?= esc($p['last_salary']) ?>"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Leave Encashment (BHD)</label>
                <input type="number" name="leave_encashment" class="form-control" step="0.001" value="<?= esc($p['leave_encashment']) ?>"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Legacy EOSB — Pre Mar 2024 (BHD)</label>
                <input type="number" name="legacy_eosb" class="form-control" step="0.001" value="<?= esc($p['legacy_eosb']) ?>"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Notice Pay (BHD)</label>
                <input type="number" name="notice_pay" class="form-control" step="0.001" value="0"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Other Entitlements (BHD)</label>
                <input type="number" name="other_entitlements" class="form-control" step="0.001" value="0"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Deductions (BHD)</label>
                <input type="number" name="total_deductions" class="form-control" step="0.001" value="0"></div>
            <div class="enterprise-field"><label class="enterprise-field-label">Notes</label>
                <textarea name="notes" class="form-control" rows="2"></textarea></div>
        </div>
        <div class="enterprise-settings-card__footer">
            <a href="<?= base_url('payroll/settlements') ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Save Settlement</button>
        </div>
    </form>
</article>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>
<?php endif; ?>

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