import router from './router'
import store from './store'
import i18n from "./lang"; // internationalization
import {
  Dialog,
} from "vant";
// 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 }) // NProgress Configuration

const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
// '/home',
const navbarLsit = ['/home', '/news', '/shopping', '/personal'] //显示navbar的页面

//需要token的名单
const demandTokenList = ['/news', '/shopping', '/personal','/toBePaid','/confirmOrder','/linePayConfirm','/linePayCancel']

router.beforeEach(async (to, from, next) => {
  // start progress bar
  // NProgress.start()
  // set page title
  // document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  if (true) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      // next({ path: '/' })
      next()
      // NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
    } else {
      const hasToken = getToken()
      // 确定用户是否已通过getInfo获得用户信息
      if (hasToken) {
        if (!store.getters.userId) {
          // 获取用户信息
          store.dispatch('user/GetInfo').then(() => {})
        }
      }
      // if (hasRoles) {}

      // 获取用户信息
      // this.$store.dispatch('user/GetInfo').then(() => {

      // })
      if (demandTokenList.indexOf(to.path) === -1) {
        next()
      } else {
        if (hasToken) {
          // //这里控制个人订单不可回退到确认订单
          if(from.path=='/toBePaid'&&(to.path=='/linePayConfirm' || to.path=='/linePayCancel' || to.path=='/confirmOrder')){
            next(`/personal`)
          }else{
            next()
          }
          //next()
        } else {
          Dialog
            .confirm({
              title: i18n.t('api.tips'),
              message: i18n.t('api.confirmTips'),
              confirmButtonText: i18n.t('api.confirm'),
              cancelButtonText: i18n.t('api.cancel'),
            })
            .then(() => {
              next(`/login`)
            })
            .catch(() => {});
        }
        try {
          // // get user info
          // // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
          // const { sysMenus } = await store.dispatch('user/getInfo')

          // // generate accessible routes map based on roles
          // const accessRoutes = await store.dispatch('permission/generateRoutes', sysMenus)

          // // dynamically add accessible routes
          // router.addRoutes(accessRoutes)
          // // hack method to ensure that addRoutes is complete
          // // set the replace: true, so the navigation will not leave a history record
          // next({ ...to, replace: true })
        } catch (error) {
          // // remove token and go to login page to re-login
          // await store.dispatch('user/resetToken')
          // // Message.error('登录超时请重新登录')
          // console.log(error,'++++')
          // next(`/login?redirect=${to.path}`)
          // // NProgress.done()
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      // ?redirect=${to.path}`
      next(`/login`)
      // NProgress.done()
    }
  }
})

router.afterEach((to) => {
  //这里控制底部导航的展示
  if (navbarLsit.indexOf(to.path) !== -1) {
    store.commit("app/TOOGLE_NAVBAR_SHOW", true);
    let navNum = null;
    switch (to.path) {
      case navbarLsit[0]:
        navNum = 1;
        break;
      case navbarLsit[1]:
        navNum = 2;
        break;
      case navbarLsit[2]:
        navNum = 3;
        break;
      case navbarLsit[3]:
        navNum = 4;
        break;
    }
    store.commit("app/TOOGLE_NAVBAR_TYPE", navNum);
  } else {
    store.commit("app/TOOGLE_NAVBAR_SHOW", false);
  }
  // finish progress bar
  // NProgress.done()
})