// ================================================ // sidebar.js — Sidebar compartida — Sisprime // ================================================ (function () { var NAV = [ { id:'inicio', label:'Acceso Rápido', icon:'fa-star-half-stroke', href:'index.html', single:true }, { id:'registros', label:'Registros', icon:'fa-user-plus', children:[ { id:'proveedores', label:'Proveedores', href:'proveedores.html', icon:'fa-truck', role:'gerente' }, { id:'usuarios', label:'Usuarios', href:'usuarios.html', icon:'fa-users' } ]}, { id:'ventas', label:'Ventas Gear Up', icon:'fa-cart-shopping', children:[ { id:'lista-pedidos', label:'Lista de Ventas', href:'lista-pedidos.html', icon:'fa-list-ul', color:'#1a6fc4' }, { id:'cotacao', label:'Nueva Solicitud', href:'cotacao.html', icon:'fa-plus-circle', color:'#16a34a' }, { id:'reportes', label:'Informe de Rentabilidad', href:'reportes.html', icon:'fa-chart-line', color:'#7c3aed', role:'admin' }, { id:'gastos', label:'Gastos de la Empresa', href:'gastos.html', icon:'fa-money-bill-wave', color:'#dc2626', role:'admin' }, { id:'socio', label:'Liquidación Socio', href:'socio.html', icon:'fa-handshake', color:'#d97706', role:'admin' }, { id:'historial', label:'Comprobantes y Cambios', href:'historial.html', icon:'fa-folder-open', color:'#0891b2', role:'gerente' } ]}, { id:'compras', label:'Compras', icon:'fa-basket-shopping', role:'gerente', children:[ { id:'compras', label:'Lista de Compras', href:'compras.html', icon:'fa-basket-shopping', role:'gerente' } ]}, { id:'ajustes', label:'Ajustes', icon:'fa-screwdriver-wrench', children:[ { id:'perfil', label:'Perfil de Usuario', href:'perfil.html', icon:'fa-user-circle', role:'gerente' }, { id:'usuarios', label:'Configurar Acceso', href:'usuarios.html', icon:'fa-shield-halved', role:'admin' } ]} ]; window.switchRole = function(role) { if (!window.SisprimeDB) return; var session = SisprimeDB.getSession(); if (!session || session.perfil !== 'admin') return; session.perfil = role; localStorage.setItem('sp_session', JSON.stringify(session)); window.location.reload(); }; function render() { var aside = document.getElementById('app-sidebar'); if (!aside) return; var page = document.body.dataset.page || ''; var cfg = window.SisprimeDB ? SisprimeDB.getConfig() : {}; var session = window.SisprimeDB ? SisprimeDB.getSession() : null; var role = (session && session.perfil) || cfg.usuario_perfil || 'vendedor'; var userName = (session && session.nombre) || cfg.usuario_nome || 'Usuario'; // Configurar o nome e avatar do usuario atual na UI var hdrName = document.getElementById('hdr-username'); var hdrAvatar = document.getElementById('hdr-avatar'); if (hdrName && hdrAvatar) { var pfx = (role==='admin')?'(A) ':(role==='gerente')?'(G) ':''; hdrName.textContent = pfx + userName; hdrAvatar.textContent = userName.charAt(0).toUpperCase(); } // Atualiza nome da empresa no header brand var brandEl = document.querySelector('.header-brand'); if (brandEl && cfg.empresa) brandEl.textContent = cfg.empresa; var logoSrc = cfg.logo_base64 || 'img/logo.png'; var empresaNome = (cfg.empresa || 'Gear Up').toUpperCase(); var html = '
' + '' + ''; aside.innerHTML = html; // Logout handler var logoutEl = document.getElementById('sidebar-logout'); if (logoutEl) { logoutEl.addEventListener('click', function(e) { e.preventDefault(); if (window.SisprimeDB) SisprimeDB.logout(); window.location.href = 'login.html'; }); } // Oculta "Cambiar Perfil" no header para usuários não-admin if (role !== 'admin') { document.querySelectorAll('[onclick*="switchRole"]').forEach(function(el) { el.style.display = 'none'; }); document.querySelectorAll('.user-menu-dropdown div, .user-menu-dropdown .user-menu-divider').forEach(function(el) { if (el.textContent.trim() === 'Cambiar Perfil') { el.style.display = 'none'; var prev = el.previousElementSibling; if (prev && prev.classList.contains('user-menu-divider')) prev.style.display = 'none'; } }); } // Search filter var si = document.getElementById('sidebar-search'); if (si) { si.addEventListener('input', function() { var q = this.value.toLowerCase().trim(); document.querySelectorAll('.nav-submenu-item, .nav-item-link').forEach(function(el) { var match = el.textContent.toLowerCase().includes(q); el.style.display = (q === '' || match) ? '' : 'none'; var grp = el.closest('.nav-group'); if (grp && q !== '' && match) grp.open = true; }); }); } } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', render); } else { render(); } })();