<?php 
include("includes/db_config.php");

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Extract and validate POST data
    $id = $_POST['pt_id'];
    $item_name = $_POST['item_name'];
    $item_hsn = $_POST['item_hsn'];
    $item_code = $_POST['item_code'];
    $sale_price = $_POST['sale_price'];
    $tax_type = $_POST['tax_type'];
    $disc_sale = $_POST['disc_sale'];
    $disc_type = $_POST['disc_type'];
    $wholesale_price = $_POST['wholesale_price'];
    $wtax = $_POST['wtax'];
    $min_wqty = $_POST['min_wqty'];
    $purchase_price = $_POST['purchase_price'];
    $ptax = $_POST['ptax'];
    $tax_rate = $_POST['tax_rate'];
    $punit = $_POST['punit'];
    $sunit = $_POST['sunit'];
    $conversion_rate = $_POST['conversion_rate'];

    // Stock details
    $opening_stock = $_POST['opening_stock'];
    $at_price_unit = $_POST['at_price_unit'];
    $item_date = $_POST['item_date'];
    $min_stock_quatity = $_POST['min_stock_quatity'];
    $item_location = $_POST['item_location'];

    // Validation
    if (!$id || !$item_name) {
        echo "Missing required fields: ID or Item Name!";
        exit;
    }

    // Enable error reporting for debugging
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    // Start MySQL Transaction
    mysqli_begin_transaction($conn);

    try {
        // Update ah_item_details table
        $sql1 = "UPDATE ah_item_details SET 
                    item_name = '$item_name', 
                    item_hsn = '$item_hsn', 
                    item_code = '$item_code', 
                    sale_price = '$sale_price', 
                    tax_type = '$tax_type', 
                    disc_sale = '$disc_sale', 
                    disc_type = '$disc_type', 
                    wholesale_price = '$wholesale_price', 
                    min_wqty = '$min_wqty', 
                    purchase_price = '$purchase_price', 
                    tax_rate = '$tax_rate', 
                    punit = '$punit', 
                    sunit = '$sunit', 
                    conversion_rate = '$conversion_rate' 
                WHERE id = '$id'";

        if (!mysqli_query($conn, $sql1)) {
            throw new Exception("SQL1 Error: " . mysqli_error($conn));
        }

        // Update ah_item_tracking_details table
        $sql2 = "UPDATE ah_item_tracking_details SET 
                    opening_stock = '$opening_stock', 
                    at_price_unit = '$at_price_unit', 
                    item_date = '$item_date', 
                    min_stock_quatity = '$min_stock_quatity', 
                    item_location = '$item_location' 
                WHERE item_id = '$id'";

        if (!mysqli_query($conn, $sql2)) {
            throw new Exception("SQL2 Error: " . mysqli_error($conn));
        }

        // Commit transaction
        mysqli_commit($conn);
        echo "success";

    } catch (Exception $e) {
        // Rollback transaction on failure
        mysqli_rollback($conn);
        
        // Log and display error
        error_log("SQL Error: " . $e->getMessage(), 0);
        echo "Error: " . $e->getMessage();
    }

    // Close connection
    mysqli_close($conn);
}
?>
