<?php
session_start();
include_once('../includes/config.php');

header('Content-Type: application/json');

if (!isset($_SESSION['uid'])) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

if (!isset($_POST['id'])) {
    echo json_encode(['success' => false, 'message' => 'Item ID is required']);
    exit();
}

$itemId = (int)$_POST['id'];

// Check if item exists
$checkQuery = "SELECT ID, ItemName FROM tblitems WHERE ID = $itemId";
$checkResult = mysqli_query($con, $checkQuery);

if (!$checkResult || mysqli_num_rows($checkResult) === 0) {
    echo json_encode(['success' => false, 'message' => 'Item not found']);
    exit();
}

// Delete the item
$deleteQuery = "DELETE FROM tblitems WHERE ID = $itemId";

if (mysqli_query($con, $deleteQuery)) {
    echo json_encode(['success' => true, 'message' => 'Item deleted successfully']);
} else {
    echo json_encode(['success' => false, 'message' => 'Failed to delete item: ' . mysqli_error($con)]);
}
?>
