<?php
$fmt = static fn ($v) => number_format((float) $v, 3);
$statusPill = static function (string $status): string {
    $map = ['draft' => 'inactive', 'calculated' => 'on_leave', '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' => esc($run['title']),
    'workspaceTitle'      => 'Payroll Run Detail',
    'workspaceDesc'       => 'Review calculated amounts, approve for payment, and export WPS bank file.',
]) ?>

<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="enterprise-settings-card p-3 text-center">
            <div class="text-muted small">Employees</div>
            <div class="fs-4 fw-bold"><?= (int) ($totals['employee_count'] ?? 0) ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="enterprise-settings-card p-3 text-center">
            <div class="text-muted small">Total Net (BHD)</div>
            <div class="fs-5 fw-bold"><?= $fmt($totals['total_net'] ?? 0) ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="enterprise-settings-card p-3 text-center">
            <div class="text-muted small">Employer SIO</div>
            <div class="fs-5 fw-bold"><?= $fmt($totals['total_sio'] ?? 0) ?></div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="enterprise-settings-card p-3 text-center">
            <div class="text-muted small">Employer EOSB</div>
            <div class="fs-5 fw-bold"><?= $fmt($totals['total_eosb'] ?? 0) ?></div>
        </div>
    </div>
</div>

<article class="enterprise-settings-card mb-4">
    <div class="d-flex flex-wrap gap-2 align-items-center justify-content-between">
        <div>
            <strong>Status:</strong> <?= $statusPill($run['status']) ?>
            <?php if ($run['approved_at']): ?>
                <span class="text-muted small ms-2">Approved <?= esc($run['approved_at']) ?></span>
            <?php endif; ?>
        </div>
        <div class="d-flex flex-wrap gap-2">
            <?php if (in_array($run['status'], ['draft', 'calculated'], true)): ?>
                <form action="<?= base_url('payroll/runs/' . $run['id'] . '/calculate') ?>" method="post" data-tpin-confirm>
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-primary btn-sm"><i class="fa-solid fa-calculator me-1"></i> Calculate</button>
                </form>
            <?php endif; ?>
            <?php if ($run['status'] === 'calculated'): ?>
                <form action="<?= base_url('payroll/runs/' . $run['id'] . '/approve') ?>" method="post" data-tpin-confirm>
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success btn-sm"><i class="fa-solid fa-check me-1"></i> Approve</button>
                </form>
            <?php endif; ?>
            <?php if ($run['status'] === 'approved'): ?>
                <form action="<?= base_url('payroll/runs/' . $run['id'] . '/paid') ?>" method="post" data-tpin-confirm>
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success btn-sm"><i class="fa-solid fa-money-bill me-1"></i> Mark Paid</button>
                </form>
            <?php endif; ?>
            <?php if (in_array($run['status'], ['approved', 'paid'], true)): ?>
                <a href="<?= base_url('payroll/runs/' . $run['id'] . '/wps') ?>" class="btn btn-outline-secondary btn-sm" data-tpin-confirm>
                    <i class="fa-solid fa-file-export me-1"></i> WPS Export
                </a>
            <?php endif; ?>
            <?php if (! in_array($run['status'], ['paid', 'locked'], true)): ?>
                <form action="<?= base_url('payroll/runs/' . $run['id'] . '/delete') ?>" method="post"
                      data-tpin-confirm data-confirm-message="Delete this payroll run?">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm"><i class="fa-solid fa-trash"></i></button>
                </form>
            <?php endif; ?>
        </div>
    </div>
</article>
<?= view('Modules\Tpin\Views\partials\verify_modal') ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('payroll/runs/' . $run['id']) ?>" 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" 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>Absent</th>
                        <th>Unpaid Leave</th>
                        <th>OT (min)</th>
                        <th>Gross</th>
                        <th>Deductions</th>
                        <th>Net Pay</th>
                        <th class="text-end">Payslip</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($items as $item): ?>
                        <tr>
                            <td>
                                <span class="enterprise-data-table__primary"><?= esc($item['full_name']) ?></span>
                                <div class="text-muted small"><?= esc($item['employee_number']) ?></div>
                            </td>
                            <td><?= number_format((float) $item['days_absent'], 1) ?></td>
                            <td><?= number_format((float) $item['unpaid_leave_days'], 1) ?></td>
                            <td><?= (int) $item['overtime_minutes_day'] + (int) $item['overtime_minutes_night'] ?></td>
                            <td><?= $fmt($item['gross_earnings']) ?></td>
                            <td><?= $fmt($item['total_deductions']) ?></td>
                            <td class="fw-semibold"><?= $fmt($item['net_pay']) ?></td>
                            <td class="text-end">
                                <a href="<?= base_url('payroll/payslips/' . $item['id']) ?>" class="btn btn-sm btn-outline-secondary">
                                    <i class="fa-solid fa-receipt"></i>
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($items) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No payroll items. Click Calculate to process employees.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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