/* ===== Tab ===== */
function switchTab(name, event) {
    // 권한 guard: currentUserAllowedTabs가 Set이면 허용된 탭만 접근
    if (typeof currentUserAllowedTabs !== 'undefined'
        && currentUserAllowedTabs
        && !currentUserAllowedTabs.has(name)) return;

    document.querySelectorAll('[id^="tab-"]').forEach(el => el.classList.add('hidden'));
    document.getElementById('tab-' + name)?.classList.remove('hidden');
    document.querySelectorAll('.tab-btn').forEach(btn => btn.classList.remove('active'));
    if (event?.currentTarget) {
        event.currentTarget.classList.add('active');
    } else {
        const btn = document.querySelector(`.tab-btn[data-tab="${name}"]`);
        if (btn) btn.classList.add('active');
    }
    // 사이드바 레이아웃 탭에서 main/nav/header 너비 확장
    const mainEl = document.querySelector('main');
    const navInner = document.getElementById('tabNavInner');
    const headerInner = document.getElementById('headerInner');
    const wideClass = 'max-w-[1600px]';
    const defaultClass = 'max-w-7xl';
    if (name === 'workplaces' || name === 'tasks' || name === 'vacations' || name === 'equipments') {
        [mainEl, navInner, headerInner].forEach(el => { el.classList.remove(defaultClass); el.classList.add(wideClass); });
    } else {
        [mainEl, navInner, headerInner].forEach(el => { el.classList.remove(wideClass); el.classList.add(defaultClass); });
    }
    if (name === 'projects' && !projectsLoaded) loadProjects();
    if (name === 'workers' && !workersLoaded) loadWorkers();
    if (name === 'departments' && !departmentsLoaded) loadDepartments();
    if (name === 'workplaces' && !workplacesLoaded) loadWorkplaces();
    if (name === 'tasks' && !tasksLoaded) loadTasksTab();
    if (name === 'vacations' && !vacationsLoaded) loadVacationsTab();
    if (name === 'issueTypes' && !issueTypesLoaded) loadIssueTypes();
    if (name === 'permissions' && !permissionsTabLoaded) loadPermissionsTab();
    if (name === 'partners' && !partnersLoaded) loadPartnersTab();
    if (name === 'vendors' && !vendorsLoaded) loadVendorsTab();
    if (name === 'consumables' && !consumablesLoaded) loadConsumablesTab();
    if (name === 'notificationRecipients' && !nrLoaded) loadNotificationRecipientsTab();
    if (name === 'equipments' && !equipmentsLoaded) loadEquipmentsTab();
    if (name === 'vacationSettings' && typeof loadVacationSettingsTab === 'function') loadVacationSettingsTab();
}
