<?php
require_once __DIR__ . '/../../includes/auth.php';
requireLogin();
requirePermission('inventory.manage');

require_once __DIR__ . '/../../config/database.php';

$error = "";
$success = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $category = $_POST['category'] ?? '';
    $brand    = trim($_POST['brand'] ?? '');
    $model    = trim($_POST['model'] ?? '');
    $sku      = trim($_POST['sku'] ?? '');
    $desc     = trim($_POST['description'] ?? '');

    if (!$category || !$brand || !$model) {
        $error = "Category, Brand and Model are required.";
    } else {

        $stmt = $pdo->prepare("
            INSERT INTO inventory_products
            (category, brand, model, sku, description)
            VALUES (?, ?, ?, ?, ?)
        ");

        $stmt->execute([$category, $brand, $model, $sku, $desc]);

        $success = "Product created successfully.";
    }
}

$breadcrumb = "Add Product";
require_once __DIR__ . '/../../includes/layout.php';
?>

<div class="container">

    <div class="page-header">
        <h2>Add Product</h2>
        <p class="page-subtitle">
            Create new inventory product model.
        </p>
    </div>

    <div class="topbar">
        <a class="btn secondary" href="products.php">← Back to Products</a>
    </div>

    <?php if ($error): ?>
        <div class="alert error"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <?php if ($success): ?>
        <div class="alert success"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>

    <form method="POST">

        <div class="form-group">
            <label>Category</label>
            <select name="category" required>
                <option value="">Select</option>
                <option value="WATER">Water</option>
                <option value="ELECTRICITY">Electricity</option>
            </select>
        </div>

        <div class="form-group">
            <label>Brand</label>
            <input type="text" name="brand" required>
        </div>

        <div class="form-group">
            <label>Model</label>
            <input type="text" name="model" required>
        </div>

        <div class="form-group">
            <label>SKU (optional)</label>
            <input type="text" name="sku">
        </div>

        <div class="form-group">
            <label>Description (optional)</label>
            <textarea name="description"></textarea>
        </div>

        <button type="submit" class="btn">Save Product</button>

    </form>

</div>

<?php require_once __DIR__ . '/../../includes/layout_footer.php'; ?>