/* ═══════════════════════════════════════════════════════════════════════════
   ═══ ★ 카드 설정 패널 토글 / 탭 / 컬럼 ON·OFF ★ ═══
   ═══════════════════════════════════════════════════════════════════════════ */

function toggleSettings(btn){
  const card=btn.closest('.tpl-card');
  const panel=card?.querySelector('.tpl-settings');
  if(!panel)return;
  const willOpen=!panel.classList.contains('open');
  // 다른 카드 패널 다 닫음
  document.querySelectorAll('.tpl-settings.open').forEach(p=>p.classList.remove('open'));
  document.querySelectorAll('.tpl-head-btn.on').forEach(b=>b.classList.remove('on'));
  if(willOpen){
    panel.classList.add('open');
    const gearBtn=card.querySelector('.tpl-head-btn[title="카드 설정"]');
    if(gearBtn)gearBtn.classList.add('on');
  }
}

function switchTpsTab(tabBtn,paneName){
  const panel=tabBtn.closest('.tpl-settings');
  if(!panel)return;
  panel.querySelectorAll('.tps-tab').forEach(t=>t.classList.remove('on'));
  tabBtn.classList.add('on');
  panel.querySelectorAll('.tps-pane').forEach(p=>p.classList.toggle('on',p.dataset.pane===paneName));
}

/* 인사 테이블 컬럼 매핑 — nth-child 인덱스 */
const HR_COL_INDEX={
  'dept':3,'rank':4,'date':5,'status':6,'action':7
};

function toggleHrCol(sw,col){
  sw.classList.toggle('on');
  const card=document.getElementById('card-hr-main');
  if(!card)return;
  const isOn=sw.classList.contains('on');
  if(col==='emp-id'){
    card.querySelectorAll('.hr-emp-id').forEach(el=>el.classList.toggle('hr-col-hidden',!isOn));
    return;
  }
  const idx=HR_COL_INDEX[col];
  if(!idx)return;
  card.querySelectorAll(`.hr-table th:nth-child(${idx}),.hr-table td:nth-child(${idx})`).forEach(el=>el.classList.toggle('hr-col-hidden',!isOn));
  toast((isOn?'':'')+(isOn?'컬럼 표시':'컬럼 숨김'),'info');
}

function toggleHrStats(sw){
  sw.classList.toggle('on');
  const card=document.getElementById('card-hr-main');
  if(!card)return;
  const stats=card.querySelector('.hr-stat-cards');
  if(stats)stats.style.display=sw.classList.contains('on')?'grid':'none';
}

function toggleHrFilter(sw){
  sw.classList.toggle('on');
  const card=document.getElementById('card-hr-main');
  if(!card)return;
  const filter=card.querySelector('.hr-filter');
  if(filter)filter.style.display=sw.classList.contains('on')?'flex':'none';
}

/* 일반 토글 (특별 동작 없는 것들 — onclick 미지정 토글에만 적용) */
document.addEventListener('click',e=>{
  const sw=e.target.closest('.toggle-sw');
  if(!sw)return;
  if(sw.classList.contains('disabled'))return;
  if(sw.hasAttribute('onclick'))return; // onclick 핸들러 있으면 그게 처리
  sw.classList.toggle('on');
});

/* 색상/스타일 prefab 클릭 */
document.addEventListener('click',e=>{
  const color=e.target.closest('.tps-color');
  if(color){
    color.parentElement.querySelectorAll('.tps-color').forEach(c=>c.classList.remove('on'));
    color.classList.add('on');
    return;
  }
  const styleBtn=e.target.closest('.tps-style-btn');
  if(styleBtn){
    styleBtn.parentElement.querySelectorAll('.tps-style-btn').forEach(b=>b.classList.remove('on'));
    styleBtn.classList.add('on');
  }
});

/* 외부 클릭으로 설정 패널 닫기 */
document.addEventListener('click',e=>{
  if(e.target.closest('.tpl-settings'))return;
  if(e.target.closest('.tpl-head-btn[title="카드 설정"]'))return;
  document.querySelectorAll('.tpl-settings.open').forEach(p=>p.classList.remove('open'));
  document.querySelectorAll('.tpl-head-btn.on').forEach(b=>b.classList.remove('on'));
});
