<?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 = '%' . $conn->real_escape_string($_GET['term'] ?? '') . '%';

$stmt = $conn->prepare("SELECT id, description, amount FROM expenses WHERE description LIKE ? OR id LIKE ? ORDER BY id DESC LIMIT 10");
$stmt->bind_param('ss', $term, $term);
$stmt->execute();
$result = $stmt->get_result();

$suggestions = [];
while ($row = $result->fetch_assoc()) {
    $suggestions[] = [
        'id' => $row['id'],
        'label' => "Expense #{$row['id']} - {$row['description']} - MWK " . number_format($row['amount'], 2)
    ];
}

echo json_encode($suggestions);
?>
