<?php $this->start('body');
    $tasks     = $this->data['tasks']     ?? [];
    $f         = $this->data['filter']    ?? [];
    $page      = (int)($this->data['page']      ?? 1);
    $pageSize  = (int)($this->data['page_size'] ?? 25);
    $total     = (int)($this->data['total']     ?? count($tasks));
    $lastPage  = (int)($this->data['last_page'] ?? 1);
    $customers = $this->data['customers'] ?? [];
    $today = date('Y-m-d');

    $q  = 'q='          . urlencode((string)($f['q']            ?? ''))
        . '&type='      . urlencode((string)($f['project_type'] ?? ''))
        . '&customer_id=' . urlencode((string)($f['customer_id']?? ''));
?>
<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-list-check me-1 text-muted"></i> My Onboarding Tasks</h5>
        <div class="text-muted" style="font-size:.72rem;"><?= $total; ?> open task<?= $total === 1 ? '' : 's' ?> across your projects</div>
    </div>
</div>

<!-- Filters -->
<form method="get" action="<?= PROOT; ?>ProjectStarter/myQueue" 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-4">
                <label class="form-label" style="color:#334155;font-size:.72rem;">Search</label>
                <input type="text" name="q" value="<?= htmlspecialchars((string)($f['q'] ?? '')); ?>" placeholder="project no, name, task…" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;font-size:.72rem;">Type</label>
                <select name="type" class="form-select form-select-sm">
                    <option value="">All</option>
                    <?php foreach (['goods','services','works'] as $tp): ?>
                        <option value="<?= $tp; ?>" <?= ($f['project_type'] ?? '') === $tp ? 'selected' : ''; ?>><?= ucfirst($tp); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label" style="color:#334155;font-size:.72rem;">Customer</label>
                <select name="customer_id" class="form-select form-select-sm">
                    <option value="">All</option>
                    <?php foreach ($customers as $c): ?>
                        <option value="<?= (int)$c['c0d']; ?>" <?= (int)($f['customer_id'] ?? 0) === (int)$c['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars((string)$c['name']); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2 d-grid gap-1">
                <button type="submit" class="btn btn-sm" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;color:#fff;"><i class="fas fa-filter me-1"></i>Filter</button>
                <a href="<?= PROOT; ?>ProjectStarter/myQueue" class="btn btn-sm btn-outline-secondary" title="Reset filters"><i class="fas fa-times me-1"></i>Reset</a>
            </div>
        </div>
    </div>
</form>

<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>Project</th>
                    <th>Task</th>
                    <th>Customer</th>
                    <th style="width:100px;">Due</th>
                    <th style="width:100px;">Type</th>
                    <th style="width:110px;" class="text-end">Action</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($tasks as $t):
                    $overdue = ($t['due_date'] && $t['due_date'] < $today);
                    $link = str_replace('{project_id}', (string)(int)$t['project_id'], (string)$t['target_url_template']);
                ?>
                    <tr>
                        <td>
                            <a href="<?= PROOT; ?>ProjectStarter/index/<?= (int)$t['project_id']; ?>">
                                <strong><?= htmlspecialchars($t['project_no']); ?></strong>
                            </a>
                            <div class="text-muted small"><?= htmlspecialchars($t['project_name']); ?></div>
                        </td>
                        <td>
                            <div><strong><?= htmlspecialchars($t['task_label']); ?></strong></div>
                            <div class="text-muted small"><?= htmlspecialchars($t['task_description']); ?></div>
                        </td>
                        <td class="text-muted small"><?= htmlspecialchars((string)($t['project_customer'] ?? '')); ?></td>
                        <td class="small <?= $overdue ? 'text-danger' : 'text-muted'; ?>">
                            <?= htmlspecialchars($t['due_date'] ?? '&mdash;'); ?>
                            <?php if ($overdue): ?><div>overdue</div><?php endif; ?>
                        </td>
                        <td><span class="badge bg-light text-dark" style="text-transform:capitalize;"><?= htmlspecialchars($t['project_type']); ?></span></td>
                        <td class="text-end">
                            <a href="<?= PROOT . htmlspecialchars($link); ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-external-link-alt"></i> Open</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($tasks)): ?>
                    <tr><td colspan="6" class="text-center text-muted py-3">
                        No open onboarding tasks match these filters.
                        <?php if (($f['q'] ?? '') !== '' || !empty($f['project_type']) || !empty($f['customer_id'])): ?>
                            <a href="<?= PROOT; ?>ProjectStarter/myQueue">Reset filters</a>
                        <?php endif; ?>
                    </td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
    <?php if ($lastPage > 1):
        $firstIdx = $total === 0 ? 0 : ($page - 1) * $pageSize + 1;
        $lastIdx  = min($page * $pageSize, $total);
        $baseHref = PROOT . 'ProjectStarter/myQueue?' . $q . '&page=';
        $window = 2;
        $pages = [];
        for ($i = 1; $i <= $lastPage; $i++) if ($i === 1 || $i === $lastPage || abs($i - $page) <= $window) $pages[] = $i;
    ?>
    <div class="card-footer bg-white d-flex align-items-center justify-content-between p-2" style="border-top:1px solid #E2E8F0;">
        <div class="small" style="color:#64748B;">Showing <strong style="color:#334155;"><?= $firstIdx; ?></strong>&ndash;<strong style="color:#334155;"><?= $lastIdx; ?></strong> of <strong style="color:#334155;"><?= $total; ?></strong></div>
        <nav aria-label="Onboarding queue pagination">
            <ul class="pagination pagination-sm mb-0">
                <li class="page-item <?= $page <= 1 ? 'disabled' : ''; ?>">
                    <a class="page-link" href="<?= $baseHref . max(1, $page - 1); ?>" style="color:#7A8BA1;"><i class="fas fa-chevron-left"></i></a>
                </li>
                <?php $prev = 0; foreach ($pages as $p): ?>
                    <?php if ($prev && $p - $prev > 1): ?>
                        <li class="page-item disabled"><span class="page-link" style="color:#94A3B8;">&hellip;</span></li>
                    <?php endif; ?>
                    <li class="page-item <?= $p === $page ? 'active' : ''; ?>">
                        <?php if ($p === $page): ?>
                            <span class="page-link" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;color:#fff;"><?= $p; ?></span>
                        <?php else: ?>
                            <a class="page-link" href="<?= $baseHref . $p; ?>" style="color:#334155;"><?= $p; ?></a>
                        <?php endif; ?>
                    </li>
                    <?php $prev = $p; ?>
                <?php endforeach; ?>
                <li class="page-item <?= $page >= $lastPage ? 'disabled' : ''; ?>">
                    <a class="page-link" href="<?= $baseHref . min($lastPage, $page + 1); ?>" style="color:#7A8BA1;"><i class="fas fa-chevron-right"></i></a>
                </li>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>
<?php $this->end(); ?>
