<?php
session_start();
header('Content-Type: application/json');
if (!isset($_SESSION['role_id']) || $_SESSION['role_id'] != 4) {
    http_response_code(403);
    echo json_encode([]);
    exit;
}

include('../../includes/config.php');

$term = trim($_GET['q'] ?? '');
if (strlen($term) < 1) {
    echo json_encode([]);
    exit;
}

$stmt = $conn->prepare("SELECT id, receipt_number FROM sales_transactions WHERE receipt_number LIKE CONCAT('%', ?, '%') LIMIT 10");
$stmt->bind_param('s', $term);
$stmt->execute();
$result = $stmt->get_result();

$suggestions = [];
while ($row = $result->fetch_assoc()) {
    $suggestions[] = ['id' => $row['id'], 'text' => $row['receipt_number']];
}
$stmt->close();

echo json_encode($suggestions);
?>
