import { createRouter, createWebHashHistory } from 'vue-router'
import MainLayout from '@/layouts/MainLayout.vue'
import { useUserStore } from "@/stores/user.js";

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
    meta: { nopermission: true }
  },
  {
    path: '/',
    name: '首页',
    redirect: '/homePage',
    component: MainLayout,
    children: [
      {
        path: '/homePage',
        name: 'dataSummary',
        title: '数据大屏',
        component: () => import('@/views/homePage/index.vue'),
        meta: { menuName: '数据大屏', icon: 'platform' }
      },
      {
        path: '/projectManage',
        name: 'projectManage',
        title: '项目管理',
        meta: { menuName: '项目管理', icon: 'Management' },
        redirect: "/projectAllPage",
        children: [
          {
            path: '/projectDraft',
            name: 'projectDraft',
            title: '项目遴选',
            component: () => import('@/views/managePage/projectDraft.vue'),
            meta: { menuName: '项目遴选' }
          },
          {
            path: '/projectSetUp',
            name: 'projectSetUp',
            title: '项目立项',
            component: () => import('@/views/managePage/projectSetUp.vue'),
            meta: {
              menuName: '项目立项'
            }
          },
          {
            path: '/projectArgument',
            name: 'projectArgument',
            title: '项目论证',
            component: () => import('@/views/managePage/projectArgument.vue'),
            meta: {
              menuName: '项目论证'
            }
          },
          {
            path: '/projectDecision',
            name: 'projectDecision',
            title: '项目决策',
            component: () => import('@/views/managePage/projectDecision.vue'),
            meta: {
              menuName: '项目决策'
            }
          },
          {
            path: '/projectAllPage',
            name: 'projectAllPage',
            title: '项目档案库',
            component: () => import('@/views/managePage/projectAllPage.vue'),
            meta: { menuName: '项目档案库' }
          },
          {
            path: '/addProject/:type',
            name: 'addProject',
            title: '新增项目',
            component: () => import('@/views/managePage/addProject.vue'),
            meta: {
              menuName: '新增项目',
              showInMenu: false // 不在菜单中显示
            }
          }
        ]
      },
      {
        path: '/templateManage',
        name: 'templateManage',
        title: '模板管理',
        component: () => import('@/views/managePage/templateManage.vue'),
        meta: { menuName: '模板管理', icon: 'Management' }
      },
      {
        path: '/systemManage',
        name: '系统管理',
        title: 'systemManage',
        meta: { menuName: '系统管理', icon: 'tools' },
        children: [
          {
            path: '/systemManage/departManage',
            name: '部门管理',
            title: 'departManage',
            component: () => import('@/views/systemManage/departManage.vue'),
            meta: { menuName: '部门管理',}
          },
          {
            path: '/systemManage/userManage',
            name: '用户管理',
            title: 'userManage',
            component: () => import('@/views/systemManage/userManage.vue'),
            meta: { menuName: '用户管理',}
          },
          {
            path: '/systemManage/roleManage',
            name: '角色管理',
            title: 'roleManage',
            component: () => import('@/views/systemManage/roleManage.vue'),
            meta: { menuName: '角色管理',}
          },
          {
            path: '/systemManage/menuManage',
            name: '菜单管理',
            title: 'menuManage',
            component: () => import('@/views/systemManage/menuManage.vue'),
            meta: { menuName: '菜单管理',}
          }
        ]
      },
      {
        path: "/building",
        name: "building",
        title: "建设中",
        component: () => import("@/views/homePage/building.vue")
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 路由守卫 - 登录验证
router.beforeEach((to, from, next) => {
  const userStore = useUserStore();
  let token = userStore.authToken || sessionStorage.getItem("authToken") || "";
  if (!token) {
    if (to.path === '/login' || to.meta.nopermission) {
      next();
    } else {
      next('/login')
    }
  } else {
    if (to.path === "/login") {
      next("/");
    } else {
      next();
    }
  }
})

export default router
