<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

include 'connect.php';

if (isset($_POST["query"])) {
    $search = mysqli_real_escape_string($conn, $_POST["query"]);
    $query = "SELECT product_number, pr_Name, pr_Price FROM products_table WHERE pr_Name LIKE '%$search%' LIMIT 5";
    $result = mysqli_query($conn, $query);
    
    if (mysqli_num_rows($result) > 0) {
        echo '<ul style="list-style:none;padding:0;margin:0;border:1px solid #ddd;max-height:200px;overflow:auto;">';
        while ($row = mysqli_fetch_assoc($result)) {
            echo '<li class="product-item" style="padding:8px;cursor:pointer;border-bottom:1px solid #ddd;" data-product_number="' . $row["product_number"] . '" data-price="' . $row["pr_Price"] . '">' . $row["pr_Name"] . '</li>';
        }
        echo '</ul>';
    } else {
        echo '<div class="product-item">Product not found</div>';
    }
}
?>
