<?php
include 'db.php';
include 'auth.php';
require_right('Manage SubCategories');


$categories = $conn->query("SELECT * FROM categories");
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $stmt = $conn->prepare("INSERT INTO sub_categories (category_id, name) VALUES (?, ?)");
    $stmt->bind_param("is", $_POST['category_id'], $_POST['name']);
    $stmt->execute();
    header("Location: category_list.php");
    exit;
}
include 'layout.php';
?>
<div class="page-wrapper">
    <div class="content">
        <h4>Add Sub Category</h4>
        <form method="POST">
            <select name="category_id" class="form-control mb-3" required>
                <option value="">Select Category</option>
                <?php while ($cat = $categories->fetch_assoc()): ?>
                    <option value="<?= $cat['id'] ?>"><?= $cat['name'] ?></option>
                <?php endwhile; ?>
            </select>
            <input type="text" name="name" placeholder="Sub Category Name" class="form-control mb-3" required>
            <div class="text-end"><button class="btn btn-primary">Save</button></div>
        </form>
    </div>
</div>
