import router from '@/router'
import { RouteLocationNormalized } from 'vue-router'
import { useStore } from './store'
import { UserActionTypes } from './store/modules/user/action-types'
// import { getUrlaccessToken } from '@/utils/https'
import { getToken, setCookieMac, setToken } from '@/utils/cookies'
import { Toast } from 'vant'
import Cookies from 'js-cookie'
import { getUrlParams } from './utils/https'

router.beforeEach(async (to: RouteLocationNormalized, _: RouteLocationNormalized, next: any) => {
  const store = useStore()
  let macAdr: string = getUrlParams('mac')
  if (macAdr && macAdr != "null" && macAdr != "NULL") {
    setCookieMac(macAdr)
    localStorage.setItem('mac', macAdr)
  }
  await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined)
  next()
  // const store = useStore()
  // const accessToken: any = getUrlaccessToken()
  // if (accessToken) {
  //   setToken(accessToken)
  // }
  // if (process.env.VUE_APP_BASE_API !== '/borui/') {
  // if (to.path === '/home') {
  //   next()
  // } else {
  //   if (getToken()) {
  //     if (store.state.user.userId) {
  //       next()
  //     } else {
  //       await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined)
  //       next()
  //     }
  //   } else {
  //     sessionStorage.clear()
  //     const tokenOld: any = Cookies.get('accessToken')
  //     if (to.path !== '/home') {
  //       window.location.href = `/sgh5/home?accessToken=${tokenOld}`
  //     }
  //     Toast('登录失效，请退出app重新登录')
  //   }
  // }
  // } else {
  //   if (store.state.user.userId) {
  //     next()
  //   } else {
  //     await store.dispatch(UserActionTypes.ACTION_GET_USER_INFO, undefined)
  //     next()
  //   }
  // }
})

router.afterEach((to: RouteLocationNormalized) => {
  // console.log(to)
  // Finish progress bar
  // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939

  // set page title
  // document.title = getPageTitle(to.meta.title)
})
