function switchTab(name){
  document.querySelectorAll('.tab').forEach(t=>t.classList.remove('active'));
  document.querySelectorAll('.card').forEach(c=>c.style.display='none');
  document.querySelector(`.tab[onclick*="${name}"]`).classList.add('active');
  document.getElementById(`tab-${name}`).style.display='block';
}

// Kalkulator FO sederhana
function calcFO(){
  const input=parseFloat(document.getElementById('inputLevel').value)||0;
  const N=parseInt(document.getElementById('splitN').value)||2;
  const idealLoss=10*Math.log10(N);
  const insLoss=1.2;
  const totalLoss=idealLoss+insLoss;
  const out=input-totalLoss;
  document.getElementById('foResult').style.display='block';
  document.getElementById('foResult').innerText=
    `Input: ${input} dBm\nSplitter 1:${N} → Loss ${totalLoss.toFixed(2)} dB\nOutput per-port: ${out.toFixed(2)} dBm`;
}

// Kalkulator Rasio FO
function calcRasio(){
  const input=parseFloat(document.getElementById('rasioInputLevel').value)||0;
  const N=parseInt(document.getElementById('rasioSplit').value)||2;
  const idealLoss=10*Math.log10(N);
  const insLoss=1.2;
  const totalLoss=idealLoss+insLoss;
  const out=input-totalLoss;
  let html=`<div class="results">Input: ${input} dBm<br>Splitter 1:${N} → Total Loss ${totalLoss.toFixed(2)} dB<br>Output per-port: ${out.toFixed(2)} dBm</div>`;
  html+=`<table><tr><th>Port</th><th>Loss (dB)</th><th>Output (dBm)</th></tr>`;
  for(let i=1;i<=N;i++){
    html+=`<tr><td>${i}</td><td>${totalLoss.toFixed(2)}</td><td>${out.toFixed(2)}</td></tr>`;
  }
  html+=`</table>`;
  document.getElementById('rasioResult').style.display='block';
  document.getElementById('rasioResult').innerHTML=html;
}

// Burst generator
function genBurst(){
  const val=document.getElementById('burstInput').value.trim();
  if(!val) return;
  const parts=val.split(" ");
  if(parts.length<6){alert("Format salah! Contoh: 40M/40M 60M/60M 32M/32M 15/15 5 default-small");return;}
  const [rate,burst,threshold,time,prio,queue]=parts;
  const [t1,t2]=time.split("/");
  const script=`/ppp profile\nset default rate-limit=${rate} burst-limit=${burst} burst-threshold=${threshold} burst-time=${t1}s/${t2}s queue-type=${queue} priority=${prio}`;
  document.getElementById('burstResult').style.display='block';
  document.getElementById('burstResult').innerText=script;
  document.getElementById('copyBox').style.display='block';
}
function copyScript(){
  const txt=document.getElementById('burstResult').innerText;
  navigator.clipboard.writeText(txt).then(()=>alert("Script copied!"));
}
