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',
  '/notice/inviteTendersDetail',
  '/notice/winTheBiddingDetail',
  '/project/projectNeedsDetail',
  '/chungeng/userSSOLogin',
  '/chungeng/farmerSSOLogin',

  '/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',

  //宅基地
  '/zjdLogin',
  '/onlineHome/workbench',

  //阳光村务--新
  '/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_photo',   //一张图公开
  '/sunVillage_info/list_contract',   //合同公开
  '/sunVillage_info/list_contract_detail',   //合同公开详情页
  '/sunVillage_info/list_tourists_detail',   //详情页
  '/sunVillage_info/list_issues_detail',   //详情页
  '/sunVillage_info/list_finance_detail',  //详情页
  '/sunVillage_info/list_photo_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',   //详情页
  '/sunVillage_info/suishoupai',   //随手拍
  '/sunVillage_info/list_complaint',   //详情页
  '/sunVillage_info/list_complaint_add',   //详情页
  '/sunVillage_info/list_complaint_detail',   //详情页
  '/sunVillage_info/list_message',   //详情页
  '/sunVillage_info/list_message_add',   //详情页
  '/sunVillage_info/list_message_detail',   //详情页
  '/sunVillage_info/homeApplication/applicationList',   //列表页面
  '/sunVillage_info/proposerLite',
  '/sunVillage_info/paidExit/paidExitList',   //列表页面
  '/sunVillage_info/paidExitAdd',
  '/sunVillage_info/paidExitModify',
  '/sunVillage_info/paidExitDetail',
  '/sunVillage_info/arbitrationList',
  '/sunVillage_info/arbitrationDetail',
  '/sunVillage_info/familyMember',
  '/sunVillage_info/familyMemberDetail',
  '/sunVillage_info/list_cbfsq',
  '/sunVillage_info/list_signature',
  '/sunVillage_info/list_official',
  '/sunVillage_info/list_signature_add',
  '/sunVillage_info/list_multipleLotsNh',
  '/sunVillage_info/list_multipleLotsNh_detail',
  '/sunVillage_info/list_cbf',
  '/sunVillage_info/list_cbht_detail',
  '/sunVillage_info/list_resources_ranking',
  '/sunVillage_info/list_property_ranking',
  '/sunVillage_info/list_contract_ranking',

  // 新型经营主体
  'newBusinessEntity/newsBulletin', //新闻公告
  '/newBusinessEntity/index',      //首页
  '/newBusinessEntity/login',      //登录
  '/newBusinessEntity/register',   //注册
  '/newBusinessEntity/conter',  //底部组件
  '/newBusinessEntity/newsDetails',  //新闻详情
  '/newBusinessEntity/listBody',  //主体名录
  '/newBusinessEntity/map',      //地图

  //农业大托管
  '/agriculturalTrusteeship/index',//首页
  '/agriculturalTrusteeship/login',//登录
  '/agriculturalTrusteeship/insuranceList',//保险列表
  '/agriculturalTrusteeship/insuranceProjectList',//保险产品列表
  '/agriculturalTrusteeship/insuranceProjectDetail',//保险产品详情
  '/agriculturalTrusteeship/societyList',//社会化列表
  '/agriculturalTrusteeship/societyProjectList',//社会化产品列表
  '/agriculturalTrusteeship/societyProjectDetail',//社会化产品详情
  '/agriculturalTrusteeship/shopList',//保险列表首页
  '/agriculturalTrusteeship/shopNeedsList',//保险列表首页
  '/agriculturalTrusteeship/shopOrganizationDetail',//保险列表首页
  '/agriculturalTrusteeship/shopDepositDetail',//保险列表首页
  '/agriculturalTrusteeship/shopDetail',//保险列表详情
  '/agriculturalTrusteeship/shopEdit',//保险列表详情
  '/agriculturalTrusteeship/shopAdd',//保险列表首页
  '/agriculturalTrusteeship/serviceEdit',//保险列表首页
  '/agriculturalTrusteeship/socializationIndex',//社会化服务组织待处理
  '/agriculturalTrusteeship/socializationProject',//社会化服务组织产品
  '/agriculturalTrusteeship/socializationProjectDetail',//社会化服务组织产品详情
  '/agriculturalTrusteeship/socializationProjectEdit',//社会化服务组织产品修改
  '/agriculturalTrusteeship/socializationBill',//社会化服务组织订单
  '/agriculturalTrusteeship/socializationBillDetail',//社会化服务组织订单详情
  '/agriculturalTrusteeship/socializationBillOrder',//社会化服务组织订单详情
  '/agriculturalTrusteeship/socializationUser',//社会化服务组织个人中心
  '/agriculturalTrusteeship/socializationUserEdit',//社会化服务组织个人信息
  '/agriculturalTrusteeship/socializationRelease',//社会化服务组织发布产品
  '/agriculturalTrusteeship/socializationDraft',//社会化服务组织草稿箱
  '/agriculturalTrusteeship/socializationDraftEdit',//社会化服务组织草稿箱修改

  //'/agriculturalTrusteeship/buyerIndex',//买家选购
  //'/agriculturalTrusteeship/buyerWaitBill',//买家选购-等待处理
  // '/agriculturalTrusteeship/buyerAllBill',//买家选购-所有订单
  // '/agriculturalTrusteeship/placeOrder',//买家选购-下单页
  // '/agriculturalTrusteeship/billDetail',//买家选购-订单详情
  // '/agriculturalTrusteeship/billDetail2',//买家选购-订单详情
  // '/agriculturalTrusteeship/billDetail3',//买家选购-订单详情
  // '/agriculturalTrusteeship/billDetail4',//买家选购-订单详情
  // '/agriculturalTrusteeship/evaluate',//买家选购-服务评价

  '/plotPremisesMobile/index',//闲置资源


  //承包地确权
  '/contracted/login',//登录
  '/contracted/index',//首页

]

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'.indexOf(to.path) !== -1) {
              next({ path: '/authenticRight/login' })
            } else if ('/homestead'.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 {
              next(`/yinnongLogin`)
              //next({ path: '/' })
            }
          })
        })
      } else {
        next()
      }
    }
  } else {
    // 没有token
    //console.info(to);
    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()
})
