<?php
// Include your database connection configuration
include('db_config.php');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Content-Type");
header('Content-Type: application/json');
// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Retrieve data from the POST request
    $json_data = file_get_contents('php://input');
    
    // Decode the JSON data into an associative array
    $data = json_decode($json_data, true);

    // Extract data fields
    $hsn=$data['product_hsn'];
    $billId = $data['bill_id'];
    $productName = $data['product_name'];
    $serialNoOrIMEI = $data['serial_no'];
    $amount = $data['amount'];
    $tax = $data['tax'];
    $stmt = $pdo->prepare("INSERT INTO billing_products (bill_id, product_hsn, product_name, serial_no_or_imei, amount, tax) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->execute([$billId, $hsn, $productName, $serialNoOrIMEI, $amount, $tax]);

    // Check if the insertion was successful
    if ($stmt->rowCount() > 0) {
        // Product added successfully
        echo json_encode(["message" => "Product added successfully"]);
    } else {
        // Failed to add product
        echo json_encode(["error" => "Failed to add product"]);
    }
} else {
    // Invalid request method
    http_response_code(200); // Method Not Allowed
    echo json_encode(["error" => "Invalid request method"]);
}
?>
