<?php
include 'includes/config.php';


// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve and sanitize form inputs
    $actualName = trim($_POST['actual_name']);
    $shortName = trim($_POST['short_name']);
    $allowDecimal = (int) $_POST['allow_decimal'];
    $addMultiple = isset($_POST['add_multiple']) ? 1 : 0;

    // Check required fields
    if (empty($actualName) || empty($shortName) || !in_array($allowDecimal, [0, 1])) {
        die("Please fill all required fields.");
    }

    // Prepare the SQL query to insert data
    $sql = "INSERT INTO `units` (`business_id`, `actual_name`, `short_name`, `allow_decimal`) VALUES (:business_id, :actual_name, :short_name, :allow_decimal)";
    $stmt = $conn->prepare($sql);

    // Bind parameters
    $businessId = 3; // Fixed value for business_id
    $stmt->bindParam(':business_id', $businessId, PDO::PARAM_INT);
    $stmt->bindParam(':actual_name', $actualName, PDO::PARAM_STR);
    $stmt->bindParam(':short_name', $shortName, PDO::PARAM_STR);
    $stmt->bindParam(':allow_decimal', $allowDecimal, PDO::PARAM_INT);

    try {
        // Execute the query
        $stmt->execute();
        echo "<script>alert('Unit successfully added!');</script>";
        echo "<script>window.location.href='add_products.php'</script>";
       
    } catch (PDOException $e) {
        die("Insertion failed: " . $e->getMessage());
    }
}
?>
