<?php
include "../db.php";

if($_SERVER['REQUEST_METHOD'] !== 'POST'){
    echo json_encode(["error"=>"Invalid request"]);
    exit;
}

$name = $_POST['name'] ?? '';
$price = $_POST['price'] ?? '';
$description = $_POST['description'] ?? '';
$category = $_POST['category'] ?? '';

if(empty($name) || empty($price)){
    echo json_encode(["error"=>"Missing fields"]);
    exit;
}

$imageName = null;

if(!empty($_FILES['image']['name'])){
    $imageName = time().'_'.$_FILES['image']['name'];
    move_uploaded_file($_FILES['image']['tmp_name'], "uploads/".$imageName);
}

$stmt = $conn->prepare("INSERT INTO merch_items(name,price,description,image,category) VALUES(?,?,?,?,?)");
$stmt->bind_param("sdsss",$name,$price,$description,$imageName,$category);
$stmt->execute();

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