';
tabs.after(pane);let view='activity',data=null,loading=false,starting=false,paused=false,map=null,layers=null,markers=new Map(),ownMarker=null,fitted=false,selected=null,selectedEvent=null;
const snapshots=new WeakMap();
function markup(selector,html){const el=$(selector);if(snapshots.get(el)===html)return;snapshots.set(el,html);el.innerHTML=html;}
const time=t=>t?new Date(t).toLocaleString(BPQI18n.locale(),{day:'2-digit',month:'2-digit',hour:'2-digit',minute:'2-digit',second:'2-digit'}):'—';
const type=t=>({position:'Posizione',message:'Messaggio',status:'Stato',object:'Oggetto APRS',weather:'Meteo',telemetry:'Telemetria','third-party':'Messaggio inoltrato',query:'Richiesta'}[t]||t);
const source=()=>$('#aprs-source').value,query=()=>$('#aprs-search').value.trim().toUpperCase();
const stations=()=>data?.stations.filter(s=>(!source()||s.transport===source()||source()==='Radio'&&s.heardRadio)&&(!query()||(s.call+' '+s.status+' '+s.lastBody).toUpperCase().includes(query())))||[];
function setView(next){view=next;form.hidden=next!=='config';pane.hidden=next!=='activity';$('#aprs-save').hidden=next!=='config';tabs.querySelectorAll('button').forEach(b=>b.setAttribute('aria-selected',String(b.dataset.aprsView===next)));if(next==='activity'&&page==='aprs'){start();requestAnimationFrame(()=>map?.invalidateSize({pan:false}));}}
tabs.onclick=e=>{const next=e.target.closest('[data-aprs-view]')?.dataset.aprsView;if(next)setView(next);};
function symbolIcon(position,transport='Radio'){
const info=window.BPQAprsSymbols.decode(position),html=window.BPQAprsSymbols.html(position);
return L.divIcon({className:'aprs-station-icon '+(transport==='Own'?'own':transport==='Radio'?'radio':'internet'),html,iconSize:[32,32],iconAnchor:[16,16],popupAnchor:[0,-17]});
}
const symbolKey=(s)=>JSON.stringify([s.position?.symset,s.position?.symbol,s.transport]);
function symbolDetail(position){const info=window.BPQAprsSymbols.decode(position);return info?esc(info.name)+' · '+esc(info.code)+''+(info.overlay?' · '+BPQI18n.t('Sovrapposizione')+' '+esc(info.overlay):''):BPQI18n.t('Simbolo non disponibile');}
function popup(s){return '
'+esc(s.call)+''+symbolDetail(s.position)+''+esc(s.transport)+(s.port?' · Port '+s.port:'')+''+esc(time(s.lastAt))+''+s.position.lat.toFixed(5)+', '+s.position.lon.toFixed(5)+'
');}
function render(){if(!data)return;const events=data.events.filter(e=>(!source()||e.transport===source())&&(!$('#aprs-direction').value||e.direction===$('#aprs-direction').value)&&(!query()||(e.packet+' '+e.from).toUpperCase().includes(query()))).slice().reverse();
$('#aprs-live-state').textContent=paused?'Vista in pausa':data.monitor==='online'?'● APRS in tempo reale':'Monitor '+data.monitor;
markup('#aprs-activity-kpis',[['Radio RX',events.filter(e=>e.transport==='Radio'&&e.direction==='RX').length],['Radio TX · BPQ',events.filter(e=>e.transport==='Radio'&&e.direction==='TX').length],['Internet RX',events.filter(e=>e.transport==='Internet'&&e.direction==='RX').length],['Internet TX',events.filter(e=>e.transport==='Internet'&&e.direction==='TX').length],['Stazioni ricevute',stations().length]].map(([k,v])=>''+esc(k)+''+v+'').join(''));
$('#aprs-traffic-count').textContent=events.length+' '+BPQI18n.t('pacchetti nel registro');markup('#aprs-traffic-rows',events.slice(0,400).map(e=>'