<?php
require("php_con.php");

if(isset($_POST["query"])) {
    $search = mysqli_real_escape_string($connation, $_POST["query"]);
    $query = "SELECT product_id, product_name, product_barcode, price_sale 
              FROM product_details 
              LEFT JOIN price_details ON product_details.product_systemid = price_details.price_systemid
              WHERE product_name LIKE '%".$search."%' 
              OR product_barcode LIKE '%".$search."%' 
              ORDER BY price_id DESC
              LIMIT 5";
    $result = mysqli_query($connation, $query);
    
    $output = '<ul class="list-group">';
    if(mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_array($result)) {
            $output .= '<li class="list-group-item product_item" data-id="'.$row["product_id"].'" data-price="'.$row["price_sale"].'">'.$row["product_name"].' ('.$row["product_barcode"].')</li>';
        }
    } else {
        $output .= '<li class="list-group-item">Product Not Found</li>';
    }
    $output .= '</ul>';
    echo $output;
}
?>