import Vue from 'vue'
import VueRouter from 'vue-router'
import { updateWechatToken } from '../utils'

Vue.use(VueRouter)

const _import = require('./import-' + process.env.NODE_ENV)
const routes = [
  // {
  //   path: '*',
  //   redirect: '/404'
  // },
  {
    path: '/404',
    name: '404',
    component: _import('page/404/index'),
    meta: {
      isUseVanTabbar: false
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: _import('login/index'),
    meta: {
      title: '登录',
      isUseVanTabbar: false
    }
  },
  {
    path: '/',
    name: 'Index',
    component: _import('views/index/index'),
    meta: {
      isUseVanTabbar: true
    },
    redirect: '/place/list'
  },
  {
    path: '/place/list',
    name: 'PlaceList',
    component: _import('views/place/list/index'),
    meta: {
      title: '订座',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: true
    }
  },
  {
    path: '/place/reserve',
    name: 'PlaceReserve',
    component: _import('views/place/reserve/index'),
    meta: {
      title: '我的订座',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: true
    }
  },
  {
    path: '/place/check',
    name: 'PlaceCheck',
    component: _import('views/place/check/index'),
    meta: {
      title: '我的订座二维码',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: false
    }
  },
  {
    path: '/payBridge', // 用于页面在微信内选择了支付宝支付
    name: 'PayBridge',
    component: _import('views/payBridge/index'),
    meta: {
      title: '支付方式',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: false
    },
    props: route => ({
      alipayForm: route.query.alipayForm,
      wxpayHref: route.query.wxpayHref
    })
  },
  {
    path: '/show/plan',
    name: 'ShowPlan',
    component: _import('views/show/plan/index'),
    meta: {
      title: '演出计划',
      isUseCache: false,
      keepAlive: true,
      isUseVanTabbar: false
    },
    props: route => ({
      alipayForm: route.query.alipayForm,
      wxpayHref: route.query.wxpayHref
    })
  },
  {
    path: '/sell/goods',
    name: 'SellGoods',
    component: _import('views/sell/goods/index'),
    meta: {
      title: '点单',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: false
    }
  },
  {
    path: '/sell/pay',
    name: 'SellPay',
    component: _import('views/sell/pay/index'),
    meta: {
      title: '订单',
      isUseCache: false,
      keepAlive: false,
      isUseVanTabbar: false
    }
  },
  {
    path: '/order',
    component: _import('views/order/index'),
    children: [
      {
        path: '',
        name: 'OrderList',
        component: _import('views/order/list/index'),
        meta: {
          title: '订单',
          isUseCache: false,
          keepAlive: false,
          isUseVanTabbar: false
        }
      }
    ]
  },
  {
    path: '/orderDetail',
    name: 'OrderDetail',
    component: _import('views/orderDetail/index'),
    meta: {
      title: '订单已支付',
      isUseCache: false,
      keepAlive: false
    }
  },
  {
    path: '/refundList',
    name: 'RefundList',
    component: _import('views/refundList/index'),
    meta: {
      title: '退款记录',
      isUseCache: false,
      keepAlive: false
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  routes,
  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)
    })
  }
})

router.beforeEach((to, from, next) => {
  updateWechatToken()
  next()
})

export default router
