<?php
include '../inc/db.php';
if ($conn->connect_error) die("DB Failed");

$calc_type   = $_POST['calc_type'] ?? null;
$sqft        = $_POST['sqft'] ?? null;
$lnft        = $_POST['lnft'] ?? null;
$material    = $_POST['material'] ?? null;
$stone_size  = $_POST['stone_size'] ?? null;
$tons        = $_POST['tons'] ?? null;
$token       = $_POST['token'] ?? null;
$product_id  = isset($_POST['product_id']) ? (int)$_POST['product_id'] : 0; // ✅ NEW

$stmt = $conn->prepare("
    INSERT INTO stone_calculations
    (calc_type, sqft, lnft, material, stone_size, tons, user_token, product_id)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?)
");

$stmt->bind_param(
    "sddssdsi",
    $calc_type,
    $sqft,
    $lnft,
    $material,
    $stone_size,
    $tons,
    $token,
    $product_id
);

$stmt->execute();
$stmt->close();
$conn->close();

echo "saved";
?>