<?php $this->start('body');
    $s        = $this->data['summary']       ?? [];
    $overdue  = $this->data['overdue_ar']    ?? [];
    $dueSoon  = $this->data['due_soon_ap']   ?? [];
    $appr     = $this->data['approvals']     ?? [];
    $activity = $this->data['activity']      ?? [];
    $projects = (int)($this->data['project_count'] ?? 0);
    $chipBg   = ['success'=>'#22c55e','warning'=>'#f59e0b','danger'=>'#ef4444'][$s['chip'] ?? 'success'] ?? '#22c55e';

    $entityRoute = function (string $type, int $id): string {
        return match ($type) {
            'GOODS_LPO', 'LPO', 'lpo'      => 'Lpos/view/' . $id,
            'SALE', 'INVOICE', 'sale'      => 'Sales/view/' . $id,
            'CRM_ONBOARDING'               => 'Onboarding',
            default                        => 'FinChains/myQueue',
        };
    };
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;">Welcome back, <?= $this->v('full_name'); ?></h5>
        <div class="text-muted" style="font-size:.74rem;letter-spacing:.4px;text-transform:uppercase;">
            <?= $this->v('role_name'); ?> · <?= date('D, d M Y'); ?>
        </div>
    </div>
    <span class="badge" style="background:<?= $chipBg; ?>;color:#fff;font-size:.72rem;padding:.4rem .7rem;">
        <i class="fas fa-circle me-1" style="font-size:.5rem;vertical-align:middle;"></i><?= htmlspecialchars($s['chip_label'] ?? ''); ?>
    </span>
</div>

<!-- ── Financial pulse (4 KPIs) ─────────────────────────────────────────── -->
<div class="row g-3 mb-3">
    <div class="col-md-3">
        <a href="<?= PROOT; ?>Cashbook" class="text-decoration-none">
            <div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= ($s['bank']??0)>=0?'#22c55e':'#ef4444'; ?> !important;">
                <div class="card-body p-3">
                    <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;">Bank on hand</div>
                    <div style="font-weight:700;font-size:1.4rem;color:<?= ($s['bank']??0)>=0?'#22c55e':'#ef4444'; ?>;">TZS <?= number_format((float)($s['bank']??0)); ?></div>
                    <div class="text-muted small mt-1">across all bank accounts</div>
                </div>
            </div>
        </a>
    </div>
    <div class="col-md-3">
        <a href="<?= PROOT; ?>CustomerStatement" class="text-decoration-none">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-3">
                    <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;">Receivables · AR</div>
                    <div style="font-weight:700;font-size:1.4rem;color:#22c55e;">+ TZS <?= number_format((float)($s['ar']??0)); ?></div>
                    <div class="text-muted small mt-1"><?= count($overdue); ?> overdue &middot; view ageing</div>
                </div>
            </div>
        </a>
    </div>
    <div class="col-md-3">
        <a href="<?= PROOT; ?>SupplierStatement" class="text-decoration-none">
            <div class="card border-0 shadow-sm h-100">
                <div class="card-body p-3">
                    <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;">Payables · AP</div>
                    <div style="font-weight:700;font-size:1.4rem;color:#ef4444;">&minus; TZS <?= number_format((float)($s['ap']??0)); ?></div>
                    <div class="text-muted small mt-1"><?= count($dueSoon); ?> due soon &middot; view ageing</div>
                </div>
            </div>
        </a>
    </div>
    <div class="col-md-3">
        <a href="<?= PROOT; ?>CashflowForecast" class="text-decoration-none">
            <div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $chipBg; ?> !important;">
                <div class="card-body p-3">
                    <div class="text-muted small" style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;">Projected closing</div>
                    <div style="font-weight:700;font-size:1.4rem;color:<?= ($s['closing']??0)>=0?'#22c55e':'#ef4444'; ?>;">TZS <?= number_format((float)($s['closing']??0)); ?></div>
                    <div class="text-muted small mt-1">after all AR + AP settled</div>
                </div>
            </div>
        </a>
    </div>
</div>

<!-- ── Approvals awaiting Executive + Recent journal activity ──────────── -->
<div class="row g-3 mb-3">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-inbox me-2 text-muted"></i>Awaiting your approval</strong>
                <a href="<?= PROOT; ?>FinChains/myQueue" class="small text-decoration-none">Open queue &rarr;</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Item</th><th>Chain</th><th>Submitted by</th><th class="text-end">Submitted</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($appr as $a): ?>
                            <tr>
                                <td>
                                    <a href="<?= PROOT . $entityRoute((string)$a['entity_type'], (int)$a['entity_id']); ?>" class="text-decoration-none">
                                        <strong><?= htmlspecialchars((string)($a['ref_label'] ?? $a['entity_type'] . ' #' . $a['entity_id'])); ?></strong>
                                    </a>
                                    <div class="text-muted small"><?= htmlspecialchars((string)$a['step_label']); ?></div>
                                </td>
                                <td class="text-muted small"><?= htmlspecialchars((string)$a['chain_key']); ?></td>
                                <td class="text-muted small"><?= htmlspecialchars((string)$a['submitted_by_name']); ?></td>
                                <td class="text-end text-muted small"><?= date('d M', strtotime((string)$a['submitted_on'])); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($appr)): ?><tr><td colspan="4" class="text-center text-muted py-3">Nothing waiting on you. <i class="fas fa-check-circle text-success"></i></td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-stream me-2 text-muted"></i>Recent journal activity</strong>
                <a href="<?= PROOT; ?>Journals" class="small text-decoration-none">All journals &rarr;</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>JE#</th><th>Source</th><th>Narrative</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($activity as $j):
                            $badge = ['INVOICE'=>'#22c55e','RECEIPT'=>'#22c55e','SALE_INVOICE'=>'#22c55e','SALES_RECEIPT'=>'#22c55e','PAYMENT'=>'#ef4444','SUPPLIER_PAYMENT'=>'#ef4444','LPO_PAYMENT'=>'#ef4444'][$j['source_type']] ?? '#94a3b8';
                        ?>
                            <tr>
                                <td class="text-muted small"><?= htmlspecialchars((string)$j['je_no']); ?></td>
                                <td><span class="badge" style="background:<?= $badge; ?>;color:#fff;font-size:.6rem;"><?= htmlspecialchars((string)$j['source_type']); ?></span></td>
                                <td class="text-muted small" style="max-width:220px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?= htmlspecialchars((string)$j['narrative']); ?>">
                                    <?= htmlspecialchars((string)$j['narrative']); ?>
                                </td>
                                <td class="text-end small" style="font-weight:600;">TZS <?= number_format((float)$j['amount']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($activity)): ?><tr><td colspan="4" class="text-center text-muted py-3">No posted journals yet.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- ── Top overdue AR + Top due-soon AP ────────────────────────────────── -->
<div class="row g-3 mb-3">
    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-arrow-down text-success me-2"></i>Top overdue receivables</strong>
                <a href="<?= PROOT; ?>CustomerStatement" class="small text-decoration-none">Full AR ageing &rarr;</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>Invoice</th><th>Customer</th><th class="text-end">Days late</th><th class="text-end">Outstanding</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($overdue as $o):
                            $displayNo = $o['sale_no'] ?: $o['invoice_ref'];
                        ?>
                            <tr>
                                <td><a href="<?= PROOT; ?>Sales/view/<?= (int)$o['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars((string)$displayNo); ?></strong></a></td>
                                <td class="text-muted small"><?= htmlspecialchars((string)$o['customer_name']); ?></td>
                                <td class="text-end"><span class="badge bg-danger" style="font-size:.6rem;"><?= (int)$o['days_overdue']; ?>d</span></td>
                                <td class="text-end" style="font-weight:600;color:#22c55e;">+ <?= number_format((float)$o['outstanding']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($overdue)): ?><tr><td colspan="4" class="text-center text-muted py-3">No overdue invoices. <i class="fas fa-check-circle text-success"></i></td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="col-md-6">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-header bg-white d-flex align-items-center justify-content-between">
                <strong><i class="fas fa-arrow-up text-danger me-2"></i>Payments due soon</strong>
                <a href="<?= PROOT; ?>SupplierStatement" class="small text-decoration-none">Full AP ageing &rarr;</a>
            </div>
            <div class="table-responsive">
                <table class="table table-sm mb-0 align-middle">
                    <thead class="table-light">
                        <tr><th>LPO</th><th>Supplier</th><th class="text-end">Days</th><th class="text-end">Amount</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach ($dueSoon as $d):
                            $days = (int)$d['days_until_due'];
                            $daysLbl = $days < 0 ? '<span class="badge bg-danger" style="font-size:.6rem;">' . abs($days) . 'd late</span>'
                                                 : '<span class="badge bg-secondary" style="font-size:.6rem;">' . $days . 'd</span>';
                        ?>
                            <tr>
                                <td><a href="<?= PROOT; ?>Lpos/view/<?= (int)$d['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars((string)$d['lpo_no']); ?></strong></a></td>
                                <td class="text-muted small"><?= htmlspecialchars((string)$d['payee_name']); ?></td>
                                <td class="text-end"><?= $daysLbl; ?></td>
                                <td class="text-end" style="font-weight:600;color:#ef4444;">&minus; <?= number_format((float)$d['outstanding']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                        <?php if (empty($dueSoon)): ?><tr><td colspan="4" class="text-center text-muted py-3">No open supplier LPOs.</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<!-- ── Quick shortcuts ─────────────────────────────────────────────────── -->
<div class="card border-0 shadow-sm">
    <div class="card-body p-3">
        <div class="d-flex flex-wrap gap-2">
            <a href="<?= PROOT; ?>Projects" class="btn btn-sm btn-light border"><i class="fas fa-project-diagram me-1 text-muted"></i>All projects (<?= $projects; ?>)</a>
            <a href="<?= PROOT; ?>Payables" class="btn btn-sm btn-light border"><i class="fas fa-money-bill-wave me-1 text-muted"></i>Payables queue</a>
            <a href="<?= PROOT; ?>Onboarding" class="btn btn-sm btn-light border"><i class="fas fa-clipboard-check me-1 text-muted"></i>Onboarding queue</a>
            <a href="<?= PROOT; ?>ProjectPnl" class="btn btn-sm btn-light border"><i class="fas fa-chart-pie me-1 text-muted"></i>Project P&amp;L</a>
            <a href="<?= PROOT; ?>VatReturn" class="btn btn-sm btn-light border"><i class="fas fa-percentage me-1 text-muted"></i>VAT return</a>
        </div>
    </div>
</div>

<?php $this->end(); ?>
