<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $status = (string)($this->data['status'] ?? '');
    $statuses = ['DRAFT','IN_PROGRESS','DONE','REVIEWED','CLIENT_ACCEPTED','CANCELLED'];
    $chip = [
        'DRAFT'           => ['#F1F5F9','#334155','Draft'],
        'IN_PROGRESS'     => ['#FEF3E7','#8B6914','In progress'],
        'DONE'            => ['#EEF2F6','#7A8BA1','Done'],
        'REVIEWED'        => ['#EAF2E8','#3F5C43','Reviewed'],
        'CLIENT_ACCEPTED' => ['#EAF2E8','#3F5C43','Client accepted'],
        '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-toolbox me-2 text-muted"></i>PM Executions
            <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; ?>PmServices" 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; ?>PmServices?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>PM #</th><th>Contract</th><th>Site · Asset</th><th>Template</th><th>Scheduled</th><th>Done by</th><th>Status</th><th></th></tr></thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $c = $chip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>PmServices/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['pm_no']); ?></strong></a></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']); ?> · <strong><?= htmlspecialchars((string)$r['asset_no']); ?></strong>
                            <div class="text-muted" style="font-size:.6rem;"><?php if (!empty($r['asset_icon'])): ?><i class="fas <?= htmlspecialchars($r['asset_icon']); ?> me-1"></i><?php endif; ?><?= htmlspecialchars((string)($r['asset_type_code'] ?? '')); ?></div>
                        </td>
                        <td class="small"><?= htmlspecialchars((string)$r['template_code']); ?><div class="text-muted" style="font-size:.6rem;"><?= htmlspecialchars((string)$r['template_name']); ?></div></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['scheduled_for'] ?? '—')); ?></td>
                        <td class="small text-muted"><?= htmlspecialchars((string)($r['done_by_name'] ?? '—')); ?></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; ?>PmServices/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="8" class="text-center text-muted py-4">No PM executions yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
