import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const _import = require('./import-' + process.env.NODE_ENV)
const routes = [
  // {
  //   path: '*',
  //   redirect: '/404'
  // },
  {
    path: '/loan',
    redirect: '/'
  },
  {
    path: '/404',
    name: '404',
    component: _import('page/404/index')
  },
  {
    path: '/', // 我的申请记录
    name: 'Loan',
    component: _import('views/loan/home/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/loan/register', // 注册
    name: 'loanRegister',
    component: _import('views/loan/register/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/loan/mall', // 商城入口
    component: _import('views/loan/mall/index'),
    children: [
      {
        path: '', // 销售
        name: 'loanIndex',
        component: _import('views/loan/index/index'),
        meta: {
          isUseCache: false,
          keepAlive: true
        }
      },
      {
        path: '/loan/calculator', // 贷款工具
        name: 'loanCalculator',
        component: _import('views/loan/calculator/index'),
        meta: {
          isUseCache: false,
          keepAlive: true
        }
      },
      {
        path: '/loan/mine', // 我的
        name: 'loanMine',
        component: _import('views/loan/mine/index'),
        meta: {
          isUseCache: false,
          keepAlive: true
        }
      }
    ]
  },
  {
    path: '/loan/media/:id', // 销售页顶部banner，轮播图详情
    name: 'loanMedia',
    component: _import('views/loan/media/index'),
    props: (route) => ({
      id: route.params.id
    })
  },
  {
    path: '/loan/recommend', // 贷款计算器推荐产品
    name: 'loanRecommend',
    component: _import('views/loan/recommend/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/loan/invite', // 邀请记录
    name: 'loanInvite',
    component: _import('views/loan/invite/index')
  },
  {
    path: '/loan/coupon', // 优惠券
    name: 'loanCoupon',
    component: _import('views/loan/coupon/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/loan/explain/:orderProductId', // 贷款介绍(自己看到、其它人看到)
    name: 'loanExplain',
    component: _import('views/loan/explain/index'),
    props: (route) => ({
      orderProductId: route.params.orderProductId,
      partnerId: route.query.partnerId, // 合伙人ID
      partnerName: route.query.partnerName, // 合伙人名字
      partnerImgUrl: route.query.partnerImgUrl, // 合伙人头像
      recommendedUser: route.query.recommendedUser ? JSON.parse(decodeURIComponent(route.query.recommendedUser)) : {} // 推荐人信息
    })
  },
  {
    path: '/loan/apply/:orderProductId', // 基本信息填写
    name: 'loanApply',
    component: _import('views/loan/apply/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    },
    props: (route) => ({
      orderProductId: route.params.orderProductId,
      partnerId: route.query.partnerId,
      RId: route.query.RId // 妥妥推荐人ID
    })
  },
  {
    path: '/loan/detail/:id', // 进度查询：贷款详情页
    name: 'loanDetail',
    component: _import('views/loan/detail/index'),
    props: true
  },
  {
    path: '/loan/attest/:id', // 上传资料
    name: 'loanAttest',
    component: _import('views/loan/attest/index'),
    props: true
  },
  {
    path: '/loan/bespeak/:id', // 预约现场办理时间
    name: 'loanBespeak',
    component: _import('views/loan/bespeak/index'),
    props: true
  },
  {
    path: '/loan/privacy', // 隐私协议
    name: 'loanPrivacy',
    component: _import('views/loan/privacy/index')
  },
  {
    path: '/loan/agreement', // 协议
    name: 'loanAgreement',
    component: _import('views/loan/agreement/index')
  },
  {
    path: '/loan/vipAgreement', // 会员协议
    name: 'loanVipAgreement',
    component: _import('views/loan/vipAgreement/index')
  },
  {
    path: '/partner', // 我的销售
    component: _import('views/partner/home/index'),
    children: [
      {
        path: '', // 推广
        component: _import('views/partner/marketing/index'),
        children: [
          {
            path: '', // 客户
            name: 'partnerCustomer',
            component: _import('views/partner/customer/index'),
            meta: {
              isUseCache: false,
              keepAlive: true
            }
          },
          {
            path: 'business', // 合伙人
            name: 'partnerAll',
            component: _import('views/partner/all/index'),
            meta: {
              isUseCache: false,
              keepAlive: true
            }
          }
        ]
      },
      {
        path: 'mine', // 我的
        name: 'partnerMine',
        component: _import('views/partner/mine/index'),
        meta: {
          isUseCache: false,
          keepAlive: true
        }
      }
    ]
  },
  {
    path: '/partner/goods', // 贷款产品
    name: 'partnerGoods',
    component: _import('views/partner/goods/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    },
    props: (route) => ({
      partnerId: route.query.partnerId,
      partnerName: route.query.partnerName,
      partnerImgUrl: route.query.partnerImgUrl
    })
  },
  {
    path: '/partner/track/:id', // 客户申请贷款详情
    name: 'partnerTrack',
    component: _import('views/partner/track/index'),
    props: true
  },
  {
    path: '/partner/sale', // 放款总额
    name: 'partnerSale',
    component: _import('views/partner/sale/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    },
    props: (route) => ({
      id: route.query.id
    })
  },
  {
    path: '/partner/wallet', // 我的钱包
    name: 'partnerWallet',
    component: _import('views/partner/wallet/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    }
  },
  {
    path: '/partner/login', // 登录
    name: 'partnerLogin',
    component: _import('views/partner/login/index')
  },
  {
    path: '/partner/explain', // 成为合伙人-介绍
    name: 'partnerExplain',
    component: _import('views/partner/explain/index'),
    props: (route) => ({
      inviteCode: route.query.inviteCode
    })
  },
  {
    path: '/partner/join', // 成为合伙人-申请资料填写
    name: 'partnerJoin',
    component: _import('views/partner/join/index'),
    meta: {
      isUseCache: false,
      keepAlive: true
    },
    props: (route) => ({
      inviteCode: route.query.inviteCode
    })
  },
  {
    path: '/partner/detail/:id', // 申请进度
    name: 'partnerDetail',
    component: _import('views/partner/detail/index'),
    props: true
  },
  {
    path: '/partner/compact', // 签约
    name: 'partnerCompact',
    component: _import('views/partner/compact/index'),
    props: (route) => ({
      id: route.query.id
    })
  },
  {
    path: '/partner/agreement', // 协议
    name: 'partnerAgreement',
    component: _import('views/partner/agreement/index'),
    props: (route) => ({
      YYMMDD: route.query.YYMMDD,
      compactAddress: route.query.compactAddress
    })
  },
  {
    path: '/act/apply', // 在线申请
    name: 'actApply',
    component: _import('views/act/apply/index')
  }
]

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)
    })
  }
})

export default router
