<?php
include('shikanisha.kts.php');

$tot_id = isset($_POST['ttot_id']) ? intval($_POST['ttot_id']) : 0;
$name = isset($_POST['tname']) ? $_POST['tname'] : '';
$price = isset($_POST['tprice']) ? floatval($_POST['tprice']) : 0;

if ($tot_id > 0 && !empty($name) && $price >= 0) {
    $query = "UPDATE tots SET name = ?, price = ? WHERE tot_id = ?";
    if ($stmt = $conn->prepare($query)) {
        $stmt->bind_param('sdi', $name, $price, $tot_id);
        if ($stmt->execute()) {
            $response = ['status' => 'success', 'message' => 'Tot updated successfully.'];
        } else {
            $response = ['status' => 'error', 'message' => 'Failed to update tot.'];
        }
        $stmt->close();
    } else {
        $response = ['status' => 'error', 'message' => 'Failed to prepare the SQL query.'];
    }
} else {
    $response = ['status' => 'error', 'message' => 'Invalid input data.'];
}

$conn->close();
header('Content-Type: application/json');
echo json_encode($response);
?>
