<?php $this->start('body');
    $items = $this->data['items'] ?? [];
    $unread = 0; foreach ($items as $it) if (empty($it['read_on'])) $unread++;
    $kindPill = [
        'TENDER_WON'              => ['#f59e0b', 'Won · terms needed'],
        'TENDER_AWARDED'          => ['#0ea5e9', 'Awarded · approval needed'],
        'CONTRACT_APPROVED'       => ['#22c55e', 'Contract approved'],
        'CONTRACT_APPROVED_FM'    => ['#7A9E7E', 'Contract approved · FYI'],
        'CONTRACT_PENDING_FM'     => ['#7A8BA1', 'Contract · FM verify'],
        'CONTRACT_PENDING_EXEC'   => ['#8B6914', 'Contract · Exec approve'],
        'CONTRACT_SENT_BACK'      => ['#D4A574', 'Contract sent back'],
        'CONTRACT_RETURNED'       => ['#D4A574', 'Contract returned to CRM'],
        'CONTRACT_DECLINED'       => ['#C48579', 'Contract declined'],
        'PROJECT_ASSIGNED_PM'     => ['#7A9E7E', 'Assigned as PM'],
        'PROJECT_ASSIGNED_ACC'    => ['#7A9E7E', 'Seated as Accountant'],
        'PROJECT_ASSIGNED_PROC'   => ['#7A9E7E', 'Seated as Procurement'],
        'PROJECT_ASSIGNED_PC'     => ['#7A9E7E', 'Assigned as PC'],
        'PO_READY_FOR_SOURCING'   => ['#7A8BA1', 'Client PO ready to source'],
        'PO_HANDOVER_FYI'         => ['#94A3B8', 'Handover · FYI'],
        'CASH_REQUEST_PENDING_PM' => ['#7A8BA1', 'Cash request · PM check'],
        'RETIREMENT_PENDING_ACCT' => ['#8B6914', 'Retirement · Accountant review'],
        'RETIREMENT_APPROVED'     => ['#7A9E7E', 'Retirement approved'],
        'RETIREMENT_REJECTED'     => ['#C48579', 'Retirement bounced back'],
        'RETIREMENT_OVERDUE'      => ['#C48579', 'Retirement overdue'],
        'CASH_DISBURSED'          => ['#7A9E7E', 'Cash ready to collect'],
        'RECEIPT_CONFIRMED'       => ['#3F5C43', 'Receipt confirmed'],
        'RECEIPT_AUTO_CONFIRMED'  => ['#8B6914', 'Receipt auto-confirmed'],
        'LPO_ISSUED_TO_SUPPLIER'  => ['#3F5C43', 'Supplier LPO issued'],
        'PR_PENDING_PC_CONFIRM'   => ['#8B6914', 'PR · PC confirm'],
        'PR_PENDING_PM'           => ['#8B6914', 'PR · PM review'],
        'PR_PENDING_FM'           => ['#8B6914', 'PR · FM schedule'],
        'PR_PENDING_ACCT'         => ['#8B6914', 'PR · Accountant record'],
        'PR_PAID'                 => ['#3F5C43', 'PR paid'],
        'PR_REJECTED'             => ['#C48579', 'PR rejected'],
        'LPO_CLOSED'              => ['#334155', 'Supplier LPO closed'],
        'RETENTION_CLAIMABLE'     => ['#8B6914', 'Retention claimable'],
        'INVOICE_DUE_SOFT'        => ['#7A8BA1', 'Invoice due soon'],
        'INVOICE_DUE_FIRM'        => ['#8B6914', 'Invoice overdue · firm'],
        'INVOICE_ESCALATED_FM'    => ['#8B6914', 'Invoice · FM escalation'],
        'INVOICE_ESCALATED_EXEC'  => ['#C48579', 'Invoice · Exec escalation'],
        'INVOICE_LEGAL_FLAG'      => ['#C48579', 'Invoice · legal flag'],
        'CASH_BOX_LOW'            => ['#C48579', 'Petty cash low'],
        'ITEM_QUERIED'            => ['#C48579', 'Item queried by Procurement'],
        'ITEM_REOPENED'           => ['#7A8BA1', 'Item reopened by PC'],
        'DISCOVERY_PENDING_PM'    => ['#8B6914', 'Product Discovery · PM check'],
        'PAYEE_ACCOUNT_PENDING_FM'=> ['#8B6914', 'Payee account · FM approve'],
        'PAYEE_ACCOUNT_APPROVED'  => ['#7A9E7E', 'Payee account approved'],
        'SCHEDULE_PENDING_FM'     => ['#8B6914', 'Payment schedule · FM approve'],
        'SCHEDULE_APPROVED'       => ['#7A9E7E', 'Payment schedule approved'],
        'SCHEDULE_READY_TO_EXECUTE'=> ['#8B6914', 'Schedule prepared · execute + record'],
        'SCHEDULE_ITEM_EXCLUDED'  => ['#C48579', 'LPO excluded from schedule'],
        'TERMS_UNLOCKED'          => ['#F59E0B', 'Terms unlocked'],
        'PROJECT_PROVISIONED'     => ['#22c55e', 'Project provisioned'],
        'STARTER_TASK_ASSIGNED'   => ['#a78bfa', 'Onboarding task'],
        'APPROVAL_QUEUED'         => ['#6b8fbe', 'Approval waiting'],
    ];
?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-bell me-1 text-muted"></i> Notifications</h5>
        <div class="text-muted" style="font-size:.72rem;"><?= count($items); ?> total · <?= $unread; ?> unread</div>
    </div>
    <?php if ($unread > 0): ?>
        <button type="button" class="btn btn-sm btn-outline-secondary" onclick="rsMarkAllRead()"><i class="fas fa-check-double me-1"></i> Mark all read</button>
    <?php endif; ?>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th style="width:100px;">When</th><th style="width:170px;">Kind</th><th>Message</th><th style="width:110px;" class="text-end">Action</th></tr>
            </thead>
            <tbody>
                <?php foreach ($items as $it):
                    $meta = $kindPill[$it['kind']] ?? ['#94a3b8', $it['kind']];
                    $unreadRow = empty($it['read_on']);
                ?>
                    <tr style="<?= $unreadRow ? 'background:#fff8e6;' : ''; ?>">
                        <td class="text-muted small"><?= htmlspecialchars($it['created_on']); ?></td>
                        <td><span class="badge" style="background:<?= $meta[0]; ?>;color:#fff;text-transform:capitalize;"><?= htmlspecialchars($meta[1]); ?></span></td>
                        <td>
                            <div><strong><?= htmlspecialchars($it['subject']); ?></strong></div>
                            <?php if (!empty($it['body'])): ?><div class="text-muted small"><?= htmlspecialchars($it['body']); ?></div><?php endif; ?>
                            <?php if (!empty($it['created_by_name'])): ?><div class="text-muted small">by <?= htmlspecialchars($it['created_by_name']); ?></div><?php endif; ?>
                        </td>
                        <td class="text-end">
                            <?php if (!empty($it['target_url'])): ?>
                                <a href="<?= PROOT . htmlspecialchars($it['target_url']); ?>" class="btn btn-sm btn-outline-secondary" onclick="rsMarkRead(<?= (int)$it['c0d']; ?>);"><i class="fas fa-external-link-alt"></i> Open</a>
                            <?php elseif ($unreadRow): ?>
                                <button type="button" class="btn btn-sm btn-outline-secondary" onclick="rsMarkRead(<?= (int)$it['c0d']; ?>);"><i class="fas fa-check"></i> Mark read</button>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($items)): ?>
                    <tr><td colspan="4" class="text-center text-muted py-4">No notifications yet — you'll be pinged when a handoff arrives.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function rsMarkRead(id) {
    fetch('<?= PROOT; ?>Notifications/markReadAj', {
        method: 'POST', headers: {'Content-Type':'application/x-www-form-urlencoded'},
        body: 'id=' + encodeURIComponent(id)
    });
}
function rsMarkAllRead() {
    fetch('<?= PROOT; ?>Notifications/markAllReadAj', { method: 'POST' })
        .then(() => location.reload());
}
</script>
<?php $this->end(); ?>
