@@ -733,49 +733,61 @@ <h3>Erro ao carregar candidatos</h3>
733733}
734734
735735// ======================
736- // MONITOR DE RESULTADOS EM TEMPO REAL - VERSÃO CORRIGIDA
736+ // MONITOR DE RESULTADOS - VERSÃO CORRIGIDA (FILTRA POR ELEIÇÃO)
737737// ======================
738738
739739async function carregarResultados ( ) {
740740 try {
741741 // Buscar TODOS os votos (incluindo pendentes)
742742 const todosVotos = await dbVotes . allDocs ( { include_docs : true } ) ;
743743
744- // ✅ CORREÇÃO: Agora conta TODOS os votos, não só os com CID
745- const votosComCandidato = todosVotos . rows . filter ( row =>
744+ // ✅ CORREÇÃO CRÍTICA: Filtrar apenas votos da ELEIÇÃO ATUAL
745+ const votosDestaEleicao = todosVotos . rows . filter ( row =>
746+ row . doc . eleicao_id === ELEICAO_ATUAL_ID
747+ ) ;
748+
749+ console . log ( `🗳️ Votos desta eleição: ${ votosDestaEleicao . length } de ${ todosVotos . rows . length } totais` ) ;
750+
751+ // ✅ CORREÇÃO: Agora conta apenas votos desta eleição
752+ const votosComCandidato = votosDestaEleicao . filter ( row =>
746753 row . doc . candidato_id && row . doc . candidato_id !== 'branco'
747754 ) ;
748755
749- const votosBrancos = todosVotos . rows . filter ( row =>
756+ const votosBrancos = votosDestaEleicao . filter ( row =>
750757 row . doc . candidato_id === 'branco'
751758 ) ;
752759
753- // ✅ CORREÇÃO: Separar votos por status
754- const votosEnviados = todosVotos . rows . filter ( row => row . doc . cid_pinata && ! row . doc . erro ) . length ;
755- const votosPendentes = todosVotos . rows . filter ( row => row . doc . erro === "sem_conexao_backend" ) . length ;
760+ // ✅ CORREÇÃO: Separar votos por status (APENAS DESTA ELEIÇÃO)
761+ const votosEnviados = votosDestaEleicao . filter ( row => row . doc . cid_pinata && ! row . doc . erro ) . length ;
762+ const votosPendentes = votosDestaEleicao . filter ( row => row . doc . erro === "sem_conexao_backend" ) . length ;
756763
757764 // Buscar candidatos ativos desta eleição
758765 const todosCandidatos = await dbCandidatos . allDocs ( { include_docs : true } ) ;
759766 const candidatosAtivos = todosCandidatos . rows . filter ( row =>
760767 row . doc . ativo !== false && row . doc . eleicao_id === ELEICAO_ATUAL_ID
761768 ) ;
762769
763- // Atualizar estatísticas
764- document . getElementById ( 'total-votos' ) . textContent = todosVotos . rows . length ;
770+ // Atualizar estatísticas (AGORA APENAS DESTA ELEIÇÃO)
771+ document . getElementById ( 'total-votos' ) . textContent = votosDestaEleicao . length ;
765772 document . getElementById ( 'votos-enviados' ) . textContent = votosEnviados ;
766773 document . getElementById ( 'votos-pendentes' ) . textContent = votosPendentes ;
767774 document . getElementById ( 'candidatos-ativos' ) . textContent = candidatosAtivos . length ;
768775
769- // Calcular votos por candidato (AGORA CONTA TODOS OS VOTOS)
776+ // ✅ CORREÇÃO: Atualizar informações detalhadas
777+ document . getElementById ( 'info-total' ) . textContent = `Apenas desta eleição` ;
778+ document . getElementById ( 'info-enviados' ) . textContent = `Com CID nesta eleição` ;
779+ document . getElementById ( 'info-pendentes' ) . textContent = `Sem sincronização nesta eleição` ;
780+
781+ // Calcular votos por candidato (APENAS VOTOS DESTA ELEIÇÃO)
770782 const resultados = candidatosAtivos . map ( candidatoRow => {
771783 const candidato = candidatoRow . doc ;
772784
773- // ✅ CORREÇÃO: Conta TODOS os votos deste candidato, mesmo pendentes
785+ // ✅ CORREÇÃO: Conta apenas votos desta eleição para este candidato
774786 const votosDoCandidato = votosComCandidato . filter ( voto =>
775787 voto . doc . candidato_id === candidato . _id
776788 ) . length ;
777789
778- // ✅ CORREÇÃO: Porcentagem baseada no total de votos válidos (com candidato)
790+ // ✅ CORREÇÃO: Porcentagem baseada no total de votos válidos desta eleição
779791 const porcentagem = votosComCandidato . length > 0 ?
780792 ( ( votosDoCandidato / votosComCandidato . length ) * 100 ) . toFixed ( 1 ) : 0 ;
781793
0 commit comments