<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $status = (string)($this->data['status'] ?? '');
    $statuses = ['OPEN','DISPATCHED','ON_SITE','RESOLVED','CLIENT_ACCEPTED','CLOSED','CANCELLED'];
    $chip = [
        'OPEN'            => ['#F5EAE1','#C48579','Open'],
        'DISPATCHED'      => ['#FEF3E7','#8B6914','Dispatched'],
        'ON_SITE'         => ['#EEF2F6','#7A8BA1','On site'],
        'RESOLVED'        => ['#EAF2E8','#3F5C43','Resolved'],
        'CLIENT_ACCEPTED' => ['#EAF2E8','#3F5C43','Client accepted'],
        'CLOSED'          => ['#F1F5F9','#334155','Closed'],
        'CANCELLED'       => ['#F5EAE1','#C48579','Cancelled'],
    ];
?>
<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-ambulance me-2 text-muted"></i>Callouts
            <span class="text-muted ms-2" style="font-weight:400;font-size:.7rem;"><?= count($rows); ?> total</span>
        </h5>
    </div>
</div>

<div class="mb-2 d-flex flex-wrap gap-1">
    <a href="<?= PROOT; ?>Callouts" class="badge <?= $status===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All</a>
    <?php foreach ($statuses as $s): ?>
        <a href="<?= PROOT; ?>Callouts?status=<?= $s; ?>" class="badge <?= $status===$s?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ', $s))); ?></a>
    <?php endforeach; ?>
</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>Callout #</th><th>Reported</th><th>Contract</th><th>Site · Asset</th><th>Category</th><th>Description</th><th>SLA breach</th><th>Status</th><th></th></tr></thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $c = $chip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                    $breach = null;
                    if ($r['sla_response_breached_hrs'] > 0 || $r['sla_restore_breached_hrs'] > 0) {
                        $breach = 'R:' . number_format((float)$r['sla_response_breached_hrs'], 1) . 'h · S:' . number_format((float)$r['sla_restore_breached_hrs'], 1) . 'h';
                    }
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Callouts/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['callout_no']); ?></strong></a>
                            <?php if ((int)$r['is_emergency'] === 1): ?><div><span class="badge bg-danger" style="font-size:.55rem;">EMERGENCY</span></div><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= htmlspecialchars((string)$r['reported_at']); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['contract_no'] ?? '—')); ?><div style="font-size:.6rem;"><?= htmlspecialchars((string)($r['customer_name'] ?? '')); ?></div></td>
                        <td class="small"><?= htmlspecialchars((string)($r['site_name'] ?? '—')); ?><?php if (!empty($r['asset_no'])): ?> · <strong><?= htmlspecialchars((string)$r['asset_no']); ?></strong><?php endif; ?></td>
                        <td class="small"><span class="badge" style="background:<?= htmlspecialchars((string)($r['color_hex'] ?? '#7A8BA1')); ?>;color:#fff;font-size:.6rem;"><?= htmlspecialchars((string)$r['cat_code']); ?></span></td>
                        <td class="small text-muted"><?= htmlspecialchars(mb_strimwidth((string)($r['description'] ?? ''), 0, 60, '…')); ?></td>
                        <td class="small text-muted"><?= $breach ? '<span style="color:#C48579;font-weight:600;font-size:.62rem;">' . $breach . '</span>' : '—'; ?></td>
                        <td><span class="badge" style="background:<?= $c[0]; ?>;color:<?= $c[1]; ?>;border:1px solid <?= $c[1]; ?>;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($c[2]); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Callouts/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($rows)): ?><tr><td colspan="9" class="text-center text-muted py-4">No callouts yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
