T

TRÁFEGO SA

🚀 Iniciando Sistema TSA
Carregando bibliotecas...
'; // PANEL: Metas html+='
'; html+='

Metas — Agência Ace

'; if(aceProjetos.length){ aceProjetos.forEach(function(proj){ var cor=proj.cor||'#e3fb04'; var projMetas=aceMetas.filter(function(m){return m.projeto_id===proj.id}); var mesesNomes=['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']; html+='
'; html+='
'; html+='
'; html+=''+proj.nome+''; html+='
'; var projVendasPorMes={}; aceVendas.filter(function(v){return v.ace_projetos&&v.ace_projetos.slug===proj.slug}).forEach(function(v){ var mes=(v.data_venda||'').substring(0,7); if(!projVendasPorMes[mes])projVendasPorMes[mes]={fat:0,cash:0}; projVendasPorMes[mes].fat+=parseFloat(v.valor_venda)||0; projVendasPorMes[mes].cash+=parseFloat(v.cash_collected)||0; }); if(projMetas.length){ html+=''; projMetas.sort(function(a,b){return a.mes-b.mes}).forEach(function(m){ var mesStr=m.ano+'-'+String(m.mes).padStart(2,'0'); var real=projVendasPorMes[mesStr]; var realVal=real?(m.tipo==='faturamento'?real.fat:real.cash):0; var pctVal=m.valor_meta?Math.round(realVal/m.valor_meta*100):0; var cor2=pctVal>=100?'#22c55e':pctVal>=50?'#eab308':'#ef4444'; html+=''; html+=''; html+=''; html+=''; html+=''; html+=''; html+=''; html+=''; }); html+='
MêsTipoMetaRealizado%Progresso
'+(mesesNomes[m.mes-1]||m.mes)+' '+m.ano+''+m.tipo+''+fmtR(m.valor_meta)+''+fmtR(realVal)+''+pctVal+'%
'; }else{ html+='
Nenhuma meta cadastrada para este projeto. Configure metas na tabela ace_metas.
'; } html+='
'; }); }else{ html+='
Nenhum projeto encontrado.
'; } html+='
'; // close ace-metas html+=''; // close tab-ace document.getElementById('main').innerHTML=html; // RBAC: Controle de acesso por role var profile=await getProfile(); var userRole=profile?profile.role:'sdr'; window._userRole=userRole; // Definir permissões por role var rbac={ admin:{inicio:1,overview:1,funnels:1,relatorios:1,vendas_dash:1,vendas_detail:1,vendedores:1,comissoes:1,metas_visao:1,metas_gestao:1,financeiro:1,auditoria:1,usuarios:1,ace:1}, closer:{inicio:1,overview:1,funnels:1,relatorios:1,vendas_dash:1,vendas_detail:1,vendedores:0,comissoes:1,metas_visao:1,metas_gestao:1,financeiro:0,auditoria:0,usuarios:0,ace:1}, sdr:{inicio:1,overview:1,funnels:1,relatorios:1,vendas_dash:1,vendas_detail:1,vendedores:0,comissoes:1,metas_visao:1,metas_gestao:0,financeiro:0,auditoria:0,usuarios:0,ace:1}, financeiro:{inicio:1,overview:0,funnels:0,relatorios:0,vendas_dash:0,vendas_detail:0,vendedores:0,comissoes:1,metas_visao:0,metas_gestao:0,financeiro:1,auditoria:0,usuarios:0,ace:0} }; var perms=rbac[userRole]||rbac.sdr; // Sidebar: esconder itens sem permissão var sidebarLinks=document.querySelectorAll('.sidebar-menu a, .sidebar-menu .sidebar-group'); // Funis group: esconder se não tem acesso a nenhum sub var funisGroup=document.getElementById('funnels-group'); if(funisGroup){ var funisItems=funisGroup.querySelectorAll('a'); funisItems.forEach(function(a){ var txt=a.textContent.trim(); if(txt==='Visão Geral'&&!perms.overview)a.style.display='none'; if(txt==='Monitoramento'&&!perms.funnels)a.style.display='none'; if(txt.indexOf('Relatório')>-1&&!perms.relatorios)a.style.display='none'; }); if(!perms.overview&&!perms.funnels&&!perms.relatorios){ funisGroup.parentElement.style.display='none'; } } // Vendas group var vendasGroup=document.getElementById('vendas-group'); if(vendasGroup){ var vendasItems=vendasGroup.querySelectorAll('a'); vendasItems.forEach(function(a){ var txt=a.textContent.trim(); if(txt==='Visão Geral'&&!perms.vendas_dash)a.style.display='none'; if(txt==='Minhas Vendas'&&!perms.vendas_detail)a.style.display='none'; if(txt==='Vendedores'&&!perms.vendedores)a.style.display='none'; if(txt==='Comissões'&&!perms.comissoes)a.style.display='none'; }); if(!perms.vendas_dash&&!perms.vendas_detail&&!perms.vendedores&&!perms.comissoes){ vendasGroup.parentElement.style.display='none'; } } // Metas group var metasGroup=document.getElementById('metas-group'); if(metasGroup){ if(!perms.metas_visao&&!perms.metas_gestao){metasGroup.parentElement.style.display='none'} else{ var metasItems=metasGroup.querySelectorAll('a'); metasItems.forEach(function(a){ if(a.textContent.indexOf('Visão')>-1&&!perms.metas_visao)a.style.display='none'; if(a.textContent.indexOf('Gestão')>-1&&!perms.metas_gestao)a.style.display='none'; }); } } // Ace group var aceGroup=document.getElementById('ace-sidebar-group'); if(aceGroup)aceGroup.style.display=perms.ace?'':'none'; // Financeiro, Auditoria, Usuarios var sideFin=document.querySelector('a[onclick*="financeiro"]'); if(sideFin&&!perms.financeiro)sideFin.style.display='none'; var sideAud=document.getElementById('sidebarAuditoria'); if(sideAud)sideAud.style.display=perms.auditoria?'block':'none'; var sideUsr=document.getElementById('sidebarUsuarios'); if(sideUsr)sideUsr.style.display=perms.usuarios?'block':'none'; // Restore active tab after render document.querySelectorAll('.tab').forEach(function(t){t.classList.remove('active')}); var activeTabEl=document.getElementById('tab-'+currentTab); if(activeTabEl)activeTabEl.classList.add('active'); if(currentTab==='vendas'){ document.querySelectorAll('.vendas-sub').forEach(function(s){s.classList.remove('active')}); var subEl=document.getElementById('vendas-sub-'+currentVendasSubtab); if(subEl)subEl.classList.add('active'); } if(currentTab==='metas'){ document.querySelectorAll('.metas-sub').forEach(function(s){s.classList.remove('active')}); var metasSub=document.getElementById('metas-sub-gestao'); if(metasSub)metasSub.classList.add('active'); } if(currentTab==='relatorios'){ var relTab=document.getElementById('tab-relatorios'); if(relTab)relTab.classList.add('active'); } if(currentTab==='ace'){ document.querySelectorAll('.ace-panel').forEach(function(p){p.classList.remove('active')}); var acePanel=document.getElementById('ace-'+aceTab); if(acePanel)acePanel.classList.add('active'); var aceGrp=document.getElementById('ace-group'); if(aceGrp)aceGrp.classList.add('open'); var aceGrpParent=aceGrp?aceGrp.previousElementSibling:null; if(aceGrpParent)aceGrpParent.classList.add('expanded'); } // Render all charts after DOM is ready setTimeout(function(){ setTimeout(function(){renderOverviewCharts(d)},200); setTimeout(function(){renderVendasCharts(v)},300); setTimeout(function(){renderMetasCharts(window._mf,window._mc)},400); setTimeout(function(){if(window._renderGestao)window._renderGestao()},500); setTimeout(function(){renderNotificações()},600); setTimeout(function(){loadRelatorios()},700); setTimeout(function(){_finParcelas=finParcelas;renderFinTable()},800); var hVendasFil=[]; if(window._allVendas){window._allVendas.forEach(function(s){var dv=(s.data_venda||'');if(dv>=hDates.start&&dv'; document.getElementById('status').textContent='Erro'; console.error(e); }finally{window._loadingInProgress=false;} } // INIT - Using EXACT same pattern as test.html (which works) (async function(){ // Charts temporarily disabled for stability try{ // Update progress document.getElementById('initStep').textContent='Carregando Supabase...'; document.getElementById('initBar').style.width='20%'; // Check if supabase loaded - fast polling instead of fixed 2s wait if(typeof window.supabase==='undefined' || !window.supabase.createClient){ for(var _w=0;_w<20;_w++){ await new Promise(r => setTimeout(r, 100)); if(typeof window.supabase!=='undefined' && window.supabase.createClient) break; } if(typeof window.supabase==='undefined' || !window.supabase.createClient){ document.getElementById('main').innerHTML='
Erro ao carregar bibliotecas. Clique aqui para recarregar
'; document.getElementById('status').textContent='Erro'; return; } } document.getElementById('initStep').textContent='Conectando ao banco...'; document.getElementById('initBar').style.width='40%'; sb=window.supabase.createClient(window._k1,window._k2); document.getElementById('initStep').textContent='Verificando login...'; document.getElementById('initBar').style.width='60%'; var sess=await sb.auth.getSession(); if(!sess.data.session){window.location.href='login.html';return;} document.getElementById('initStep').textContent='Carregando dados...'; document.getElementById('initBar').style.width='80%'; // Registrar login no user_logs try{await logAction('login_dashboard',{});}catch(le){} await loadAndRender(); document.getElementById('initBar').style.width='100%'; document.getElementById('initProgress').style.display='none'; }catch(e){ document.getElementById('main').innerHTML='
⚠️ Erro ao Iniciar
'+e.message+'
'; document.getElementById('status').textContent='Erro'; console.error('INIT ERROR:',e); } })();