<?php
include("../config/db.php");
if (!isset($_SESSION['admin'])) header("Location: login.php");

$cats = $conn->query("SELECT * FROM categories");

if (isset($_POST['save'])) {

    $imageName = time() . "_" . $_FILES['image']['name'];
    move_uploaded_file(
        $_FILES['image']['tmp_name'],
        "../uploads/menu/" . $imageName
    );

    $stmt = $conn->prepare("
        INSERT INTO menu_items 
        (category_id, item_name, price, type, image)
        VALUES (?, ?, ?, ?, ?)
    ");
    $stmt->bind_param(
        "isdss",
        $_POST['category'],
        $_POST['item'],
        $_POST['price'],
        $_POST['type'],
        $imageName
    );
    $stmt->execute();

    header("Location: dashboard.php");
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Add Menu Item</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css" rel="stylesheet">

</head>
<body class="bg-light">

<div class="container mt-5">
    <div class="card shadow">
        <div class="card-header bg-dark text-light">
            <h4>Add Menu Item</h4>
        </div>

        <div class="card-body">
            <form method="post" enctype="multipart/form-data">

                <div class="mb-3">
                    <label>Category</label>
                    <select name="category" class="form-select" required>
                        <?php while($c = $cats->fetch_assoc()): ?>
                            <option value="<?= $c['id'] ?>"><?= $c['name'] ?></option>
                        <?php endwhile; ?>
                    </select>
                </div>

                <div class="mb-3">
                    <label>Item Name</label>
                    <input name="item" class="form-control" required>
                </div>

                <div class="mb-3">
                    <label>Price</label>
                    <input name="price" class="form-control" required>
                </div>

                <div class="mb-3">
                    <label>Type</label>
                    <select name="type" class="form-select">
                        <option value="veg">🥬 Veg</option>
                        <option value="nonveg">🍗 Non-Veg</option>
                    </select>
                </div>

                <div class="mb-3">
                    <label>Item Image</label>
                    <input type="file" name="image" class="form-control" required>
                </div>

                <button name="save" class="btn btn-success">
                    <i class="fa fa-save"></i> Save Item
                </button>

                <a href="dashboard.php" class="btn btn-secondary">Back</a>
            </form>
        </div>
    </div>
</div>

</body>
</html>
