<div class="d-flex justify-content-between align-items-center mb-3">
    <h5 class="fw-bold mb-0"><i class="fas fa-boxes-stacked me-2 text-primary"></i>Spare Parts</h5>
    <?php if ($canManage): ?>
        <a class="btn btn-sm btn-primary" href="<?= url('/Parts/create') ?>"><i class="fas fa-plus me-1"></i>New Part</a>
    <?php endif; ?>
</div>

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

<div class="kpi-grid mb-3">
    <div class="rs-card p-2 text-center"><div class="text-muted small">Total SKUs</div><div class="fw-bold fs-5"><?= (int) $counts['total'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Active</div><div class="fw-bold fs-5"><?= (int) $counts['active'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Below Min</div><div class="fw-bold fs-5 text-warning"><?= (int) $counts['low_stock'] ?></div></div>
    <div class="rs-card p-2 text-center"><div class="text-muted small">Out of Stock</div><div class="fw-bold fs-5 text-danger"><?= (int) $counts['out_of_stock'] ?></div></div>
</div>

<form method="get" action="<?= url('/Parts') ?>" class="rs-card p-2 mb-3">
    <div class="row g-2 align-items-end">
        <div class="col-md-4"><input type="search" name="q" class="form-control form-control-sm" placeholder="Search SKU / name / supplier…" value="<?= e($filters['q'] ?? '') ?>"></div>
        <div class="col-md-3"><input type="text" name="category" class="form-control form-control-sm" placeholder="Category" value="<?= e($filters['category'] ?? '') ?>"></div>
        <div class="col-md-3 d-flex align-items-end">
            <div class="form-check">
                <input type="checkbox" class="form-check-input" name="low_only" value="1" id="lowOnly" <?= !empty($filters['low_only']) ? 'checked' : '' ?>>
                <label for="lowOnly" class="form-check-label">Below min only</label>
            </div>
        </div>
        <div class="col-md-2"><button type="submit" class="btn btn-sm btn-outline-primary w-100"><i class="fas fa-filter me-1"></i>Filter</button></div>
    </div>
</form>

<?php if (empty($parts)): ?>
    <div class="rs-card p-4 text-center text-muted">No parts match your filter.</div>
<?php else: ?>
    <div class="rs-card p-0">
        <table class="table table-sm table-hover mb-0 align-middle">
            <thead class="table-light"><tr>
                <th>SKU</th><th>Name</th><th>Category</th><th class="text-end">On hand</th>
                <th class="text-end">Min</th><th class="text-end">Unit cost</th>
                <th>Location</th><th class="text-center">Status</th><th></th>
            </tr></thead>
            <tbody>
                <?php foreach ($parts as $p):
                    $low = !empty($p['is_low']);
                    $zero = (float) $p['on_hand'] == 0.0;
                    if ($zero) { $b = ['bg-danger', 'OUT']; }
                    elseif ($low) { $b = ['bg-warning text-dark', 'LOW']; }
                    else { $b = ['bg-success', 'OK']; }
                ?>
                    <tr>
                        <td><code class="small"><?= e($p['sku']) ?></code></td>
                        <td>
                            <a href="<?= url('/Parts/show/' . (int) $p['part_id']) ?>"><strong><?= e($p['name']) ?></strong></a>
                            <?php if (!$p['is_active']): ?><span class="badge bg-secondary ms-1">inactive</span><?php endif; ?>
                        </td>
                        <td class="small text-muted"><?= e($p['category'] ?? '—') ?></td>
                        <td class="text-end"><strong><?= rtrim(rtrim(number_format((float) $p['on_hand'], 3), '0'), '.') ?></strong> <span class="small text-muted"><?= e($p['unit']) ?></span></td>
                        <td class="text-end small text-muted"><?= rtrim(rtrim(number_format((float) $p['min_stock'], 3), '0'), '.') ?></td>
                        <td class="text-end small"><?= number_format((float) $p['unit_cost'], 2) ?></td>
                        <td class="small text-muted"><?= e($p['location'] ?? '—') ?></td>
                        <td class="text-center"><span class="badge <?= $b[0] ?>" style="font-size:.6rem;"><?= $b[1] ?></span></td>
                        <td class="text-end"><a class="btn btn-sm btn-outline-primary" href="<?= url('/Parts/show/' . (int) $p['part_id']) ?>"><i class="fas fa-eye"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>
