<?php
include 'config.php';

$term = $_GET['term'] ?? '';

// Search products by name (case insensitive)
$query = "SELECT p.idproduk, p.kode_produk, p.nama_produk, p.harga_jual, p.stock, k.nama_kategori 
          FROM produk p 
          JOIN kategori k ON p.idkategori = k.idkategori 
          WHERE p.nama_produk LIKE '%$term%' 
          OR p.kode_produk LIKE '%$term%'
          ORDER BY p.nama_produk ASC";
$result = mysqli_query($conn, $query);

$products = array();
while($row = mysqli_fetch_assoc($result)) {
    $row['harga_jual'] = number_format($row['harga_jual'], 0, ',', '.');
    $products[] = $row;
}

echo json_encode(array('products' => $products));
?>