<?php $this->start('body');
    $rows = $this->data['rows'] ?? [];
    $status = $this->data['status'] ?? '';
    $today = date('Y-m-d');
    $scopeColor = [
        'PM' => '#7A9E7E', 'INSPECTION' => '#6b8fbe', 'HANDOVER' => '#a78bfa',
        'RECEIVING' => '#D4A574', 'SAFETY' => '#dc2626', 'CHECKLIST' => '#94A3B8',
    ];
    $statusPill = [
        'DRAFT'     => ['#f59e0b','In progress'],
        'SUBMITTED' => ['#0ea5e9','Submitted'],
        'APPROVED'  => ['#22c55e','Approved'],
        'REJECTED'  => ['#dc2626','Rejected'],
        'CANCELLED' => ['#94a3b8','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-tasks me-1 text-muted"></i> My Work Queue</h5>
        <div class="text-muted" style="font-size:.72rem;"><?= count($rows); ?> item<?= count($rows) === 1 ? '' : 's' ?> assigned to me</div>
    </div>
    <div class="btn-group">
        <a href="?" class="btn btn-sm <?= $status === '' ? 'btn-primary' : 'btn-outline-secondary'; ?>">All</a>
        <a href="?status=DRAFT" class="btn btn-sm <?= $status === 'DRAFT' ? 'btn-primary' : 'btn-outline-secondary'; ?>">Open</a>
        <a href="?status=SUBMITTED" class="btn btn-sm <?= $status === 'SUBMITTED' ? 'btn-primary' : 'btn-outline-secondary'; ?>">Submitted</a>
        <a href="?status=APPROVED" class="btn btn-sm <?= $status === 'APPROVED' ? 'btn-primary' : 'btn-outline-secondary'; ?>">Approved</a>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Ref</th>
                    <th>Template</th>
                    <th>Scope</th>
                    <th>Target</th>
                    <th style="width:110px;">Due</th>
                    <th style="width:110px;">Status</th>
                    <th style="width:100px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $overdue = ($r['scheduled_for'] && $r['scheduled_for'] < $today && $r['status'] === 'DRAFT');
                    $sp = $statusPill[$r['status']] ?? ['#94a3b8', $r['status']];
                    $sc = $scopeColor[$r['scope']] ?? '#94A3B8';
                ?>
                    <tr>
                        <td>
                            <a href="<?= PROOT; ?>WorkInstances/view/<?= (int)$r['c0d']; ?>"><strong><?= htmlspecialchars($r['ref_label']); ?></strong></a>
                        </td>
                        <td class="text-muted small"><?= htmlspecialchars($r['template_title']); ?> <span style="color:#94A3B8;">v<?= (int)$r['template_version']; ?></span></td>
                        <td><span class="badge" style="background:<?= $sc; ?>;color:#fff;"><?= htmlspecialchars($r['scope']); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['target_entity_type']); ?> #<?= (int)$r['target_entity_id']; ?></td>
                        <td class="small <?= $overdue ? 'text-danger' : 'text-muted'; ?>">
                            <?= htmlspecialchars($r['scheduled_for'] ?: '—'); ?>
                            <?php if ($overdue): ?><div>overdue</div><?php endif; ?>
                        </td>
                        <td><span class="badge" style="background:<?= $sp[0]; ?>;color:#fff;text-transform:capitalize;"><?= htmlspecialchars($sp[1]); ?></span></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>WorkInstances/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i> Open</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No items assigned.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $this->end(); ?>
