<?php
require_once 'conexion.php';

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

$sql = "
SELECT id_arrendatario, nombre, rut, direccion, celular, email, patente_vehiculo
FROM arrendatarios
WHERE nombre LIKE CONCAT('%', ?, '%')
LIMIT 10
";

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

$data = [];

while ($row = $res->fetch_assoc()) {
    $data[] = [
        "label" => $row["nombre"],
        "value" => $row["nombre"],
        "id" => $row["id_arrendatario"],
        "rut" => $row["rut"],
        "direccion" => $row["direccion"],
        "celular" => $row["celular"],
        "email" => $row["email"],
        "patente_vehiculo" => $row["patente_vehiculo"]
    ];
}

echo json_encode($data);