39094-vm/product.php
Flatlogic Bot f6d1ea8ef2 test
2026-03-10 15:19:21 +00:00

105 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
require_once __DIR__ . '/includes/layout.php';
ensure_tables();
$productId = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$productId = (int)($_POST['id'] ?? 0);
$status = trim($_POST['status'] ?? 'active');
$stock = (int)($_POST['stock'] ?? 0);
if ($productId) {
$stmt = db()->prepare(
"UPDATE products SET status = :status, stock = :stock WHERE id = :id"
);
$stmt->bindValue(':status', $status);
$stmt->bindValue(':stock', $stock, PDO::PARAM_INT);
$stmt->bindValue(':id', $productId, PDO::PARAM_INT);
$stmt->execute();
flash_set('success', 'Product updated.');
} else {
flash_set('error', 'Product not found.');
}
header('Location: /product.php?id=' . $productId);
exit;
}
$product = $productId ? get_product($productId) : null;
if (!$product) {
render_header('Product not found');
?>
<div class="content-card">
<h4 class="mb-2">Product not found</h4>
<p class="text-muted mb-3">The requested product does not exist.</p>
<a class="btn btn-dark" href="/products.php">Back to catalog</a>
</div>
<?php
render_footer();
exit;
}
render_header($product['name'], 'Product detail with inventory status.');
?>
<section class="row g-4">
<div class="col-12 col-lg-7">
<div class="content-card h-100">
<p class="text-muted small mb-1">SKU <?= h($product['sku']) ?></p>
<h3 class="mb-2"><?= h($product['name']) ?></h3>
<p class="text-muted mb-4">Store: <?= h($product['store_name']) ?></p>
<div class="row g-3">
<div class="col-6">
<div class="mini-card">
<p class="text-muted small mb-1">Price</p>
<p class="mb-0"><?= h($product['currency']) ?> <?= h(number_format((float)$product['price'], 2)) ?></p>
</div>
</div>
<div class="col-6">
<div class="mini-card">
<p class="text-muted small mb-1">Stock on hand</p>
<p class="mb-0"><?= h((string)$product['stock']) ?></p>
</div>
</div>
</div>
<div class="mini-card mt-4">
<p class="text-muted small mb-1">Status</p>
<span class="badge bg-light text-dark border"><?= h(ucfirst($product['status'])) ?></span>
</div>
</div>
</div>
<div class="col-12 col-lg-5">
<div class="content-card h-100">
<h4 class="mb-3">Update inventory</h4>
<form method="post" class="vstack gap-3">
<input type="hidden" name="id" value="<?= h((string)$product['id']) ?>">
<div>
<label class="form-label">Status</label>
<select name="status" class="form-select">
<?php foreach (['active', 'paused', 'archived'] as $state): ?>
<option value="<?= h($state) ?>" <?= $product['status'] === $state ? 'selected' : '' ?>>
<?= h(ucfirst($state)) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div>
<label class="form-label">Stock</label>
<input type="number" name="stock" class="form-control" value="<?= h((string)$product['stock']) ?>" min="0">
</div>
<button class="btn btn-dark w-100" type="submit">Save updates</button>
</form>
<p class="text-muted small mt-3 mb-0">Use this for quick stock corrections before orders are fulfilled.</p>
</div>
</div>
</section>
<?php render_footer(); ?>