<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $f    = $this->data['filter'] ?? [];
    $q    = 'stage=' . urlencode((string)($f['stage'] ?? '')) . '&q=' . urlencode((string)($f['q'] ?? '')) . ($f['overdue'] ? '&overdue=1' : '');

    $stageChip = [
        'NEW'         => ['bg'=>'#E2E8F0','ink'=>'#334155'],
        'QUALIFYING'  => ['bg'=>'#F1F5F9','ink'=>'#475569'],
        'QUOTING'     => ['bg'=>'#FEF3C7','ink'=>'#92400E'],
        'SUBMITTED'   => ['bg'=>'#DBEAFE','ink'=>'#1E40AF'],
        'WON'         => ['bg'=>'#DCFCE7','ink'=>'#166534'],
        'LOST'        => ['bg'=>'#F1F5F9','ink'=>'#64748B'],
    ];

    $totalOpp = 0; $wonOpp = 0; $overdueCount = 0;
    foreach ($rows as $r) {
        $totalOpp += (float)$r['opportunity_value'];
        if ($r['status'] === 'WON') $wonOpp += (float)$r['opportunity_value'];
        if (!empty($r['next_action_date']) && $r['next_action_date'] < date('Y-m-d') && !in_array($r['status'], ['WON','LOST'], true)) $overdueCount++;
    }
?>

<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-address-book me-2" style="color:#7A8BA1;"></i>Business Leads · Pipeline</h5>
    <div class="d-flex gap-2">
        <a href="<?= PROOT; ?>BusinessLeads/xlsx?<?= $q; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-file-excel me-1" style="color:#7A9E7E;"></i>Excel</a>
        <a href="<?= PROOT; ?>BusinessLeads/pdf?<?= $q; ?>"  class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-file-pdf   me-1" style="color:#C48579;"></i>PDF</a>
        <?php if (UserGroups::can('Bids')): ?><a href="<?= PROOT; ?>Bids" class="btn btn-sm" style="background:#F1F5F9;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-columns me-1" style="color:#7A8BA1;"></i>Board view</a><?php endif; ?>
        <?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>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A8BA1 !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Total leads</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;"><?= count($rows); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A9E7E !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Open pipeline</div>
        <div style="font-weight:700;font-size:1.4rem;color:#334155;">TZS <?= number_format($totalOpp); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid #7A9E7E !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Won this view</div>
        <div style="font-weight:700;font-size:1.4rem;color:#7A9E7E;">TZS <?= number_format($wonOpp); ?></div>
    </div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $overdueCount > 0 ? '#C48579' : '#E2E8F0'; ?> !important;background:#F8FAFC;"><div class="card-body p-3">
        <div style="text-transform:uppercase;letter-spacing:.4px;font-size:.66rem;color:#64748B;">Follow-up overdue</div>
        <div style="font-weight:700;font-size:1.4rem;color:<?= $overdueCount > 0 ? '#C48579' : '#334155'; ?>;"><?= $overdueCount; ?></div>
    </div></div></div>
</div>

<form method="get" action="<?= PROOT; ?>BusinessLeads" 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-3"><label class="form-label" style="color:#334155;">Search</label>
                <input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="lead, customer, note…" class="form-control form-control-sm"></div>
            <div class="col-md-3"><label class="form-label" style="color:#334155;">Stage</label>
                <select name="stage" class="form-select form-select-sm">
                    <option value="">All stages</option>
                    <?php foreach (BusinessLeads::STAGES as $s): ?>
                        <option value="<?= $s; ?>" <?= ($f['stage'] ?? '') === $s ? 'selected' : ''; ?>><?= $s; ?></option>
                    <?php endforeach; ?>
                </select></div>
            <div class="col-md-3"><label class="form-check-label mt-4" style="color:#334155;">
                <input type="checkbox" name="overdue" value="1" <?= !empty($f['overdue']) ? 'checked' : ''; ?>> Only overdue follow-ups
            </label></div>
            <div class="col-md-3 text-end">
                <button type="submit" class="btn btn-sm" style="background:#7A8BA1;border-color:#7A8BA1;color:#fff;font-weight:600;"><i class="fas fa-filter me-1"></i>Filter</button>
            </div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead style="background:#F1F5F9;">
                <tr>
                    <th style="color:#334155;">Lead</th>
                    <th style="color:#334155;">Customer</th>
                    <th style="color:#334155;">Source</th>
                    <th style="color:#334155;">Type</th>
                    <th class="text-end" style="color:#334155;">Opportunity</th>
                    <th style="color:#334155;">Stage</th>
                    <th style="color:#334155;">Next action</th>
                    <th style="color:#334155;">Owner</th>
                    <th style="color:#334155;">Project</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $chip = $stageChip[$r['status']] ?? ['bg'=>'#F1F5F9','ink'=>'#64748B'];
                    $overdue = !empty($r['next_action_date']) && $r['next_action_date'] < date('Y-m-d') && !in_array($r['status'], ['WON','LOST'], true);
                ?>
                    <tr>
                        <td style="color:#334155;font-weight:500;"><?= htmlspecialchars($r['lead_name']); ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['customer_name']); ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['lead_source']); ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['project_type']); ?></td>
                        <td class="text-end" style="color:#334155;font-weight:500;"><?= number_format((float)$r['opportunity_value']); ?></td>
                        <td><span class="badge" style="background:<?= $chip['bg']; ?>;color:<?= $chip['ink']; ?>;font-size:.62rem;"><?= $r['status']; ?></span></td>
                        <td class="text-muted small" style="color:<?= $overdue ? '#C48579' : '#64748B'; ?>;"><?= htmlspecialchars((string)($r['next_action_date'] ?? '—')); ?><?= $overdue ? ' <span style="font-size:.66rem;">(overdue)</span>' : ''; ?></td>
                        <td class="text-muted small" style="color:#64748B;"><?= htmlspecialchars((string)$r['owner_name']); ?></td>
                        <td class="text-muted small">
                            <?php if (!empty($r['project_no'])): ?>
                                <a href="<?= PROOT; ?>Projects" style="color:#7A8BA1;font-weight:500;"><?= htmlspecialchars($r['project_no']); ?></a>
                                <div style="font-size:.65rem;color:#94A3B8;">crm: <?= htmlspecialchars((string)$r['crm_status']); ?></div>
                            <?php else: ?>—<?php endif; ?>
                        </td>
                        <td class="text-end"><a href="<?= PROOT; ?>BusinessLeads/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm" style="background:#F8FAFC;border:1px solid #E2E8F0;color:#334155;"><i class="fas fa-eye me-1" style="color:#7A8BA1;"></i>View</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="10" class="text-center py-4" style="color:#94A3B8;">No leads. <?php if (UserGroups::can('BusinessLeads','add')): ?><a href="<?= PROOT; ?>BusinessLeads/add" style="color:#7A8BA1;font-weight:600;">Add your first lead</a>.<?php endif; ?></td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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