import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'
//加载进度条
NProgress.configure({ showSpinner: false });
// no redirect whitelist  白名单
const whiteList = ['/login'];

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start();
  // set page title
  // 动态获取 id
  const id = to.params.id

  // 根据 id 设置 title
  if (to.name === 'imagetextToImage' && id) {
    let title = id == 1 ? '创意生图' : (id == 2 ? '图生图' : '线稿生成')
    to.meta.title = title
    document.title = getPageTitle(title)
  } else {
    document.title = getPageTitle(to.meta.title)
  }
  // determine whether the user has logged in
  const hasToken = getToken();

  

  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' });
      NProgress.done()
    } else {
      const hasRoles = store.getters.roles && store.getters.roles.length > 0;
      if (hasRoles) {
        next();
      } else {
        try {
          const res = await store.dispatch('user/getUserInfo');
          // const data = res.data.roleCodeList;
          const data = [
            { routerPath: "/auth" },
            { routerPath: "/auth/user" },
            { routerPath: "/auth/role" },
            { routerPath: "/auth/orgGroup" }, 
            { routerPath: "/auth/position" },
            { routerPath: "/auth/changePassword" },
            { routerPath: "/auth/org" },
            { routerPath: "/auth/res" },
            { routerPath: "/product" },
            { routerPath: "/catwalkVideo" },
            { routerPath: "/catwalkVideo/catwalkVideoIndex" },
            { routerPath: "/catwalkVideo/catwalkVideoTool" },
            { routerPath: "/catwalkVideo/brandVideoEditor" },
            { routerPath: "/videoCoverManager" },
            { routerPath: "/videoCoverManager/videoCoverIndex" },
            { routerPath: "/videoCoverManager/faceSwapVideoIndex" },
            { routerPath: "/designAgent" },
            { routerPath: "/designAgent/designAgentIndex" },
            { routerPath: "/designAgent/chatCreation" },
            { routerPath: "/designAgent/imagetextToImage" },
            { routerPath: "/musicLibraryManager" },
            { routerPath: "/musicLibraryManager/musicCategoryIndex" },
            { routerPath: "/musicLibraryManager/musicLibrary" },
            { routerPath: "/faceSwapVideo" },
            { routerPath: "/faceSwapVideo/faceSwapVideoIndex" },
            { routerPath: "/faceSwapVideo/faceSwapVideoTool" },
            { routerPath: "/faceSwapVideo/faceSwapImageTool" },
            { routerPath: "/oralVideo" },
            { routerPath: "/oralVideo/oralVideoIndex" },
            { routerPath: "/AIAssetSearch" },
            { routerPath: "/AIAssetSearch/AIAssetSearchIndex" },
            { routerPath: "/trendingTermList" },
            { routerPath: "/trendingTermList/trendingTermListIndex" },
            { routerPath: "/productRepo" },
            { routerPath: "/productRepo/productRepoIndex" },
            { routerPath: "/cropList" },
            { routerPath: "/cropList/cropListIndex" },
            { routerPath: "/AiHotVideoList"},
            { routerPath: "/AiHotVideoList/AiHotVideoListIndex"},
            { routerPath: "/AiProductSearch"},
            { routerPath: "/AiProductSearch/AiProductSearchIndex"}]
         
          const accessRoutes = await store.dispatch('permission/generateRoutes', data);
          router.addRoutes(accessRoutes);
          next({ ...to, replace: true })
        } catch (error) {
          await store.dispatch('user/resetToken');
          Message.error(error || 'Has Error');
          next(`/login?redirect=${to.path}`);
          NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login?redirect=${to.path}`);
      NProgress.done()
    }
  }
});

router.afterEach(() => {
  // 关闭进度条
  NProgress.done();
});
