<?php $this->start('body'); ?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-clipboard-check me-2 text-muted"></i>CRM Onboarding Queue</h5>
    <a href="<?= PROOT; ?>Projects/add" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;">
        <i class="fas fa-plus me-1"></i> New Project
    </a>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Project #</th><th>Name</th><th>Type</th><th>Customer</th>
                    <th>Origin</th><th>Status</th><th>Checklist</th><th>Value</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $crmClass = [
                        'SUBMITTED'   => 'bg-info text-white',
                        'IN_PROGRESS' => 'bg-warning text-dark',
                        'NOT_STARTED' => 'bg-light text-muted',
                    ][$r['crm_status']] ?? 'bg-light text-muted';
                    $pct = ((int)$r['ck_total'] > 0) ? round((int)$r['ck_done'] * 100 / (int)$r['ck_total']) : 0;
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Projects/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['project_no']); ?></strong></a></td>
                        <td><?= htmlspecialchars($r['project_name']); ?></td>
                        <td style="text-transform:capitalize;"><?= htmlspecialchars($r['project_type']); ?></td>
                        <td class="text-muted"><?= htmlspecialchars($r['project_customer']); ?></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['origin']))); ?></span></td>
                        <td><span class="badge <?= $crmClass; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['crm_status']))); ?></span></td>
                        <td style="width:180px;">
                            <div class="d-flex align-items-center gap-2">
                                <div class="progress flex-grow-1" style="height:5px;">
                                    <div class="progress-bar" style="width:<?= $pct; ?>%;background:<?= BRAND_COLOR; ?>;"></div>
                                </div>
                                <span class="text-muted small"><?= (int)$r['ck_done']; ?>/<?= (int)$r['ck_total']; ?></span>
                            </div>
                        </td>
                        <td class="text-end"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['contract_value']); ?></td>
                        <td class="text-end">
                            <?php if (!empty($r['approval_id'])): ?>
                                <a href="<?= PROOT; ?>FinChains/view/<?= (int)$r['approval_id']; ?>" class="btn btn-sm btn-outline-primary" title="Open approval"><i class="fas fa-hourglass-half"></i></a>
                            <?php endif; ?>
                            <a href="<?= PROOT; ?>Projects/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($this->data['rows'])): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4">Onboarding queue is empty — nothing pending.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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