<?php
include 'db.php';

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

$id = $_POST['id'] ?? 0;
$product_id = $_POST['product_id'] ?? 0;
$material_id = $_POST['material_id'] ?? 0;
$calc_type = $_POST['calc_type'] ?? '';
$sqft_value = $_POST['sqft_value'] ?? null;
$lnft_value = $_POST['lnft_value'] ?? null;

/* VALIDATION */
if (!$id || !$product_id || !$material_id) {
    echo json_encode([
        "status" => "error",
        "message" => "Missing required fields"
    ]);
    exit;
}

/* UPDATE */
$update = mysqli_query($conn, "
UPDATE product_materials SET
    product_id = '$product_id',
    material_id = '$material_id',
    calc_type = '$calc_type',
    sqft_value = '$sqft_value',
    lnft_value = '$lnft_value'
WHERE id = '$id'
");

if ($update) {
    echo json_encode([
        "status" => "success",
        "message" => "Product material updated successfully"
    ]);
} else {
    echo json_encode([
        "status" => "error",
        "message" => mysqli_error($conn)
    ]);
}