<?php
header('Content-Type: application/json');
require_once "../configuracao/conexao.php"; 

if ($mysqli->connect_error) {
    echo json_encode(['error' => 'Conexão falhou']);
    exit;
}

$termo = isset($_GET['q']) ? $_GET['q'] : '';
$likeTermo = "%$termo%";

// Selecionamos o reg_0150_cod_part explicitamente
$sql = "SELECT 
            reg_0150_cod_part, 
            reg_0150_nome, 
            reg_0150_cnpj, 
            reg_0150_cpf 
        FROM reg_0150 
        WHERE ativo = 's' 
        AND (reg_0150_nome LIKE ? 
             OR reg_0150_cnpj LIKE ? 
             OR reg_0150_cpf LIKE ? 
             OR reg_0150_cod_part LIKE ?)
        ORDER BY reg_0150_nome ASC 
        LIMIT 50";

$stmt = $mysqli->prepare($sql);
$stmt->bind_param("ssss", $likeTermo, $likeTermo, $likeTermo, $likeTermo);
$stmt->execute();
$result = $stmt->get_result();
echo json_encode($result->fetch_all(MYSQLI_ASSOC));