import { createRouter, createWebHistory } from 'vue-router'
import {CookieUtils} from "@/utils/CookieUtils.ts";
import {userStore} from "@/stores/user.ts";
//import {menuStore} from "@/stores/menu.ts";

//const noPermissionRouterName = ['main','home','error'];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: 'login' },
    { path: '/login', name: 'login', component: () => import('@/views/layout/LoginView.vue') },
    { path: '/printCode', name: 'printCodePage', component: () => import('@/views/layout/PrintCodePage.vue') },
    { path: '/home', name: 'homePage', component: () => import('@/views/layout/HomeViewPage.vue') },
    { path: '/wcs', name: 'main', component: () => import('@/views/layout/MainView.vue'),
      children: [
        { path: 'home', name: 'home', component: () => import('@/views/tabs/HomeView.vue') },
        { path: 'error', name: 'error', component: () => import('@/views/tabs/Error404View.vue') },
        { path: 'menuManage', name: 'menuManage', component: () => import('@/views/tabs/MenuManage.vue') },
        { path: 'userGroupManage', name: 'userGroupManage', component: () => import('@/views/tabs/UserGroupManage.vue') },
        { path: 'userManage', name: 'userManage', component: () => import('@/views/tabs/UserManage.vue') },
        { path: 'localUserManage', name: 'localUserManage', component: () => import('@/views/tabs/LocalUserManage.vue') },
        { path: 'configManage', name: 'configManage', component: () => import('@/views/tabs/ConfigManage.vue') },
        { path: 'quartzManage', name: 'quartzManage', component: () => import('@/views/tabs/QuartzManage.vue') },
        { path: 'stockComposeTask', name: 'stockComposeTask', component: () => import('@/views/tabs/StockComposeTask.vue') },
        { path: 'stockComposeTaskBak', name: 'stockComposeTaskBak', component: () => import('@/views/tabs/StockComposeTaskBak.vue') },
        { path: 'plcManage', name: 'plcManage', component: () => import('@/views/tabs/PlcManage.vue') },
        { path: 'stackerManage', name: 'stackerManage', component: () => import('@/views/tabs/StackerManage.vue') },
        { path: 'stackerLocationManage', name: 'stackerLocationManage', component: () => import('@/views/tabs/StackerLocationManage.vue') },
        { path: 'stockSingleTask', name: 'stockSingleTask', component: () => import('@/views/tabs/StockSingleTask.vue') },
        { path: 'stockSingleTaskBak', name: 'stockSingleTaskBak', component: () => import('@/views/tabs/StockSingleTaskBak.vue') },
        { path: 'trayConveyManage', name: 'trayConveyManage', component: () => import('@/views/tabs/TrayConveyManage.vue')},
        { path: 'conveyLocationManage', name: 'conveyLocationManage', component: () => import('@/views/tabs/ConveyLocationManage.vue')},
        { path: 'stockScanManage', name: 'stockScanManage', component: () => import('@/views/tabs/StockScanManage.vue') },
        { path: 'recordScan', name: 'recordScan', component: () => import('@/views/tabs/RecordScan.vue') },
        { path: 'recordApiRequest', name: 'recordApiRequest', component: () => import('@/views/tabs/RecordApiRequest.vue') },
        { path: 'recordApiResponse', name: 'recordApiResponse', component: () => import('@/views/tabs/RecordApiResponse.vue') },
        { path: 'apiInfoManage', name: 'apiInfoManage', component: () => import('@/views/tabs/ApiInfoManage.vue') },
        { path: 'printCode', name: 'printCode', component: () => import('@/views/tabs/PrintCode.vue') },
        { path: 'conveyTask', name: 'conveyTask', component: () => import('@/views/tabs/ConveyTask.vue') },
        { path: 'conveyTaskBak', name: 'conveyTaskBak', component: () => import('@/views/tabs/ConveyTaskBak.vue') },
        { path: 'conveyPickStand', name: 'conveyPickStand', component: () => import('@/views/tabs/ConveyPickStand.vue')},
        { path: 'conveyCheckStand', name: 'conveyCheckStand', component: () => import('@/views/tabs/ConveyCheckStand.vue')},
        { path: 'conveyDeliverStand', name: 'conveyDeliverStand', component: () => import('@/views/tabs/ConveyDeliverStand.vue')},
        { path: 'baseError', name: 'baseError', component: () => import('@/views/tabs/ErrorBase.vue')},
        { path: 'equipmentError', name: 'equipmentError', component: () => import('@/views/tabs/RecordEquipmentError.vue')},
        { path: 'ledManage', name: 'ledManage', component: () => import('@/views/tabs/LedBaseManage.vue')},
        { path: 'fileManage', name: 'fileManage', component: () => import('@/views/tabs/FileManage.vue')},
        { path: 'siemensDbManage', name: 'siemensDbManage', component: () => import('@/views/tabs/SiemensDbManage.vue')},
        { path: 'etagController', name: 'etagController', component: () => import('@/views/tabs/EtagController.vue')},
        { path: 'etagTag', name: 'etagTag', component: () => import('@/views/tabs/EtagTag.vue')},
        { path: 'etagLocation', name: 'etagLocation', component: () => import('@/views/tabs/EtagLocation.vue')},
        { path: 'etagTask', name: 'etagTask', component: () => import('@/views/tabs/EtagTask.vue')},
        { path: 'etagTagBak', name: 'etagTagBak', component: () => import('@/views/tabs/EtagTaskBak.vue')},
      ] }

  ],
})

router.beforeEach((to, from, next) => {
  if(to.path === '/login' || to.path === '/printCode' || to.path === '/home') {
    new CookieUtils().clear();
    next();
    return;
  }

  const token = new CookieUtils().getItem('wcsToken');
  const userStoreInstance = userStore();
  //const menuStoreInstance = menuStore();
  //const toName = to.name?.toString();
  // if (token && userStoreInstance.getUserInfo().userId != null
  //     && (menuStoreInstance.getUserMenuWithRouterName(toName) || noPermissionRouterName.find(f=>f.toString() == toName))) {
  if (token && userStoreInstance.getUserInfo().userId != null) {
    next();
    return;
  }
  router.push('/login').then(r => {});
})

export default router
