<?php
header('Content-Type: application/json; charset=UTF-8');
require_once 'conexion.php';

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

$sql = "
SELECT DISTINCT rut, nombre
FROM (
    SELECT rut, nombre FROM arrendatarios
    UNION
    SELECT rut, nombre FROM acompanantes
) t
WHERE rut LIKE ?
LIMIT 10
";

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

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

echo json_encode($data, JSON_UNESCAPED_UNICODE);
