<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Buscar Participante</title>
    <style>
        body { background: #c0c0c0; font-family: "Tahoma", sans-serif; font-size: 11px; margin: 10px; }
        .win-table { width: 100%; border-collapse: collapse; background: #fff; border: 2px inset #fff; }
        .win-table th { background: #d4d0c8; border: 1px solid #808080; padding: 4px; text-align: left; }
        .win-table td { border: 1px solid #f0f0f0; padding: 4px; cursor: pointer; }
        .win-table tr:hover td { background: #000080; color: #fff; }
    </style>
</head>
<body>
    <input type="text" id="txtBusca" placeholder="Digite nome ou código..." style="width:100%; margin-bottom:10px;" onkeyup="pesquisar()">
    
    <table class="win-table" id="tabelaResultados">
        <thead>
            <tr>
                <th width="80">Código</th>
                <th>Nome / Razão Social</th>
            </tr>
        </thead>
        <tbody>
            </tbody>
    </table>

    <script>
        function pesquisar() {
            let q = document.getElementById('txtBusca').value;
            if(q.length < 2) return;

            fetch('busca_participante_motor.php?q=' + q)
            .then(res => res.json())
            .then(data => {
                let html = '';
                data.forEach(item => {
                    html += `<tr onclick="selecionar('${item.reg_0150_cod_part}', '${item.reg_0150_nome}')">
                                <td>${item.reg_0150_cod_part}</td>
                                <td>${item.reg_0150_nome}</td>
                             </tr>`;
                });
                document.querySelector('#tabelaResultados tbody').innerHTML = html;
            });
        }

        function selecionar(codigo, nome) {
            // Preenche os dois campos na tela index.php
            window.opener.document.getElementById('val_nf_cab_cod_part').value = codigo;
            window.opener.document.getElementById('val_nf_cab_nome').value = nome;
            window.close();
        }
    </script>
</body>
</html>