38348-vm/products.php
2026-02-11 01:46:33 +00:00

101 lines
4.1 KiB
PHP

<?php
$page_title = "Products";
require_once 'includes/header.php';
$search = $_GET['search'] ?? '';
$where = "deleted_at IS NULL";
$params = [];
if ($search) {
$where .= " AND (name LIKE ? OR description LIKE ?)";
$params = ["%$search%", "%$search%"];
}
$stmt = db()->prepare("SELECT * FROM products WHERE $where ORDER BY name ASC");
$stmt->execute($params);
$products = $stmt->fetchAll();
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="fw-bold m-0">Products & Services</h2>
<?php if (in_array($user_role, ['Admin', 'Sales'])): ?>
<a href="product_form.php" class="btn btn-primary">
<i class="bi bi-plus-lg me-2"></i>Add Product
</a>
<?php endif; ?>
</div>
<div class="card mb-4">
<div class="card-body p-0">
<div class="p-3 border-bottom bg-light">
<form class="row g-2" method="GET">
<div class="col-auto">
<div class="input-group input-group-sm">
<span class="input-group-text bg-white border-end-0"><i class="bi bi-search"></i></span>
<input type="text" name="search" class="form-control border-start-0" placeholder="Search products..." value="<?= e($search) ?>">
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-sm btn-outline-secondary">Search</button>
</div>
<?php if ($search): ?>
<div class="col-auto">
<a href="products.php" class="btn btn-sm btn-link text-decoration-none">Clear</a>
</div>
<?php endif; ?>
</form>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead>
<tr>
<th class="ps-4">Product/Service</th>
<th>Price</th>
<th>Created</th>
<th class="text-end pe-4">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($products)): ?>
<tr>
<td colspan="4" class="text-center py-5 text-muted">
<i class="bi bi-box-seam fs-1 d-block mb-3"></i>
No products found.
</td>
</tr>
<?php endif; ?>
<?php foreach ($products as $p): ?>
<tr>
<td class="ps-4">
<div class="fw-bold"><?= e($p['name']) ?></div>
<div class="text-muted small"><?= e($p['description']) ?></div>
</td>
<td><span class="fw-medium text-primary"><?= format_currency($p['price']) ?></span></td>
<td class="text-muted"><?= date('M d, Y', strtotime($p['created_at'])) ?></td>
<td class="text-end pe-4">
<?php if (in_array($user_role, ['Admin', 'Sales'])): ?>
<a href="product_form.php?id=<?= $p['id'] ?>" class="btn btn-sm btn-outline-secondary py-1 px-2" title="Edit"><i class="bi bi-pencil"></i></a>
<?php endif; ?>
<?php if ($user_role === 'Admin'): ?>
<button type="button" class="btn btn-sm btn-outline-danger py-1 px-2 ms-1" title="Delete" onclick="deleteProduct(<?= $p['id'] ?>)"><i class="bi bi-trash"></i></button>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
function deleteProduct(id) {
if (confirm('Are you sure you want to delete this product?')) {
window.location.href = 'product_delete.php?id=' + id;
}
}
</script>
<?php require_once 'includes/footer.php'; ?>