<?php
require_once '../db.php';


$name = $_POST['name'] ?? '';
$price = $_POST['price'] ?? '';
$description = $_POST['description'] ?? '';
$category_id = $_POST['category_id'] ?? '';
$alt = $_POST['alt'] ?? '';
$image = '';

if (!empty($_FILES['image']['name'])) {
  $target_dir = "../uploads/menu/";
  if (!file_exists($target_dir)) mkdir($target_dir, 0777, true);
  $target_file = $target_dir . basename($_FILES["image"]["name"]);
  move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
  $image = "uploads/menu/" . basename($_FILES["image"]["name"]);
}

$stmt = $conn->prepare("INSERT INTO menu_items (category_id, name, price, description, image, alt) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isdsss", $category_id, $name, $price, $description, $image, $alt);
$success = $stmt->execute();

echo json_encode(["success" => $success]);
