<?php
session_start();
require_once 'includes/auth.php';
requireAdmin();
require_once 'config/db.php';
$success = '';
$error = '';
// Sticky form values
$form = [
'name' => '',
'description' => '',
'unit' => 'pcs',
'initial_stock' => '0',
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Collect & sanitize inputs
$form['name'] = trim($_POST['name'] ?? '');
$form['description'] = trim($_POST['description'] ?? '');
$form['unit'] = trim($_POST['unit'] ?? 'pcs');
$form['initial_stock'] = $_POST['initial_stock'] ?? '0';
$initial_stock = (int) $form['initial_stock'];
if ($form['name'] === '') {
$error = 'Product name is required.';
} elseif ($initial_stock < 0) {
$error = 'Initial stock cannot be negative.';
} else {
// Check for duplicate name
$dup = $pdo->prepare("SELECT id FROM products WHERE name = ?");
$dup->execute([$form['name']]);
if ($dup->fetch()) {
$error = "A product named \"{$form['name']}\" already exists.";
} else {
// Insert product
$ins = $pdo->prepare(
"INSERT INTO products (name, description, unit, current_stock) VALUES (?, ?, ?, ?)"
);
$ins->execute([$form['name'], $form['description'], $form['unit'], $initial_stock]);
$new_id = (int) $pdo->lastInsertId();
// Log initial stock as a transaction
if ($initial_stock > 0) {
$pdo->prepare(
"INSERT INTO stock_transactions (product_id, quantity, type, notes) VALUES (?, ?, 'added', 'Initial stock')"
)->execute([$new_id, $initial_stock]);
}
$success = "Product \"{$form['name']}\" added successfully!";
// Reset form after success
$form = ['name' => '', 'description' => '', 'unit' => 'pcs', 'initial_stock' => '0'];
}
}
}
$units = ['pcs', 'meter', 'roll', 'pack', 'box', 'set', 'unit'];
include 'includes/header.php';
?>
<div class="row mb-3">
<div class="col">
<h4 class="page-title"><i class="bi bi-plus-circle me-2 text-primary"></i>Add New Product</h4>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
<li class="breadcrumb-item active">Add Product</li>
</ol>
</nav>
</div>
</div>
<div class="row justify-content-center">
<div class="col-md-7 col-lg-6">
<div class="card">
<div class="card-header bg-primary text-white">
<i class="bi bi-box me-1"></i> Product Details
</div>
<div class="card-body">
<?php if ($success !== ''): ?>
<div class="alert alert-success d-flex align-items-center gap-2">
<i class="bi bi-check-circle-fill"></i>
<div>
<?= htmlspecialchars($success) ?>
<div class="mt-2 d-flex gap-2">
<a href="add_product.php" class="btn btn-sm btn-success">Add Another</a>
<a href="product_list.php" class="btn btn-sm btn-outline-secondary">View All Products</a>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($error !== ''): ?>
<div class="alert alert-danger d-flex align-items-center gap-2">
<i class="bi bi-exclamation-triangle-fill"></i>
<?= htmlspecialchars($error) ?>
</div>
<?php endif; ?>
<form method="POST" action="" novalidate>
<div class="mb-3">
<label for="name" class="form-label fw-semibold">
Product Name <span class="text-danger">*</span>
</label>
<input type="text"
class="form-control"
id="name"
name="name"
value="<?= htmlspecialchars($form['name']) ?>"
placeholder="e.g., ONU, Cable, TJ Box"
maxlength="100"
required>
</div>
<div class="mb-3">
<label for="description" class="form-label fw-semibold">Description</label>
<textarea class="form-control"
id="description"
name="description"
rows="3"
placeholder="Optional — short description of the product"><?= htmlspecialchars($form['description']) ?></textarea>
</div>
<div class="row g-3 mb-3">
<div class="col-6">
<label for="unit" class="form-label fw-semibold">Unit of Measure</label>
<select class="form-select" id="unit" name="unit">
<?php foreach ($units as $u): ?>
<option value="<?= $u ?>" <?= $form['unit'] === $u ? 'selected' : '' ?>>
<?= ucfirst($u) ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="col-6">
<label for="initial_stock" class="form-label fw-semibold">Initial Stock</label>
<input type="number"
class="form-control"
id="initial_stock"
name="initial_stock"
value="<?= (int) $form['initial_stock'] ?>"
min="0"
placeholder="0">
</div>
</div>
<div class="d-grid gap-2 mt-4">
<button type="submit" class="btn btn-primary fw-semibold">
<i class="bi bi-plus-circle me-1"></i> Add Product
</button>
<a href="product_list.php" class="btn btn-outline-secondary">Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include 'includes/footer.php'; ?>