<?php
include("db/cn.php");

// Check connection
if ($connection->connect_error) {
    die("Connection failed: " . $connection->connect_error);
}

// Function to fetch product details
function fetchProductDetails($connection, $product_name) {
    $sql = "SELECT * FROM purchases WHERE name = '$product_name'";
    echo $sql; // Debugging line to see the SQL query
    $result = $connection->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        return $row;
    } else {
        return false;
    }
}

// Get the product name from the query string
if (isset($_GET['product_name'])) {
    $product_name = $_GET['product_name'];
    $product_details = fetchProductDetails($connection, $product_name);

    if ($product_details) {
        echo json_encode($product_details);
        exit;
    } else {
        echo json_encode(["error" => "Product not found"]);
        exit;
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Product Information</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
</head>
<body>

<div class="container mt-5">
    <form>
        <div class="form-group">
            <label for="product_name">Product Name:</label>
            <input type="text" class="form-control typeahead" id="product_name" name="product_name" autocomplete="off">
        </div>
        <div class="form-group">
            <label for="price">Price:</label>
            <input type="text" class="form-control" id="price" name="price">
        </div>
        <div class="form-group">
            <label for="category">Category:</label>
            <input type="text" class="form-control" id="category" name="category">
        </div>
        <div class="form-group">
            <label for="company_category">Company Category:</label>
            <input type="text" class="form-control" id="company_category" name="company_category">
        </div>
        <div class="form-group">
            <label for="stock_qty">Stock Quantity:</label>
            <input type="text" class="form-control" id="stock_qty" name="stock_qty">
        </div>
        <div class="form-group">
            <label for="supplier_name">Supplier Name:</label>
            <input type="text" class="form-control" id="supplier_name" name="supplier_name">
        </div>
    </form>
</div>

<script>
$(document).ready(function(){
    // Live search with typeahead
    $('#product_name').typeahead({
        source: function(query, result){
            $.ajax({
                url: '<?php echo $_SERVER['PHP_SELF']; ?>',
                method: 'GET',
                data: {product_name:query},
                dataType: 'json',
                success: function(data){
                    result($.map(data, function(item){
                        return item.name;
                    }));
                }
            });
        },
        updater:function (item) {
            // Fetch and display product details
            $.ajax({
                url: '<?php echo $_SERVER['PHP_SELF']; ?>',
                method: 'GET',
                data: {product_name:item},
                dataType: 'json',
                success: function(data){
                    $('#price').val(data.price);
                    $('#category').val(data.category);
                    $('#company_category').val(data.company_category);
                    $('#stock_qty').val(data.stock_qty);
                    $('#supplier_name').val(data.supplier_name);
                }
            });
            return item;
        }
    });
});
</script>

</body>
</html>

<?php
// Close connection
$connection->close();
?>
