<?php
include 'connect.php';
$item_id=$_POST['itemID'];
$speciName = $_POST['speciName'];
$speciCost = $_POST['speciCost'];
$speciPrice = $_POST['speciPrice'];
$speciQty = $_POST['speciQty'];
$itemImageName = $_FILES['speciImage']['name'];

$itemImageTmp = $_FILES['speciImage']['tmp_name'];
$itemImageExtension = pathinfo($itemImageName, PATHINFO_EXTENSION); // Get the file extension

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


if (move_uploaded_file($itemImageTmp, $destination)) {
    $sql = "INSERT INTO a_specifications (ItemID_FK,S_description,S_cost,S_price,S_quantity,S_Image) VALUES ('$item_id', '$speciName', $speciCost, $speciPrice, $speciQty,'$itemImage')";
    $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.'));
}
?>
