import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/',
      name: 'Home',
      meta: {
        isAuth: true
      },
      redirect: '/Welcome',
      component: () => import('../view/Home.vue'),
      children: [
        {
          path: 'welcome',
          name: 'welcome',
          component: () => import('../view/Welcome.vue')
        },
        {
          path: 'changePwd',
          name: 'changePwd',
          component: () => import('../view/login/ChangePwd')
        },
        {
          path: 'normalSendGoods',
          name: 'NormalSendGoods',
          component: () => import('../view/shipping/NormalSendGoods.vue')
        },
        {
          path: 'shippingDesk',
          name: 'shippingDesk',
          component: () => import('../view/shipping/ShippingDesk.vue')
        },
        {
          path: 'shippingList',
          name: 'shippingList',
          component: () => import('../view/shipping/ShippingList.vue')
        },
        {
          path: 'waitPackageList',
          name: 'waitPackageList',
          component: () => import('../view/shipping/WaitPackageList.vue')
        },
        {
          path: 'waitShippingList',
          name: 'waitShippingList',
          component: () => import('../view/shipping/WaitShippingList.vue')
        },

        {
          path: 'copyProduct',
          name: 'copyProduct',
          component: () => import('../view/product/CopyProduct.vue')
        },
        {
          path: 'reducePrice',
          name: 'reducePrice',
          component: () => import('../view/product/ReducePrice.vue')
        },

        {
          path: 'niubiCopy',
          name: 'niubiCopy',
          component: () => import('../view/selection/NiubiCopy.vue')
        },
        {
          path: 'storeTrack',
          name: 'storeTrack',
          component: () => import('../view/selection/storetrack/Index.vue')
        },
        {
          path: 'keywordTrack',
          name: 'keywordTrack',
          component: () => import('../view/selection/keywordtrack/Index.vue')
        },

        {
          path: 'message',
          name: 'message',
          component: () => import('../view/Message.vue')
        },

        {
          path: 'coinFlow',
          name: 'coinFlow',
          component: () => import('../view/CoinFlow.vue')
        },
        
        {
          path: 'saleData',
          name: 'saleData',
          component: () => import('../view/ExportSaleData.vue')
        },
        
        // {
        //   path: 'statistics',
        //   name: 'statistics',
        //   component: () => import('../view/Statistics.vue')
        // },
        {
          path: 'learning',
          name: 'learning',
          component: () => import('../view/Learning.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('../view/login/Login.vue')
    },
    {
      path: '/register',
      name: 'register',
      meta: {
        title: '注册'
      },
      component: () => import('../view/login/Register.vue')
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  const isLogin = !!store.state.token

  if (to.meta.isAuth && !isLogin) {
    next({
      path: '/login',
      query: {
        redirect: to.fullPath
      }
    })
  } else {
    next()
  }
})

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
