<?php
include 'connect.php';

$itemName = mysqli_real_escape_string($con, $_POST['itemName']);
$itemDesc = mysqli_real_escape_string($con, $_POST['itemDesc']);

$itemImageName = $_FILES['itemImage']['name'];
$catID = $_POST['catID'];
$show = $_POST['show'];
$out = $_POST['out'];
$promotion=$_POST['promotion'];
$itemImageTmp = $_FILES['itemImage']['tmp_name'];
$itemImageExtension = pathinfo($itemImageName, PATHINFO_EXTENSION); // Get the file extension

$folderName = "../images/Items/";
$newItemImageName = uniqid() . '.' . $itemImageExtension;
$destination = $folderName . $newItemImageName;
$itemImage = "images/Items/" . $newItemImageName;


if (move_uploaded_file($itemImageTmp, $destination)) {
    $sql = "INSERT INTO a_items (ItemName,ItemDescription,ItemImage,ItemShow_status,ItemOutOfStock_status,Promotion,CategoryID_FK) VALUES ('$itemName', '$itemDesc','$itemImage', $show, $out,$promotion, $catID)";
    $result = mysqli_query($con,$sql);
    if ($result) {
        echo json_encode(array('message' => 'Item added successfully.'));
    } else {
        echo json_encode(array('error' => mysqli_error($con)));
    }
} else {
    echo json_encode(array('error' => 'Error uploading the file.'));
}
?>
