import { useState, useEffect } from "react"; import { Link, Outlet, useLocation } from "react-router-dom"; import { API_URL, getToken, getUsername, getCompany } from "../utils/api"; import Icon from "../components/ui/Icon"; // Estructura del menú tipada para SAP Business One interface SubMenuItem { name: string; path: string; } interface MenuItem { title: string; icon: string; subItems: SubMenuItem[]; } const menuConfig: MenuItem[] = [ { title: "0. Dashboard", icon: "chart", subItems: [{ name: "Inicio", path: "/dashboard" }], }, { title: "1. Inventario", icon: "box", subItems: [ { name: "Articulos", path: "/inventario/articulos" }, { name: "Lotes", path: "/inventario/lotes" }, { name: "Inmovilizar Lotes", path: "/inventario/inmovilizar-lotes" }, { name: "Liberar Lotes", path: "/inventario/liberar-lotes" }, { name: "Confirmar Liberación Lote", path: "/inventario/confirmar-liberacion-lote" }, ], }, { title: "2. Socio de negocios", icon: "users", subItems: [ { name: "Clientes", path: "/socios/clientes" }, { name: "Proveedores", path: "/socios/proveedores" }, ], }, { title: "3. Ventas", icon: "cart", subItems: [ { name: "Ordenes de venta", path: "/ventas/ordenes" }, { name: "Entregas o Guías", path: "/ventas/entrega" }, { name: "Facturas de venta", path: "/ventas/facturas" }, ], }, { title: "4. Compras", icon: "briefcase", subItems: [ { name: "Ordenes de compra", path: "/compras/ordenes" }, { name: "Recepciones de compras", path: "/compras/recepcion" }, { name: "Facturas de proveedores", path: "/compras/facturas" }, ], }, { title: "5. Informes", icon: "chart", subItems: [ { name: "Socios de negocios", path: "/informes/socios" }, { name: "Ventas", path: "/informes/ventas" }, { name: "Compras", path: "/informes/compras" }, { name: "Inventario", path: "/informes/inventario" }, ], }, { title: "6. Reportes Crystal", icon: "chart", subItems: [ { name: "Socios de negocios", path: "/reportes-crystal/socios" }, { name: "Ventas", path: "/reportes-crystal/ventas" }, { name: "Compras", path: "/reportes-crystal/compras" }, { name: "Inventario", path: "/reportes-crystal/inventario" }, ], }, { title: "7. Reportes HTML", icon: "tag", subItems: [ { name: "Socios de negocios", path: "/reportes-html/socios" }, { name: "Ventas", path: "/reportes-html/ventas" }, { name: "Compras", path: "/reportes-html/compras" }, { name: "Inventario", path: "/reportes-html/inventario" }, ], }, ]; export default function MainLayout() { const location = useLocation(); const [isSidebarOpen, setIsSidebarOpen] = useState(true); const [currentTheme, setCurrentTheme] = useState( () => localStorage.getItem("sap_theme") || "theme-default", ); const [openModules, setOpenModules] = useState>({ "1. Inventario": true, "2. Socio de negocios": true, "5. Informes": true, }); const [allowedPaths, setAllowedPaths] = useState(null); const [permissionsError, setPermissionsError] = useState(false); useEffect(() => { const fetchPermissions = async () => { const token = getToken(); if (!token) return; try { const res = await fetch(`${API_URL}/api/user/menu`, { headers: { "X-Session-Token": token }, }); if (!res.ok) throw new Error("No se pudo cargar el menú"); const data = await res.json(); setAllowedPaths(data.allowedPaths); } catch { setPermissionsError(true); } }; fetchPermissions(); }, []); useEffect(() => { document.documentElement.className = currentTheme; localStorage.setItem("sap_theme", currentTheme); }, [currentTheme]); const pathMatches = (path: string) => { if (!allowedPaths) return false; return allowedPaths.some( (p) => p === "*" || (p.endsWith("/*") && path.startsWith(p.slice(0, -2))) || p === path, ); }; const toggleModule = (title: string) => { setOpenModules((prev) => ({ ...prev, [title]: !prev[title] })); }; const username = getUsername(); const company = getCompany(); const handleLogout = async () => { const token = getToken(); if (token) { try { await fetch(`${API_URL}/api/Logout`, { method: "POST", headers: { "X-Session-Token": token }, }); } catch { /* ignore */ } } localStorage.removeItem("sap_session"); window.location.href = "/login"; }; return (
{/* Menu lateral (Sidebar) */} {/* Contenedor principal */}
{/* Barra superior (TopBar Flat) */}
SAP Web Client — {company || "SAP Business One"}
Usuario: {username || "manager"}
{/* Espacio de trabajo (Renderiza cada submódulo) */}
); }