/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue'
import Router from 'vue-router'
import { clearLoginInfo } from '@/utils'

Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

// 全局路由(无需嵌套上左右整体布局)
const globalRoutes = [
  { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } }
]

// 参猫
export const toolsNav0 = [
  {
    path: '/tools-analyze',
    component: _import('modules/tools/tools-analyze'),
    name: 'toolsAnalyze',
    meta: { title: '任意竞品透析' }
  },
  {
    path: '/tools-online',
    component: _import('modules/tools/tools-online'),
    name: 'toolsOnline',
    meta: { title: '在线指数还原' }
  }
]

export const toolsNav1 = [
  {
    path: '/tools-credit',
    component: _import('modules/tools/tools-credit'),
    name: 'toolsCredit',
    meta: { title: '淘宝极速验号' }
  },
  {
    path: '/tools-ordersearch',
    component: _import('modules/tools/tools-ordersearch'),
    name: 'toolsOrdersearch',
    meta: { title: '淘客订单查询' }
  }
]

export const toolsNav2 = [
  {
    path: '/tools-starkeysearch',
    component: _import('modules/tools/tools-starkeysearch'),
    name: 'toolsStarkeysearch',
    meta: { title: '淘宝下拉框选词' }
  },
  {
    path: '/tools-shopinfo',
    component: _import('modules/tools/tools-shopinfo'),
    name: 'toolsShopinfo',
    meta: { title: '店铺信息查询' }
  },
  {
    path: '/tools-promote',
    component: _import('modules/tools/tools-promote'),
    name: 'toolsPromote',
    meta: { title: '淘宝卡首屏' }
  }
]
export const toolsNav3 = [
  {
    path: '/tools-pricetrend',
    component: _import('modules/tools/tools-pricetrend'),
    name: 'toolsPricetrend',
    meta: { title: '价格走势' }
  }
]

export const toolsRoutes = [
  {
    path: '/tools-feedback',
    component: _import('modules/tools/tools-feedback'),
    name: 'toolsFeedback',
    meta: { title: '商品评论查询' }
  },
  {
    path: '/tools-productinfo',
    component: _import('modules/tools/tools-productinfo'),
    name: 'toolsProductinfo',
    meta: { title: '商品信息查询' }
  },
  {
    path: '/tools-productaskinfo',
    component: _import('modules/tools/tools-productaskinfo'),
    name: 'toolsProductaskinfo',
    meta: { title: '商品问大家' }
  }
]

// 个人中心
const mineRoutes = [
  {
    path: '/mine',
    component: _import('modules/mine/index'),
    children: [
      {
        path: '',
        component: _import('modules/mine/user'),
        name: 'mine',
        meta: {
          title: '个人中心',
          isTab: true,
          isUseCache: false,
          keepAlive: true,
          isAuth: true
        }
      },
      {
        path: '/order-check-order',
        component: _import('modules/mine/order/order-check-order'),
        name: 'orderCheckOrder',
        meta: {
          title: '订单查询',
          isTab: true,
          isUseCache: false,
          keepAlive: true,
          isAuth: true
        }
      },
      {
        path: '/order-check-package',
        component: _import('modules/mine/order/order-check-package'),
        name: 'orderCheckPackage',
        meta: {
          title: '包裹查询',
          isTab: true,
          isUseCache: false,
          keepAlive: true,
          isAuth: true
        },
        props: (route) => ({
          orderNumber: route.query.orderNumber
        })
      },
      {
        path: '/pay-check',
        component: _import('modules/mine/pay/pay-check'),
        name: 'payCheck',
        meta: {
          title: '充值',
          isTab: true,
          isUseCache: false,
          keepAlive: true,
          isAuth: true
        }
      },
      {
        path: '/pay-wallet',
        component: _import('modules/mine/pay/pay-wallet'),
        name: 'payWallet',
        meta: {
          title: '我的钱包',
          isTab: true,
          isUseCache: false,
          keepAlive: true,
          isAuth: true
        }
      }
    ]
  }
]

// 礼品商城
const mallRoutes = [
  {
    path: '/mall',
    component: _import('modules/mall/index'),
    name: 'mall',
    meta: {
      title: '礼品商城',
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/goods-detail',
    component: _import('modules/mall/goods-detail'),
    name: 'goodsDetail',
    meta: {
      title: '商品详情',
      isUseCache: false,
      keepAlive: true
    },
    props: (route) => ({
      id: route.query.id
    })
  },
  {
    path: '/goods-create-order',
    component: _import('modules/mall/goods-create-order'),
    name: 'goodsCreateOrder',
    meta: {
      title: '创建订单',
      isUseCache: false
    },
    props: (route) => ({
      id: route.query.id
    })
  },
  {
    path: '/goods-order-detail',
    component: _import('modules/mall/goods-order-detail'),
    name: 'goodsOrderDetail',
    meta: {
      title: '订单详情',
      isUseCache: false
    },
    props: (route) => ({
      id: route.query.id
    })
  }
]

// 主入口路由(需嵌套上左右整体布局)
const mainRoutes = {
  path: '/',
  component: _import('main'),
  name: 'main',
  redirect: { name: 'home' },
  meta: { title: '主入口整体布局' },
  children: [
    // 通过meta对象设置路由展示方式
    // 1. isTab: 是否通过tab展示内容, true: 是, false: 否
    // 2. iframeUrl: 是否通过iframe嵌套展示内容, '以http[s]://开头': 是, '': 否
    // 提示: 如需要通过iframe嵌套展示内容, 但不通过tab打开, 请自行创建组件使用iframe处理!
    {
      path: '/login',
      component: _import('modules/account/login'),
      name: 'login',
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      component: _import('modules/account/register'),
      name: 'register',
      meta: {
        title: '注册'
      }
    },
    {
      path: '/home',
      component: _import('common/home'),
      name: 'home',
      meta: {
        title: '首页',
        isTab: true
      }
    },
    ...toolsNav0,
    ...toolsNav1,
    ...toolsNav2,
    ...toolsNav3,
    ...toolsRoutes,
    ...mineRoutes,
    ...mallRoutes
  ]
}

const router = new Router({
  mode: 'history',
  scrollBehavior (to, from, savedPosition) {
    if (to.hash) {
      return {
        selector: to.hash
      }
    }
    // keep-alive 返回缓存页面后记录浏览位置
    if (savedPosition && to.meta.keepAlive) {
      return savedPosition
    }
    // 异步滚动操作
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({ x: 0, y: 1 })
      }, 0)
    })
  },
  isAddDynamicMenuRoutes: false, // 是否已经添加动态(菜单)路由
  routes: globalRoutes.concat(mainRoutes)
})

router.beforeEach((to, from, next) => {
  let token = Vue.cookie.get('token')
  if ((!token || !/\S/.test(token)) && to.meta.isAuth) {
    clearLoginInfo()
    next({ name: 'login' })
  } else {
    next()
  }
})

export default router
