<?php
session_start();
if (!isset($_SESSION['id'])) { header("Location: ../index/index.php"); exit(); }
include_once('../configuracao/conexao.php');

// Filtros de busca
$nome = $_GET['nome'] ?? '';
$doc  = $_GET['doc'] ?? '';

// SQL buscando da reg_c141 (onde tens o código e nome do participante)
$sql = "SELECT * FROM reg_c141 WHERE ativo = 's'";

if ($nome != '') { $sql .= " AND reg_c141_nome LIKE '%".$mysqli->real_escape_string($nome)."%'"; }
if ($doc != '')  { $sql .= " AND reg_c141_c140_origem LIKE '%".$mysqli->real_escape_string($doc)."%'"; }

$sql .= " ORDER BY reg_c141_dt_vcto DESC LIMIT 50";
$res = $mysqli->query($sql);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <style>
        body { background: #d4d0c8; font-family: "Tahoma", sans-serif; font-size: 11px; margin: 10px; }
        .win-border { border: 2px solid; border-color: #fff #808080 #808080 #fff; background: #d4d0c8; padding: 2px; }
        .title-bar { background: #000080; color: white; padding: 3px 6px; font-weight: bold; }
        
        /* Painel de Filtros */
        .filter-pane { border: 2px inset #fff; padding: 8px; margin-bottom: 5px; display: flex; gap: 10px; align-items: center; }
        input { border: 2px inset #fff; padding: 3px; font-size: 11px; font-family: "Tahoma"; }
        .btn-win { background: #d4d0c8; border: 2px solid; border-color: #fff #808080 #808080 #fff; cursor: pointer; padding: 2px 10px; font-size: 11px; }
        .btn-win:active { border-color: #808080 #fff #fff #808080; }

        /* Tabela Estilo Windows 2000 */
        .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; position: sticky; top: 0; }
        .win-table td { border: 1px solid #d4d0c8; padding: 4px; cursor: pointer; white-space: nowrap; }
        .win-table tr:hover td { background: #000080; color: #fff; }

        .status-a { color: red; font-weight: bold; }
        .status-f { color: green; font-weight: bold; }
    </style>
</head>
<body>

<div class="win-border">
    <div class="title-bar">Localizar Título para Baixa de Caixa</div>
    
    <form method="GET" class="filter-pane">
        <label>Nome:</label>
        <input type="text" name="nome" value="<?= htmlspecialchars($nome) ?>" style="width:180px;">
        <label>Documento:</label>
        <input type="text" name="doc" value="<?= htmlspecialchars($doc) ?>" style="width:80px;">
        <button type="submit" class="btn-win">Pesquisar</button>
        <button type="button" class="btn-win" onclick="location.href='busca_titulo.php'">Todos</button>
    </form>

    <div style="height: 380px; overflow-y: scroll; background: #fff; border: 2px inset #fff;">
        <table class="win-table">
            <thead>
                <tr>
                    <th>Doc</th>
                    <th>Parc</th>
                    <th>Vencimento</th>
                    <th>Valor</th>
                    <th>Status</th>
                    <th>Participante</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($r = $res->fetch_assoc()): 
                    $st = strtoupper($r['reg_c141_status']);
                ?>
                <tr onclick="enviar('<?= $r['reg_c141_c140_origem'] ?>', '<?= $r['reg_c141_num_parc'] ?>', '<?= $r['reg_c141_vl_parc'] ?>', '<?= $st ?>', '<?= $r['reg_c141_cod_part'] ?>', '<?= $r['reg_c141_nome'] ?>')">
                    <td><?= $r['reg_c141_c140_origem'] ?></td>
                    <td align="center"><?= $r['reg_c141_num_parc'] ?></td>
                    <td><?= date('d/m/Y', strtotime($r['reg_c141_dt_vcto'])) ?></td>
                    <td align="right"><?= number_format($r['reg_c141_vl_parc'], 2, ',', '.') ?></td>
                    <td align="center" class="<?= ($st == 'A' ? 'status-a' : 'status-f') ?>"><?= $st ?></td>
                    <td><?= $r['reg_c141_cod_part'] ?> - <?= $r['reg_c141_nome'] ?></td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function enviar(tit, parc, vl, status, cod, nome) {
    if (window.opener && !window.opener.closed) {
        // Envia exatamente os 6 campos que o teu index.php espera receber
        window.opener.definirTitulo(tit, parc, vl, status, cod, nome);
    }
    window.close();
}
</script>

</body>
</html>