import Vue from 'vue'
import Router from 'vue-router'
import login from '@/components/login'
import local from '../extend/js/localStorage'
Vue.use(Router)

let router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/home',
      name: 'home',
      redirect: '/order/statistics',
      component: resolve => require(['@/components/home'], resolve)
    },
    {
      path: '/r',
      name: 'openrefund',
      component: resolve => require(['@/components/order/openrefunds'], resolve)
    },
    {
      path: '/',
      name: '',
      redirect: 'home/blank',
      component: resolve => require(['@/components/common/layout/index'], resolve),
      children: [
        // 默认
        {
          path: 'home/blank',
          name: 'users',
          component: resolve => require(['@/components/home/blank'], resolve)
        },
        // 用户
        {
          path: 'user/index',
          name: 'users',
          component: resolve => require(['@/components/user/index'], resolve)
        },
        // 新注册用户领取
        {
          path: 'user/register',
          name: 'userregister',
          component: resolve => require(['@/components/user/register'], resolve)
        },
        // 用户改价
        {
          path: 'user/search',
          name: 'userssearch',
          component: resolve => require(['@/components/user/search'], resolve)
        },
        // 客户经理拨打电话
        {
          path: 'manager/called',
          name: 'managercalled',
          component: resolve => require(['@/components/user/called'], resolve)
        },
        {
          path: 'user/account',
          name: 'account',
          component: resolve => require(['@/components/product/account'], resolve)
        },
        {
          path: 'user/accountsearch',
          name: 'accountsearch',
          component: resolve => require(['@/components/product/accountSearch'], resolve)
        },
        {
          path: 'user/userprice',
          name: 'userprice',
          component: resolve => require(['@/components/user/userprice'], resolve)
        },
        {
          path: 'user/statistics',
          name: 'userstatistics',
          component: resolve => require(['@/components/user/statistics'], resolve)
        },
        {
          path: 'user/statisticsrecord',
          name: 'userstatisticsrecord',
          component: resolve => require(['@/components/user/statisticsrecord'], resolve)
        },
        {
          path: 'user/statistics_one',
          name: 'statistics_one',
          component: resolve => require(['@/components/user/statisticsOne'], resolve)
        },
        // 设置
        {
          path: 'user/managerlist',
          name: 'managerlist',
          component: resolve => require(['@/components/setting/managerlist'], resolve)
        },
        {
          path: 'user/addmanager',
          name: 'addmanager',
          component: resolve =>
            require(['@/components/setting/managerDetail'], resolve)
        },
        {
          path: 'user/argument',
          name: 'argument',
          component: resolve =>
            require(['@/components/user/argument'], resolve)
        },
        // 角色管理
        {
          path: 'user/rolelist',
          name: 'rolelist',
          component: resolve => require(['@/components/setting/roles'], resolve)
        },
        // 添加角色
        {
          path: 'user/editrole',
          name: 'editrole',
          component: resolve => require(['@/components/setting/editrole'], resolve)
        },
         // 代理商管理
         {
          path: 'user/agent',
          name: 'agent',
          component: resolve => require(['@/components/user/agent'], resolve)
        },
         // 代理商价格
         {
          path: 'agent/agentprice',
          name: 'agentprice',
          component: resolve => require(['@/components/user/agentprice'], resolve)
        },
        // 代理商明细
        {
          path: 'user/agentscore',
          name: 'agentscore',
          component: resolve => require(['@/components/user/agentscore'], resolve)
        },
        // 产品管理
        {
          path: 'product/list',
          name: 'productlist',
          component: resolve => require(['@/components/product/list'], resolve)
        },
        {
          path: 'product/roslist',
          name: 'productroslist',
          component: resolve => require(['@/components/product/roslist'], resolve)
        },
        {
          path: 'product/package',
          name: 'package',
          component: resolve => require(['@/components/product/package'], resolve)
        },
        {
          path: 'product/config',
          name: 'productconfig',
          component: resolve => require(['@/components/product/config'], resolve)
        },
        {
          path: 'product/soft',
          name: 'productsoft',
          component: resolve => require(['@/components/product/soft'], resolve)
        },
        {
          path: 'product/route',
          name: 'productroute',
          component: resolve => require(['@/components/product/route'], resolve)
        },
        {
          path: 'product/pricescheme',
          name: 'pricescheme',
          component: resolve => require(['@/components/product/pricescheme'], resolve)
        },
        {
          path: 'product/pricediscount',
          name: 'pricediscount',
          component: resolve => require(['@/components/product/pricediscount'], resolve)
        },
        // 资讯管理
        {
          path: 'article/list',
          name: 'articlelist',
          component: resolve => require(['@/components/article'], resolve)
        },
        {
          path: 'article/add',
          name: 'addindex',
          component: resolve => require(['@/components/article/add'], resolve)
        },
        // 订单
        {
          path: 'order/index',
          name: 'orderindex',
          component: resolve => require(['@/components/order/index'], resolve)
        },
        {
          path: 'order/ros',
          name: 'orderros',
          component: resolve => require(['@/components/order/ros'], resolve)
        },
        {
          path: 'order/taobao',
          name: 'orderindex',
          component: resolve => require(['@/components/order/taobao'], resolve)
        },
        {
          path: 'order/courseorder',
          name: 'courseorder',
          component: resolve => require(['@/components/order/course'], resolve)
        },
        {
          path: 'order/packageorder',
          name: 'packageorder',
          component: resolve => require(['@/components/order/package'], resolve)
        },
        {
          path: 'order/refundorder',
          name: 'refundOrderItems',
          component: resolve => require(['@/components/order/refunds'], resolve)
        },
        {
          path: 'order/refundsearch',
          name: 'refundsearch',
          component: resolve => require(['@/components/order/refundsearch'], resolve)
        },
        {
          path: 'order/amountdetails',
          name: 'amountdetails',
          component: resolve => require(['@/components/user/amountdetails'], resolve)
        },
        {
          path: 'order/charge',
          name: 'charge',
          component: resolve => require(['@/components/user/chargeorder'], resolve)
        },
        {
          path: 'order/chargeclaim',
          name: 'claim',
          component: resolve => require(['@/components/user/chargeclaim'], resolve)
        },
        {
          path: 'order/sellinfos',
          name: 'sellinfos',
          component: resolve => require(['@/components/order/managerstatistics'], resolve)
        },
        // 销售管理
        {
          path: 'sell/coupon',
          name: 'coupon',
          component: resolve => require(['@/components/sell/coupon'], resolve)
        },
        {
          path: 'sell/addcoupon',
          name: 'addcoupon',
          component: resolve => require(['@/components/sell/coupon_add'], resolve)
        },
        {
          path: 'order/statistics',
          name: 'statistics',
          component: resolve => require(['@/components/order/statistics'], resolve)
        },
        {
          path: 'sell/couponused',
          name: 'couponused',
          component: resolve => require(['@/components/sell/couponused'], resolve)
        },
        {
          path: 'sell/sellerorder',
          name: 'sellerorder',
          component: resolve => require(['@/components/order/sellerorder'], resolve)
        },
        {
          path: 'sell/sellerstatistics',
          name: 'sellerstatistics',
          component: resolve => require(['@/components/order/sellerstatistics'], resolve)
        },
        {
          path: 'logs',
          name: 'logs',
          component: resolve => require(['@/components/logs/logList'], resolve)
        },
        {
          path: 'order/cashout',
          name: 'cashout',
          component: resolve => require(['@/components/order/cashOut'], resolve)
        },
        {
          path: 'order/tb_refund',
          name: 'tb_refund',
          component: resolve => require(['@/components/order/tb_refund'], resolve)
        },
        {
          path: 'order/failed',
          name: 'failed',
          component: resolve => require(['@/components/order/failed'], resolve)
        },
        {
          path: 'order/check',
          name: 'check',
          component: resolve => require(['@/components/order/check'], resolve)
        },
        {
          path: 'order/finance',
          name: 'finance',
          component: resolve => require(['@/components/order/finance'], resolve)
        },
        {
          path: 'order/buyi',
          name: 'buyi',
          component: resolve => require(['@/components/order/buyiRecord'], resolve)
        },
        {
          path: 'order/jinqiao',
          name: 'jinqiao',
          component: resolve => require(['@/components/order/jinqiao'], resolve)
        },
        {
          path: 'product/line',
          name: 'linlist',
          component: resolve => require(['@/components/product/lineList'], resolve)
        },
        {
          path: 'http/recharge',
          name: 'httprecharge',
          component: resolve => require(['@/components/http/recharge'], resolve)
        },
        {
          path: 'http/order',
          name: 'httporder',
          component: resolve => require(['@/components/http/order'], resolve)
        },
        {
          path: 'http/payments',
          name: 'httppayments',
          component: resolve => require(['@/components/http/payments'], resolve)
        }
      ]
    }
  ]
})

// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  let userInfo = local.getUserByCache()
  if(userInfo != null){
    $.ajax({
      type: 'GET',
      url: 'https://php-api.juip.com/api/Allow/urlCheck',
      dataType: "json",
      contentType: "application/json",
      data: {'url':to.fullPath,'id':userInfo.Manager.ID},
      success: function (res) {
        if(res.Code == -10000){
          router.replace({
            path: '/login',
            query: {
              redirect: router.currentRoute.fullPath
            }
          })
        }
      }
    });
  }
  if (to.path === '/login' || to.path === '/' || to.path === '/r') {
    next()
  } else {
    // localStorage.getItem('etorcurrentuser');
    if (!userInfo || userInfo === '') {
      router.replace({
        path: '/login',
        query: {
          redirect: router.currentRoute.fullPath
        }
      })
    } else {
      next()
    }
  }
})

export default router
