<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-boxes me-2 text-primary"></i>Products & Services</h5>
    <a class="btn btn-sm btn-primary" href="<?= url('/ProductsServices/create') ?>"><i class="fas fa-plus me-1"></i>New Item</a>
</div>

<?php if (!empty($flash)): ?><div class="alert alert-info py-2 small"><?= e($flash) ?></div><?php endif; ?>

<form method="get" action="<?= url('/ProductsServices') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-3"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search name / SKU…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-2"><select name="type" class="form-select form-select-sm"><option value="">All types</option><option value="product" <?= ($filters['type'] ?? '') === 'product' ? 'selected' : '' ?>>Products</option><option value="service" <?= ($filters['type'] ?? '') === 'service' ? 'selected' : '' ?>>Services</option></select></div>
        <div class="col-md-2"><select name="category" class="form-select form-select-sm"><option value="">All categories</option><?php foreach ($categories as $cat): ?><option value="<?= e($cat) ?>" <?= ($filters['category'] ?? '') === $cat ? 'selected' : '' ?>><?= e($cat) ?></option><?php endforeach; ?></select></div>
        <div class="col-md-2"><select name="sort" class="form-select form-select-sm" onchange="this.form.submit()"><option value="name_asc" <?= ($sort ?? 'name_asc') === 'name_asc' ? 'selected' : '' ?>>Name A–Z</option><option value="name_desc" <?= ($sort ?? '') === 'name_desc' ? 'selected' : '' ?>>Name Z–A</option><option value="price_asc" <?= ($sort ?? '') === 'price_asc' ? 'selected' : '' ?>>Price ↑</option><option value="price_desc" <?= ($sort ?? '') === 'price_desc' ? 'selected' : '' ?>>Price ↓</option><option value="newest" <?= ($sort ?? '') === 'newest' ? 'selected' : '' ?>>Newest</option><option value="oldest" <?= ($sort ?? '') === 'oldest' ? 'selected' : '' ?>>Oldest</option></select></div>
        <div class="col-md-1"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter"></i></button></div>
    </div>
</form>

<div class="rs-card p-3">
<?php if (empty($items)): ?>
    <p class="text-muted small mb-0">No items found.</p>
<?php else: ?>
    <table class="table table-sm table-hover mb-2">
        <thead><tr><th>Name</th><th>Type</th><th>SKU</th><th>Unit</th><th class="text-end">Unit Price</th><th>Category</th><th>Active</th><th></th></tr></thead>
        <tbody>
            <?php foreach ($items as $it): ?>
                <tr>
                    <td><a href="<?= url('/ProductsServices/edit/' . (int) $it['item_id']) ?>" class="fw-semibold text-decoration-none"><?= e($it['name']) ?></a></td>
                    <td><span class="badge <?= $it['type'] === 'service' ? 'bg-info' : 'bg-warning text-dark' ?> text-uppercase"><?= e($it['type']) ?></span></td>
                    <td class="small text-muted"><?= e($it['sku'] ?? '—') ?></td>
                    <td class="small"><?= e($it['unit']) ?></td>
                    <td class="text-end"><?= number_format((float) $it['unit_price'], 2) ?></td>
                    <td class="small"><?= e($it['category'] ?? '—') ?></td>
                    <td><span class="badge <?= !empty($it['is_active']) ? 'bg-success' : 'bg-secondary' ?>"><?= !empty($it['is_active']) ? 'Yes' : 'No' ?></span></td>
                    <td class="text-end"><a href="<?= url('/ProductsServices/edit/' . (int) $it['item_id']) ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i></a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <?php $qs = array_filter(['q' => $filters['q'] ?? null, 'type' => $filters['type'] ?? null, 'category' => $filters['category'] ?? null, 'per_page' => $pager['per_page'] !== 25 ? $pager['per_page'] : null, 'sort' => ($sort ?? 'name_asc') !== 'name_asc' ? $sort : null]);
    $pageUrl = function (int $n) use ($qs) { $qs['page'] = $n; return url('/ProductsServices') . '?' . http_build_query($qs); };
    $window = []; $window[] = 1;
    for ($i = max(2, $pager['page'] - 2); $i <= min($pager['last'] - 1, $pager['page'] + 2); $i++) $window[] = $i;
    if ($pager['last'] > 1) $window[] = $pager['last'];
    $window = array_values(array_unique($window));
    ?>
    <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 pt-1">
        <div class="small text-muted">Showing <strong><?= (int) $pager['from'] ?></strong>–<strong><?= (int) $pager['to'] ?></strong> of <strong><?= (int) $pager['total'] ?></strong></div>
        <nav><ul class="pagination pagination-sm mb-0">
            <li class="page-item <?= $pager['page'] <= 1 ? 'disabled' : '' ?>"><a class="page-link" href="<?= $pager['page'] > 1 ? $pageUrl($pager['page'] - 1) : '#' ?>">«</a></li>
            <?php $prev = 0; foreach ($window as $p):
                if ($prev > 0 && $p > $prev + 1) echo '<li class="page-item disabled"><span class="page-link">…</span></li>';
                ?><li class="page-item <?= $p == $pager['page'] ? 'active' : '' ?>"><a class="page-link" href="<?= $pageUrl($p) ?>"><?= $p ?></a></li><?php
                $prev = $p;
            endforeach; ?>
            <li class="page-item <?= $pager['page'] >= $pager['last'] ? 'disabled' : '' ?>"><a class="page-link" href="<?= $pager['page'] < $pager['last'] ? $pageUrl($pager['page'] + 1) : '#' ?>">»</a></li>
        </ul></nav>
    </div>
<?php endif; ?>
</div>