Chiste Automacao
Agendamento de Calibracoes
+55
Chiste Automacao
Agenda de Calibracoes - Semana '+lbl+'
'+new Date().toLocaleDateString('pt-BR')+'
EQUIPE: '+leg+'
'+corpo+'
'; } // ── WHATSAPP ────────────────────────────────────────────────────────────────── function rWA(){ var ds=gSW();var s=ds[0],e=ds[ds.length-1]; document.getElementById('wal').textContent=String(s.getDate()).padStart(2,'0')+'/'+(s.getMonth()+1).toString().padStart(2,'0')+' (Dom) - '+String(e.getDate()).padStart(2,'0')+'/'+(e.getMonth()+1).toString().padStart(2,'0')+' (Sab)'; var pv=document.getElementById('waprev');var dts=ds.map(fmt); var ags=D.agendamentos.filter(function(a){return dts.includes(a.data);}).sort(function(a,b){return a.data.localeCompare(b.data)||a.turno.localeCompare(b.turno);}); if(!ags.length){pv.innerHTML='
Nenhum servico esta semana.
';return;} var h='';var ud=''; ags.forEach(function(a){ var ts=gT(a); var nt=ts.map(function(id){var t=D.tecnicos.find(function(x){return x.id===id;});var c=co(id);return t?''+t.nome.split(' ')[0]+'':'';}).join(' '); if(a.data!==ud){var dia=ds.find(function(d){return fmt(d)===a.data;});h+='
'+(dia?DS[dia.getDay()]+' '+dia.getDate()+'/'+(dia.getMonth()+1).toString().padStart(2,'0'):fBR(a.data))+'
';ud=a.data;} h+='
'+TI[a.turno]+'
'+gE(a)+(a.prioridade==='urgente'?' URGENTE':'')+(a.os?' | OS:'+a.os:'')+'
'+TL[a.turno]+' | '+a.local+'
'+nt+'
'; }); pv.innerHTML=h; } function sUI(){ var t=document.getElementById('aon');if(!t)return; t.checked=AC.on;document.getElementById('acfg').style.display=AC.on?'block':'none'; document.getElementById('atxt').textContent=AC.on?'Ativado':'Desativado'; document.getElementById('adia').value=AC.dia;document.getElementById('ahr').value=AC.hr;document.getElementById('asem').value=AC.sem;aRes(); } function aRes(){ var d=document.getElementById('adia');var h=document.getElementById('ahr');if(!d)return; var r=document.getElementById('ares'); if(r)r.textContent='Todo '+DL[parseInt(d.value)]+' as '+h.value+' sera gerado o PDF da agenda '+(document.getElementById('asem').value==='proxima'?'da proxima semana':'da semana atual')+'.'; } // ── RENDERIZACOES ───────────────────────────────────────────────────────────── function rChips(){ var c=document.getElementById('chips');if(!c)return;c.innerHTML=''; var b=document.createElement('button');b.className='tch active';b.style.cssText='background:#e8f1fb;color:#0c447c;border-color:#b5d4f4;';b.textContent='Todos'; b.addEventListener('click',function(){fTec('',this);});c.appendChild(b); D.tecnicos.forEach(function(t){var cc=co(t.id);var bt=document.createElement('button');bt.className='tch';bt.textContent=t.nome.split(' ')[0];bt.addEventListener('click',function(){fTec(t.id,this);});c.appendChild(bt);}); } function rAg(){if(vw==='d')rD();else if(vw==='s')rS();else rM();} function tAvs(ids){ return ids.slice(0,3).map(function(id,i){var c=co(id);var t=D.tecnicos.find(function(x){return x.id===id;});return'
'+(t?t.nome.split(' ').map(function(n){return n[0];}).join('').slice(0,2):'?')+'
';}).join('')+(ids.length>3?'
+'+(ids.length-3)+'
':''); } function tNms(ids){ return ids.map(function(id){var t=D.tecnicos.find(function(x){return x.id===id;});var c=co(id);return t?''+t.nome.split(' ')[0]+'':'?';}).join(', '); } function mkC(a){ var ts=gT(a);var c=ts.length?co(ts[0]):{bg:'#eee',tx:'#999'}; var sm={agendado:'bwa2',campo:'bi2',concluido:'bsu'};var sl={agendado:'Agendado',campo:'Em campo',concluido:'Concluido'}; var div=document.createElement('div');div.className='agc '+clA(a); div.innerHTML='
'+tAvs(ts)+'
'+gE(a)+(a.prioridade==='urgente'?' Urgente':'')+(ts.length>1?' '+ts.length+' tec.':'')+'
'+(a.os?'OS: '+a.os+'':'')+(a.orc?'Orc: '+a.orc+'':'')+''+tNms(ts)+' | '+a.local+'
'+(sl[a.status]||a.status)+'
'+(adm()?'
':''); return div; } function rD(){ var d=new Date();d.setDate(d.getDate()+dO);var ds=fmt(d);var hj=fmt(new Date()); document.getElementById('ld').textContent=(ds===hj?'Hoje - ':'')+DS[d.getDay()]+', '+d.getDate()+' de '+MS[d.getMonth()]+' de '+d.getFullYear(); var ags=gA(function(a){return a.data===ds;}); [['tmanha','manha'],['ttarde','tarde']].forEach(function(p){ var el=document.getElementById(p[0]);el.innerHTML=''; var it=ags.filter(function(a){return a.turno===p[1]||a.turno==='manhaTarde';}); if(!it.length){el.innerHTML='
Nenhum agendamento.
';return;} it.forEach(function(a){el.appendChild(mkC(a));}); }); } function rS(){ var h=new Date();var dw=h.getDay();var mo=new Date(h);mo.setDate(h.getDate()-(dw===0?6:dw-1)+sO*7); var ds=Array.from({length:5},function(_,i){var d=new Date(mo);d.setDate(mo.getDate()+i);return d;}); document.getElementById('ls').textContent=String(ds[0].getDate()).padStart(2,'0')+'/'+(ds[0].getMonth()+1).toString().padStart(2,'0')+' - '+String(ds[4].getDate()).padStart(2,'0')+'/'+(ds[4].getMonth()+1).toString().padStart(2,'0')+'/'+ds[4].getFullYear(); var g=document.getElementById('sgrid');g.innerHTML=''; ['Turno'].concat(ds.map(function(d){return DS[d.getDay()]+' '+d.getDate()+'/'+(d.getMonth()+1);})).forEach(function(t){var c=document.createElement('div');c.className='sh2';c.textContent=t;g.appendChild(c);}); [{k:'manha',ic:'Manha'},{k:'tarde',ic:'Tarde'}].forEach(function(p){ var tc=document.createElement('div');tc.className='st';tc.textContent=p.ic;g.appendChild(tc); ds.forEach(function(dia){ var cell=document.createElement('div');cell.className='sc'; gA(function(a){return a.data===fmt(dia)&&(a.turno===p.k||a.turno==='manhaTarde');}).forEach(function(ev){ var ts=gT(ev);var c=ts.length?co(ts[0]):{bg:'#eee',tx:'#999'}; var nm=ts.map(function(id){var t=D.tecnicos.find(function(x){return x.id===id;});return t?t.nome.split(' ')[0]:'?';}).join('+'); var dv=document.createElement('div');dv.className='se';dv.style.cssText='background:'+c.bg+';color:'+c.tx+';border-left:3px solid '+c.tx+';'; dv.innerHTML=''+gE(ev).split(' ')[0]+' '+nm+''+(adm()?'':''); cell.appendChild(dv); }); g.appendChild(cell); }); }); } function rM(){ var rf=new Date();rf.setMonth(rf.getMonth()+mO);var an=rf.getFullYear();var me=rf.getMonth(); document.getElementById('lm').textContent=MS[me]+' '+an; var g=document.getElementById('cgrid');g.innerHTML=''; ['Seg','Ter','Qua','Qui','Sex','Sab','Dom'].forEach(function(d){var c=document.createElement('div');c.className='ch';c.textContent=d;g.appendChild(c);}); var pr=new Date(an,me,1);var ini=new Date(pr);var dw=pr.getDay();ini.setDate(pr.getDate()-(dw===0?6:dw-1));var hj=fmt(new Date()); for(var i=0;i<42;i++){ var d=new Date(ini);d.setDate(ini.getDate()+i);var ds=fmt(d); var cell=document.createElement('div');cell.className='cd'+(d.getMonth()!==me?' om':'')+(ds===hj?' today':''); var nu=document.createElement('div');nu.className='cn';nu.textContent=d.getDate();cell.appendChild(nu); gA(function(a){return a.data===ds;}).slice(0,3).forEach(function(ev){ var ts=gT(ev);var c=ts.length?co(ts[0]):{bg:'#eee',tx:'#999'}; var e=document.createElement('div');e.className='ce';e.style.cssText='background:'+c.bg+';color:'+c.tx+';'; e.innerHTML=''+gE(ev).split(' ')[0]+(ev.os?' OS:'+ev.os:'')+''+(adm()?'':''); cell.appendChild(e); }); var evs=gA(function(a){return a.data===ds;}); if(evs.length>3){var m=document.createElement('div');m.style.cssText='font-size:10px;color:#64748b;';m.textContent='+'+(evs.length-3)+' mais';cell.appendChild(m);} g.appendChild(cell); } } function rServ(){ var li=document.getElementById('lserv');if(!li)return; var ft=document.getElementById('fst').value; var td=D.agendamentos.slice().sort(function(a,b){return b.data.localeCompare(a.data);}); if(ft)td=td.filter(function(a){return a.status===ft;}); li.innerHTML=''; if(!td.length){li.innerHTML='

Nenhum servico.

';return;} var sm={agendado:'bwa2',campo:'bi2',concluido:'bsu'};var sl={agendado:'Agendado',campo:'Em campo',concluido:'Concluido'}; td.forEach(function(a){ var ts=gT(a);var c=ts.length?co(ts[0]):{bg:'#eee',tx:'#999'}; var div=document.createElement('div');div.className='si'; var info=document.createElement('div');info.style.cssText='display:flex;align-items:center;gap:10px;flex:1;cursor:pointer;'; info.innerHTML='
'+tAvs(ts)+'
'+gE(a)+(a.prioridade==='urgente'?' Urgente':'')+'
'+(a.os?'OS: '+a.os+'':'')+(a.orc?'Orc: '+a.orc+'':'')+''+tNms(ts)+' | '+TL[a.turno]+'
'+fBR(a.data)+'
'+(sl[a.status]||a.status)+'
'; info.addEventListener('click',function(){editA(a.id);});div.appendChild(info); if(adm()){ var ac=document.createElement('div');ac.style.cssText='display:flex;gap:5px;flex-shrink:0;'; var bE=document.createElement('button');bE.className='btn';bE.style.cssText='font-size:12px;padding:5px 8px;';bE.textContent='Editar';bE.addEventListener('click',function(ev){ev.stopPropagation();editA(a.id);}); var bD=document.createElement('button');bD.className='btn d';bD.style.cssText='font-size:12px;padding:5px 8px;';bD.textContent='Excluir';bD.addEventListener('click',function(ev){ev.stopPropagation();if(!confirm('Excluir?'))return;D.agendamentos=D.agendamentos.filter(function(x){return x.id!==a.id;});salvar();render();toast('Excluido.');}); ac.appendChild(bE);ac.appendChild(bD);div.appendChild(ac); } li.appendChild(div); }); } function rTecs(){ var li=document.getElementById('ltec');if(!li)return;li.innerHTML=''; if(!D.tecnicos.length){li.innerHTML='

Nenhum tecnico cadastrado.

';return;} var n=new Date(); D.tecnicos.forEach(function(t){ var ms=D.agendamentos.filter(function(a){var d=new Date(a.data+'T12:00');return gT(a).includes(t.id)&&d.getMonth()===n.getMonth()&&d.getFullYear()===n.getFullYear();}); var c=co(t.id);var ini=t.nome.split(' ').map(function(x){return x[0];}).join('').slice(0,2).toUpperCase(); var tw=t.whatsapp&&t.whatsapp.replace(/\D/g,'').length>=10; var div=document.createElement('div');div.className='li'; div.innerHTML='
'+ini+'
'+t.nome+(tw?' WA':'Sem WhatsApp')+'
'+(tw?'+55 '+t.whatsapp:'sem numero')+' | '+ms.length+' servico(s) este mes
'; var wr=document.createElement('div');wr.style.cssText='display:flex;gap:6px;align-items:center;'; if(tw){var bw=document.createElement('button');bw.className='btn wa';bw.style.cssText='font-size:12px;padding:5px 8px;';bw.textContent='WA';bw.addEventListener('click',function(){window.open('https://wa.me/55'+t.whatsapp.replace(/\D/g,'')+'?text=Ola+'+encodeURIComponent(t.nome),'_blank');});wr.appendChild(bw);} var bE=document.createElement('button');bE.className='btn';bE.style.cssText='font-size:12px;padding:5px 8px;';bE.textContent='Editar';bE.addEventListener('click',function(){mT(t.id);}); var bD=document.createElement('button');bD.className='btn d';bD.style.cssText='font-size:12px;padding:5px 8px;';bD.textContent='Excluir';bD.addEventListener('click',function(){D.tecnicos=D.tecnicos.filter(function(x){return x.id!==t.id;});salvar();render();toast('Tecnico removido.');}); wr.appendChild(bE);wr.appendChild(bD);div.appendChild(wr);li.appendChild(div); }); } function rAcess(){ var tb=document.getElementById('tacess');if(!tb)return;tb.innerHTML=''; var pm={admin:'Administrador',visualizador:'Visualizador'}; USUARIOS.forEach(function(u){ var im=CU&&u.id===CU.id;var ini=u.nome.split(' ').map(function(x){return x[0];}).join('').slice(0,2).toUpperCase(); var pc=u.perfil==='admin'?'bwa2':'bg2'; var tr=document.createElement('tr'); tr.innerHTML='
'+ini+'
'+u.nome+(im?' Voce':'')+'
'+u.email+''+(pm[u.perfil]||u.perfil)+''+(u.status==='ativo'?'Ativo':'Inativo')+'Fixo no sistema'; tb.appendChild(tr); }); } // ── MODAIS ──────────────────────────────────────────────────────────────────── window.editA=function(id){ if(!CU)return;eA=id;var a=D.agendamentos.find(function(x){return x.id===id;});if(!a)return; document.getElementById('agtit').textContent='Editar agendamento';document.getElementById('agban').style.display='flex';document.getElementById('bexag').style.display=adm()?'inline-flex':'none'; document.getElementById('femp').value=gE(a);document.getElementById('fos').value=a.os||'';document.getElementById('forc').value=a.orc||''; document.getElementById('fdat').value=a.data;document.getElementById('ftur').value=a.turno;document.getElementById('fpri').value=a.prioridade; document.getElementById('floc').value=a.local;document.getElementById('fsta').value=a.status;document.getElementById('fobs').value=a.obs||''; rTP(gT(a));oM('oag');setTimeout(iAC,50); }; window.exAg=function(id){if(!confirm('Excluir?'))return;D.agendamentos=D.agendamentos.filter(function(x){return x.id!==id;});salvar();render();toast('Agendamento excluido.');}; function nA(){ if(!adm())return;eA=null; document.getElementById('agtit').textContent='Novo agendamento';document.getElementById('agban').style.display='none';document.getElementById('bexag').style.display='none'; ['femp','fos','forc','fobs'].forEach(function(id){document.getElementById(id).value='';}); document.getElementById('fdat').value=fmt(new Date());document.getElementById('ftur').value='manha';document.getElementById('fsta').value='agendado';document.getElementById('fpri').value='normal';document.getElementById('floc').value='Campo (cliente)'; rTP([]);oM('oag');setTimeout(iAC,50); } function sA(){ var en=document.getElementById('femp').value.trim();var os=document.getElementById('fos').value.trim();var orc=document.getElementById('forc').value.trim();var dt=document.getElementById('fdat').value; if(!en||!dt){toast('Preencha ao menos a empresa e a data!',true);return;} if(tS.length===0){document.getElementById('tperr').style.display='block';return;} document.getElementById('tperr').style.display='none'; if(!D.empresasCustom.includes(en))D.empresasCustom.push(en); var d={empresaNome:en,os:os,orc:orc,data:dt,turno:document.getElementById('ftur').value,tecnicos:tS.slice(),prioridade:document.getElementById('fpri').value,local:document.getElementById('floc').value,status:document.getElementById('fsta').value,obs:document.getElementById('fobs').value}; if(eA){var a=D.agendamentos.find(function(x){return x.id===eA;});if(a)Object.assign(a,d);toast('Atualizado!');} else{D.agendamentos.push(Object.assign({id:'a'+Date.now()},d));toast('Salvo!');} salvar();cM('oag');render(); } function exAgM(){if(!eA||!confirm('Excluir?'))return;D.agendamentos=D.agendamentos.filter(function(x){return x.id!==eA;});salvar();cM('oag');render();toast('Excluido.');} function mT(id){ eT=id||null; if(id){var t=D.tecnicos.find(function(x){return x.id===id;});if(!t)return;document.getElementById('tectit').textContent='Editar tecnico';document.getElementById('ftnm').value=t.nome;document.getElementById('ftwp').value=t.whatsapp||'';document.getElementById('bextec').style.display='inline-flex';} else{document.getElementById('tectit').textContent='Novo tecnico';document.getElementById('ftnm').value='';document.getElementById('ftwp').value='';document.getElementById('bextec').style.display='none';} oM('otec'); } function svT(){ var nm=document.getElementById('ftnm').value.trim();if(!nm){toast('Informe o nome!',true);return;} var w=document.getElementById('ftwp').value.replace(/\D/g,''); if(eT){var t=D.tecnicos.find(function(x){return x.id===eT;});if(t){t.nome=nm;t.whatsapp=w;}toast('Tecnico atualizado!');} else{D.tecnicos.push({id:'t'+Date.now(),nome:nm,whatsapp:w});toast('Tecnico adicionado!');} salvar();cM('otec');render(); } function mU(id){ eU=id||null;document.getElementById('faerr').style.display='none'; if(id){var u=USUARIOS.find(function(x){return x.id===id;});if(!u)return;document.getElementById('usrtit').textContent='Ver usuario';document.getElementById('fanm').value=u.nome;document.getElementById('faem').value=u.email;document.getElementById('fasen').value='';document.getElementById('faprf').value=u.perfil;document.getElementById('faust').value=u.status;document.getElementById('bexusr').style.display='none';} else{document.getElementById('usrtit').textContent='Novo usuario';['fanm','faem','fasen'].forEach(function(x){document.getElementById(x).value='';});document.getElementById('faprf').value='visualizador';document.getElementById('faust').value='ativo';document.getElementById('bexusr').style.display='none';} oM('ousr'); } function svU(){ document.getElementById('faerr').textContent='Os usuarios sao gerenciados pelo administrador do sistema.'; document.getElementById('faerr').style.display='block'; } // ── INIT ────────────────────────────────────────────────────────────────────── function init(){ document.getElementById('btn-entrar').addEventListener('click',login); document.getElementById('le').addEventListener('keydown',function(e){if(e.key==='Enter')document.getElementById('lsen').focus();}); document.getElementById('lsen').addEventListener('keydown',function(e){if(e.key==='Enter')login();}); document.getElementById('vlog').addEventListener('click',function(){var el=document.getElementById('lsen');el.type=el.type==='password'?'text':'password';this.querySelector('i').className=el.type==='password'?'ti ti-eye':'ti ti-eye-off';}); document.getElementById('bout').addEventListener('click',logout); ['agenda','servicos','whatsapp','tecnicos','acessos'].forEach(function(id){document.getElementById('tab-'+id).addEventListener('click',function(){sTab(id);});}); document.getElementById('vtd').addEventListener('click',function(){sView('d');});document.getElementById('vts').addEventListener('click',function(){sView('s');});document.getElementById('vtm').addEventListener('click',function(){sView('m');}); document.getElementById('dp').addEventListener('click',function(){dO--;rD();});document.getElementById('dn').addEventListener('click',function(){dO++;rD();});document.getElementById('dh').addEventListener('click',function(){dO=0;rD();}); document.getElementById('sp').addEventListener('click',function(){sO--;rS();});document.getElementById('sn').addEventListener('click',function(){sO++;rS();});document.getElementById('sh').addEventListener('click',function(){sO=0;rS();}); document.getElementById('mp').addEventListener('click',function(){mO--;rM();});document.getElementById('mn').addEventListener('click',function(){mO++;rM();});document.getElementById('mh').addEventListener('click',function(){mO=0;rM();}); document.getElementById('bimp').addEventListener('click',function(){gerarPDF(gSW());}); document.getElementById('bnag').addEventListener('click',nA);document.getElementById('bnag2').addEventListener('click',nA); document.getElementById('bsag').addEventListener('click',sA);document.getElementById('bcag').addEventListener('click',function(){cM('oag');});document.getElementById('bexag').addEventListener('click',exAgM); document.getElementById('oag').addEventListener('click',function(e){if(e.target===this)cM('oag');}); document.getElementById('bntec').addEventListener('click',function(){mT(null);});document.getElementById('bstec').addEventListener('click',svT);document.getElementById('bctec').addEventListener('click',function(){cM('otec');}); document.getElementById('bextec').addEventListener('click',function(){if(!eT)return;D.tecnicos=D.tecnicos.filter(function(x){return x.id!==eT;});salvar();cM('otec');render();toast('Tecnico excluido.');}); document.getElementById('otec').addEventListener('click',function(e){if(e.target===this)cM('otec');}); document.getElementById('bnusr').addEventListener('click',function(){mU(null);});document.getElementById('bsusr').addEventListener('click',svU);document.getElementById('bcusr').addEventListener('click',function(){cM('ousr');}); document.getElementById('bexusr').addEventListener('click',function(){cM('ousr');}); document.getElementById('ousr').addEventListener('click',function(e){if(e.target===this)cM('ousr');}); document.getElementById('fst').addEventListener('change',rServ); document.getElementById('ftwp').addEventListener('input',function(){this.value=this.value.replace(/\D/g,'');}); document.getElementById('wap').addEventListener('click',function(){wO--;rWA();});document.getElementById('wan').addEventListener('click',function(){wO++;rWA();});document.getElementById('wah').addEventListener('click',function(){wO=0;rWA();}); document.getElementById('waenv').addEventListener('click',function(){gerarPDF(gSW());}); document.getElementById('aon').addEventListener('change',function(){AC.on=this.checked;document.getElementById('acfg').style.display=this.checked?'block':'none';document.getElementById('atxt').textContent=this.checked?'Ativado':'Desativado';if(this.checked)aRes();salvarAuto();}); document.getElementById('adia').addEventListener('change',function(){AC.dia=parseInt(document.getElementById('adia').value);salvarAuto();aRes();}); document.getElementById('ahr').addEventListener('change',function(){AC.hr=document.getElementById('ahr').value;salvarAuto();aRes();}); document.getElementById('asem').addEventListener('change',function(){AC.sem=document.getElementById('asem').value;salvarAuto();aRes();}); } // Primeiro faz login, depois conecta Firebase carregar(); init(); autoLogin(); // Firebase em segundo plano - nao bloqueia o login setTimeout(function(){ try{ db.ref('chiste').on('value', function(snapshot){ var dados = snapshot.val(); if(dados){ D.tecnicos = dados.tecnicos || []; D.agendamentos = dados.agendamentos || []; D.empresasCustom = dados.empresasCustom || []; D.agendamentos.forEach(function(a){ if(!a.tecnicos)a.tecnicos=a.tecnico?[a.tecnico]:[]; if(!a.empresaNome)a.empresaNome=a.empresa||''; if(!a.os)a.os='';if(!a.orc)a.orc=''; }); try{localStorage.setItem(SK,JSON.stringify(D));}catch(e){} if(CU)render(); } }); }catch(e){console.log('Firebase error:',e);} }, 1000);