<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <title>Busca de Participante - Morfo Engine</title>
    <style>
        body { background: #d4d0c8; font-family: "Tahoma", sans-serif; font-size: 11px; margin: 8px; }
        .window { border: 2px solid; border-color: #fff #808080 #808080 #fff; background: #c0c0c0; padding: 2px; }
        .title-bar { background: #000080; color: white; padding: 3px 6px; font-weight: bold; }
        .content { padding: 10px; }
        input { border: 2px inset #fff; padding: 3px; background: #fff; width: 300px; outline: none; }
        .btn-win { background: #d4d0c8; border: 2px solid; border-color: #fff #808080 #808080 #fff; font-weight: bold; cursor: pointer; padding: 2px 10px; }
        .table-container { background: #fff; border: 2px inset #fff; margin-top: 10px; height: 300px; overflow-y: scroll; }
        table { width: 100%; border-collapse: collapse; }
        th { background: #d4d0c8; border: 1px solid #808080; position: sticky; top: 0; text-align: left; padding: 4px; }
        td { border-bottom: 1px solid #f0f0f0; padding: 4px; cursor: pointer; }
        tr:hover td { background: #000080; color: #fff; }
    </style>
</head>
<body>

<div class="window">
    <div class="title-bar">Localizar Participante (reg_0150)</div>
    <div class="content">
        <input type="text" id="campobusca" placeholder="Digite nome, CPF/CNPJ ou Código..." onkeyup="if(event.key==='Enter') pesquisar()">
        <button class="btn-win" onclick="pesquisar()">BUSCAR</button>

        <div class="table-container">
            <table>
                <thead>
                    <tr>
                        <th width="80">Código</th>
                        <th>Nome / Razão Social</th>
                        <th width="120">CPF / CNPJ</th>
                    </tr>
                </thead>
                <tbody id="resultadotabela"></tbody>
            </table>
        </div>
    </div>
</div>

<script>
    async function pesquisar() {
        const query = document.getElementById('campobusca').value;
        const tbody = document.getElementById('resultadotabela');
        tbody.innerHTML = '<tr><td colspan="3">Procurando...</td></tr>';

        try {
            const response = await fetch(`busca_participante_motor.php?q=${encodeURIComponent(query)}`);
            const dados = await response.json();
            tbody.innerHTML = '';

            dados.forEach(item => {
                const tr = document.createElement('tr');
                const doc = item.reg_0150_cnpj || item.reg_0150_cpf || '';
                // Passamos o reg_0150_cod_part que é o campo correto
                tr.onclick = () => selecionar(item.reg_0150_cod_part, item.reg_0150_nome);
                tr.innerHTML = `<td>${item.reg_0150_cod_part}</td><td>${item.reg_0150_nome}</td><td>${doc}</td>`;
                tbody.appendChild(tr);
            });
        } catch (e) {
            tbody.innerHTML = '<tr><td colspan="3" style="color:red">Erro na consulta.</td></tr>';
        }
    }

    function selecionar(id, nome) {
        if (window.opener && !window.opener.closed) {
            // Preenche os campos no index.php
            window.opener.document.getElementById('val_id_participante').value = id;
            window.opener.document.getElementById('val_nome_participante').value = nome;
            window.close();
        }
    }
</script>
</body>
</html>