import Vue from 'vue'
import Cookies from 'js-cookie'
import router from './router'
import store from './store'
import request from '@/utils/request'
import {getToken, setToken, cookGetToken} from '@/utils/auth'
const whiteList = ['/login', '/reg', '/pwdforget'] // no redirect whitelist
import { wxAuth } from "@/utils/wx-auth-two.js";
router.beforeEach((to, from, next) => {
  if (Vue.$httpRequestList.length > 0) { //强行中断时才向下执行
    Vue.$httpRequestList.forEach(item => {
      item('interrupt'); //给个标志，中断请求
    })
  }
  if(to.path == '/noAccess' || to.path == '/login' || to.path == '/'){ 
    next()
    return
 }

  if (cookGetToken()){//Cook中A-Token
    setToken(cookGetToken())
    detailAxiox()//员工详细信息
    // Cookies.remove('Admin-Token')
    Cookies.remove('Admin-Token')
    thens(to, from, next)
  } else if (getToken()) {//local中A-Token
    console.log('无cook，走localStorage')
    thens(to, from, next)
  } else {
      if (!window.$routes_map[to.name].need_login) { // 在免登录白名单，直接进入
        if(to.name=='init'&&localStorage.getItem('init')){
          // window.document.title = '用户登录'
          next('/login')
        }else{
          if (typeof(window.$routes_map[to.name]) != 'undefined') {
            window.document.title = window.$routes_map[to.name].label
          }
          next()
        }
      } else {
        // window.document.title = '用户登录'
        next('/login')
      }
  }
})

function thens(to, from, next){
  // if (to.path == '/login'||to.name == 'init') { // 在免登录白名单，直接进入
  //   next({ name: 'home'})
  //   return false
  // }
  if (typeof(window.$routes_map[to.name]) != 'undefined') {
    window.document.title = window.$routes_map[to.name].label
  } else {
    // window.document.title = '工作台'
  }
  console.log(window.$routes_map)
  
  console.log('跳转后的path：')
  console.log(to.path)
  console.log('跳转前的path：')
  console.log(from.path)
  //首页按返回到登录 或 初始化时--直接退出
  if(to.path == '/login' && from.path == '/home' || to.path == '/init' && from.path == '/home' || to.path == '/' && from.path == '/home'){
    wx.closeWindow();//关闭当前网页窗口
    return false
  }
  
  if (to.path == '/login' || to.name == 'init') { // 在免登录白名单，直接进入
    next({ name: 'home'})
  }
  // 初始一些数据
  init(to);
  // next();
  console.log('wxAuth12121221121221888')
  console.log(router.history.current.name)
  wxAuth(false,next,to)

}

function init(to) {
  store.dispatch('get_user_info', getToken()).then((res) => { //获取用户信息
    if (router.history.current.name == 'login' && to.name != 'home') {
      router.push({
        name: 'home'
      })
      return false
    }
    if (store.getters.user_info.try_user == 1) { //判断是否试用账号
      store.dispatch("setBuyPopup",true);
    }else{
       store.dispatch("setBuyPopup",false);
    }
  });
  store.dispatch('get_point_types').then(res => {}); //获取积分类型
  // 数据更新，每两分钟更新一次
  store.dispatch('get_employee_map').then((res) => {}); //获取人员列表
  // store.dispatch('get_account_info').then((res) => {}); //获取公司信息

}

function detailAxiox(){
  request('get',process.env.BASE_API+'/api/employee/detail')
  .then(res => {
    if(res.data.code == 1){
      store.commit("SET_USERINFO", res.data.data.user); // 设置员工信息
      window.sessionStorage.setItem('__VCKEEPALIVE__', JSON.stringify(['/']))
      window.sessionStorage.setItem('routers', '/')
      console.log(getToken());
      console.log(JSON.parse(localStorage.getItem('user_info')))
      console.log(store.getters.user_info)
    }
  })
}
