<?php $this->start('body');
    $buckets = $this->data['buckets'] ?? [];
    $stages  = $this->data['stages']  ?? [];
    $f       = $this->data['filter']  ?? [];

    $stageColor = [
        'NEW' => '#7A8BA1', 'QUALIFYING' => '#94A3B8', 'QUOTING' => '#D4A574',
        'SUBMITTED' => '#7A9E7E', 'WON' => '#166534', 'LOST' => '#C48579',
    ];
    $totalOpp = [];
    foreach ($stages as $s) { $totalOpp[$s] = 0; foreach ($buckets[$s] ?? [] as $l) $totalOpp[$s] += (float)$l['opportunity_value']; }
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;color:#334155;"><i class="fas fa-bullseye me-2" style="color:#7A8BA1;"></i>Bids · Pipeline Board</h5>
    <div class="d-flex gap-2">
        <a href="<?= PROOT; ?>BusinessLeads" class="btn btn-sm" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-list me-1" style="color:#7A8BA1;"></i>List view</a>
        <?php if (UserGroups::can('BusinessLeads','add')): ?><a href="<?= PROOT; ?>BusinessLeads/add" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-plus me-1"></i>New lead</a><?php endif; ?>
    </div>
</div>

<form method="get" action="<?= PROOT; ?>Bids" class="card border-0 shadow-sm mb-3" style="background:#F8FAFC;">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-4"><input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="Search leads…" class="form-control form-control-sm"></div>
            <div class="col-md-2"><button type="submit" class="btn btn-sm w-100" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;"><i class="fas fa-filter"></i></button></div>
        </div>
    </div>
</form>

<div class="row g-2" style="min-height:520px;">
    <?php foreach ($stages as $s):
        $items = $buckets[$s] ?? [];
        $col   = $stageColor[$s] ?? '#94A3B8';
    ?>
        <div class="col">
            <div class="card border-0 shadow-sm h-100" style="background:#F8FAFC;">
                <div class="card-header py-2 d-flex align-items-center justify-content-between" style="background:#fff;border-bottom:3px solid <?= $col; ?>;">
                    <div>
                        <strong style="color:#334155;font-size:.86rem;letter-spacing:.3px;"><?= $s; ?></strong>
                        <div style="color:#64748B;font-size:.7rem;"><?= count($items); ?> lead(s)</div>
                    </div>
                    <div style="text-align:right;">
                        <div style="color:#334155;font-weight:600;font-size:.82rem;"><?= number_format($totalOpp[$s]); ?></div>
                        <div style="color:#94A3B8;font-size:.66rem;">TZS</div>
                    </div>
                </div>
                <div class="card-body p-2" style="max-height:600px;overflow-y:auto;">
                    <?php foreach ($items as $l): ?>
                        <a href="<?= PROOT; ?>BusinessLeads/view/<?= (int)$l['c0d']; ?>" class="text-decoration-none">
                            <div class="mb-2 p-2" style="background:#fff;border:1px solid #E2E8F0;border-left:3px solid <?= $col; ?>;border-radius:3px;">
                                <div style="color:#334155;font-weight:600;font-size:.82rem;line-height:1.25;"><?= htmlspecialchars($l['lead_name']); ?></div>
                                <div class="mt-1" style="color:#64748B;font-size:.72rem;"><?= htmlspecialchars((string)$l['customer_name']); ?></div>
                                <div class="mt-1 d-flex align-items-center justify-content-between">
                                    <span style="color:#334155;font-weight:500;font-size:.76rem;">TZS <?= number_format((float)$l['opportunity_value']); ?></span>
                                    <?php if (!empty($l['next_action_date'])):
                                        $overdue = $l['next_action_date'] < date('Y-m-d') && !in_array($l['status'], ['WON','LOST'], true);
                                    ?>
                                        <span style="color:<?= $overdue ? '#C48579' : '#94A3B8'; ?>;font-size:.68rem;"><i class="fas fa-clock me-1"></i><?= htmlspecialchars($l['next_action_date']); ?></span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </a>
                    <?php endforeach; ?>
                    <?php if (empty($items)): ?><div class="text-center py-3" style="color:#94A3B8;font-size:.74rem;">Empty</div><?php endif; ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

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