36037-vm/views/admin_products.php
Flatlogic Bot b6296eed55 Version 1
2025-11-22 17:18:03 +00:00

119 lines
5.2 KiB
PHP

<?php
// Fetch products from the database
try {
$pdo = db();
$stmt = $pdo->query("SELECT * FROM products ORDER BY created_at DESC");
$products = $stmt->fetchAll();
} catch (PDOException $e) {
echo "<div class='alert alert-danger'>Database error: " . htmlspecialchars($e->getMessage()) . "</div>";
$products = [];
}
?>
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h3">Manage Products</h1>
</div>
<?php
if (isset($_SESSION['success_message'])) {
echo '<div class="alert alert-success alert-dismissible fade show" role="alert">' . htmlspecialchars($_SESSION['success_message']) . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
unset($_SESSION['success_message']);
}
if (isset($_SESSION['error_message'])) {
echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">' . htmlspecialchars($_SESSION['error_message']) . '<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>';
unset($_SESSION['error_message']);
}
?>
<!-- Add Product Form -->
<div class="card shadow-sm mb-4">
<div class="card-header">
<h2 class="h5 mb-0">Add New Product</h2>
</div>
<div class="card-body">
<?php include __DIR__ . '/partials/product_form.php'; ?>
</div>
</div>
<!-- Product List -->
<div class="card shadow-sm">
<div class="card-header">
<h2 class="h5 mb-0">Existing Products</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead class="table-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Barcode</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($products)): ?>
<tr>
<td colspan="5" class="text-center">No products found.</td>
</tr>
<?php else: ?>
<?php foreach ($products as $product): ?>
<tr id="product-row-<?= $product['id'] ?>">
<td><?= htmlspecialchars($product['id']) ?></td>
<td><?= htmlspecialchars($product['name']) ?></td>
<td><?= htmlspecialchars(number_format($product['price'], 2)) ?></td>
<td><?= htmlspecialchars($product['barcode'] ?? 'N/A') ?></td>
<td>
<a href="dashboard.php?page=admin_edit_product&id=<?= $product['id'] ?>" class="btn btn-sm btn-outline-primary">
<i class="bi bi-pencil-square"></i> Edit
</a>
<button class="btn btn-sm btn-outline-danger delete-product-btn" data-id="<?= $product['id'] ?>">
<i class="bi bi-trash"></i> Delete
</button>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const deleteButtons = document.querySelectorAll('.delete-product-btn');
deleteButtons.forEach(button => {
button.addEventListener('click', function () {
const productId = this.getAttribute('data-id');
if (confirm('Are you sure you want to delete this product?')) {
fetch('/api/delete_product.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'id=' + encodeURIComponent(productId)
})
.then(response => response.json())
.then(data => {
if (data.success) {
const row = document.getElementById('product-row-' + productId);
row.remove();
// Optionally, show a success message
const alertContainer = document.querySelector('.d-flex.justify-content-between');
const successAlert = `<div class="alert alert-success alert-dismissible fade show" role="alert">${data.message}<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button></div>`;
alertContainer.insertAdjacentHTML('afterend', successAlert);
} else {
alert('Error: ' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while deleting the product.');
});
}
});
});
});
</script>