import router from './router'
import store from './store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken } from '@/utils/auth'
import Cookies from "js-cookie";

NProgress.configure({ showSpinner: false })

const whiteList = [
  '/index',
  '/login',
  '/auth-redirect',
  '/bind',
  '/register',
  '/news/index',
  '/news/newDetail',
  '/notice/index',
  '/supply/index',
  '/interaction/index',
  '/register/index',
  '/register/registerType',
  '/register/userRegister',
  '/register/companyRegister',
  '/biddingHall',
  '/authenticRight',
  '/authenticRight/login',
  '/project',
  '/policy',
  '/policyDetail',
  '/attestationDetail',
  '/noticeDetail',
  '/homestead/login',
  '/applicationForm',
  '/applicationList',
  '/certificateList',
  '/certificateSee',
  '/informationList',
  '/circulationList',
  '/circulationAdd',
  '/onlineHomeLogin',
  '/lawEnforcement',
  '/lawEnforcement/caseList',
  '/lawEnforcement/productsList',
  '/lawEnforcement/user',
  '/lawEnforcement/programme',
  '/lawEnforcement/programmeDetail',
  '/lawEnforcement/programmeModify',
  '/lawEnforcement/programmeApproval',
  '/lawEnforcement/complaint',
  '/lawEnforcement/reply',
  '/lawEnforcement/changePassword',
  '/lawEnforcement/feedback',
  '/lawEnforcement/feedbackForm',
  '/lawEnforcement/agreement',
  '/lawEnforcement/work',
  '/lawEnforcement/workDetail',
  '/lawEnforcement/caseDetail',
  '/lawEnforcement/login',
  '/lawEnforcement/indexComplaint',
  '/lawEnforcement/schemeDetail',
  '/lawEnforcement/waitingProcessing',
  '/lawEnforcement/complaintAdd',
  '/homesteadLogin',
  '/yinnongLogin',
  '/lawEnforcement/userRegister',
  '/lawEnforcement/companyRegister',
  '/sunVillage/login',
  //阳光村务公众号 -- 待删
  '/sunVillage/index',
  '/sunVillage/latestReport', //最新报道列表
  '/sunVillage/latestReport/details',  //最新报道详情
  '/sunVillage/importantItems',   //重大事项-列表
  '/sunVillage/importantItems/details', //重大事项-详情
  '/sunVillage/public',  //三务公开
  '/sunVillage/public/details', //三务公开-详情
  '/sunVillage/financial', //惠民金融
  '/sunVillage/financial/details', //惠民金融-详情
  '/sunVillage/financial/products', //惠民金融 - 理财产品
  '/sunVillage/financial/loan', //惠民金融 - 理财产品
  '/sunVillage/injoint', //三资公开-财务公开
  '/sunVillage/injoint/details',//三资公开-财务公开-详情
  '/sunVillage/injoint/meansOf', //三资公开-清产核资
  '/sunVillage/injoint/meansOf_desc', //三资公开-清产核资-详情页
  '/sunVillage/injoint/assetsPublic',  //三资公开-资产公开
  '/sunVillage/injoint/assetsPublic_desc', //三资公开-资产公开-详情
  '/sunVillage/injoint/publicResources', //三资公开-资源公开
  '/sunVillage/injoint/publicResources_desc', //三资公开-资源公开-详情
  '/sunVillage/consulting',   //民生直通车
  '/sunVillage/consulting/inquiries',  //民生直通车-咨询
  "/sunVillage/consulting/my_consulting",  //民生直通车-我的咨询详情
  '/sunVillage/consulting/kanban', //民生直通车-民生看板
  '/sunVillage/consulting/kanban/focusList', //民生直通车-民生看板 -关注民生list
  '/sunVillage/consulting/kanban/focusDesc', //民生直通车-民生看板 -关注民生详情
  '/sunVillage/consulting/kanban/affairsList', //民生直通车-民生看板 - 民生办事list
  '/sunVillage/equity', //股权查询-股民信息
  '/sunVillage/equity_desc', // 股权查询-股民信息-详情
  '/sunVillage/shareBonus', //股权查询-股权分红
  '/sunVillage/shareBonus_num', //股权查询-股权分红-股数详情页
  '/sunVillage/shareBonus_amount', //股权查询-股权分红-分红详情页
  '/sunVillage/user/index',  //个人中心
  '/sunVillage/user/mobile', //个人中心-修改手机号
  '/sunVillage/user/information', //个人中心-修改用户信息
  '/sunVillage/user/cooperative',  //个人中心-修改所属合作社
  //阳光村务公众号 -- 待删

  //宅基地
  '/zjdLogin',

  //阳光村务--新
  '/sunVillage_info/login',  //登录
  '/sunVillage_info/index',  //首页
  '/sunVillage_info/list_tourists',  //二页列表-游客
  '/sunVillage_info/fixedAssets', //固定资产
  '/sunVillage_info/information',  //合同信息
  '/sunVillage_info/details',   //详情页
  '/sunVillage_info/index_code',  //详情页
  '/sunVillage_info/list_finance',   //详情页
  '/sunVillage_info/list_issues',   //详情页
  '/sunVillage_info/list_tourists_detail',   //详情页
  '/sunVillage_info/list_issues_detail',   //详情页
  '/sunVillage_info/list_finance_detail',   //详情页
  '/sunVillage_info/list_finance_ranking',   //详情页
  '/sunVillage_info/list_tourists_ranking',   //详情页
  '/sunVillage_info/login_code',   //详情页
  '/sunVillage_info/index_code_rights',   //详情页
  '/sunVillage_info/list_vote',   //详情页
  '/sunVillage_info/list_vote_detail',   //详情页
  '/sunVillage_info/list_vote_form',   //详情页
]

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    /* has token*/
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done()
    } else {
      if (store.getters.roles.length === 0) {
        next()
        // 判断当前用户是否已拉取完user_info信息
        store.dispatch('GetInfo').then(res => {
          // 拉取user_info
          const roles = res.roles
          // store.dispatch('GenerateRoutes', { roles }).then(accessRoutes => {
          //   // 根据roles权限生成可访问的路由表
          //   router.addRoutes(accessRoutes) // 动态添加可访问路由表
          //   next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
          // })
          next()
        }).catch(err => {
          store.dispatch('LogOut').then(() => {
            try {
              let loginUrl = Cookies.get("_Login_url");
              if (loginUrl && loginUrl.indexOf("onlineHomeLogin") !== -1) {
                next({ path: '/onlineHomeLogin' })
                return;
              }
            } catch (e) { console.log(e); }
            // Message.error(err)
            if ('/authenticRight/index'.indexOf(to.path) !== -1) {
              next({ path: '/authenticRight/login' })
            } else if ('/homestead/index'.indexOf(to.path) !== -1) {
              next({ path: '/homestead/login' })
            } else if (to.path.indexOf('/onlineHome') !== -1) {
              //next(`/onlineHomeLogin`)
              next(`/zjdLogin`)
            } else if (to.path.indexOf('/yinnong') !== -1) {
              next(`/yinnongLogin`)
            } else if (window.location.href.indexOf('/sunVillage_info/') != -1) {
              window.location.href = '/sunVillage_info/login';
            } else if (to.path.indexOf('/homesteadSurvey') !== -1) {
              next(`/homesteadLogin`)
            } else if (to.path.indexOf('/lawEnforcement') !== -1) {
              next(`//lawEnforcement/login`)
            } else if (to.path.indexOf('/sunVillage') !== -1) {
              next(`/sunVillage/login`)
            } else if (to.path.indexOf('/homestead/') !== -1) {
              next(`/homestead/login`)
            } else {
              next(`/yinnongLogin`)
              //next({ path: '/' })
            }
          })
        })
      } else {
        next()
      }
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      console.log(to.path)
      next()
    } else {
      try {
        let loginUrl = Cookies.get("_Login_url");
        if (loginUrl && loginUrl.indexOf("onlineHomeLogin") !== -1) {
          next({ path: `/onlineHomeLogin?redirect=${to.fullPath}` })
          return;
        }
      } catch (e) { console.log(e); }
      if ('/authenticRight/index'.indexOf(to.path) !== -1) {
        next(`/authenticRight/login?redirect=${to.fullPath}`)
      } else if ('/homestead/index'.indexOf(to.path) !== -1) {
        next(`/homestead/login?redirect=${to.fullPath}`)
      } else if (to.path.indexOf('/onlineHome') !== -1) {
        next(`/onlineHomeLogin`)
      } else if (to.path.indexOf('/yinnong') !== -1) {
        next(`/yinnongLogin`)
      } else if (window.location.href.indexOf('/sunVillage_info/') != -1) {
        window.location.href = '/sunVillage_info/login';
      } else if (to.path.indexOf('/homesteadSurvey') !== -1) {
        next(`/homesteadLogin`)
      } else if (to.path.indexOf('/lawEnforcement') !== -1) {
        next(`/lawEnforcement/login?redirect=${to.fullPath}`)
      } else if (to.path.indexOf('/sunVillage') !== -1) {
        next(`/sunVillage/login`)
      } else {
        next(`/login?redirect=${to.fullPath}`)
      }

      //  // 否则全部重定向到登录页
      //next('/index');
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  NProgress.done()
})
