<?php
require_once 'conexion.php';

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

$sql = "
SELECT nombre, rut
FROM acompanantes
WHERE nombre LIKE CONCAT('%', ?, '%')
   OR rut LIKE CONCAT('%', ?, '%')
LIMIT 10
";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $term, $term);
$stmt->execute();
$res = $stmt->get_result();

$data = [];

while ($row = $res->fetch_assoc()) {
    $data[] = [
        "label" => $row["nombre"] . " - " . $row["rut"],
        "value" => $row["nombre"],
        "nombre" => $row["nombre"],
        "rut" => $row["rut"]
    ];
}

echo json_encode($data);