import Vue from 'vue';
import VueRouter, { RouteConfig } from 'vue-router';
import Main from '@/views/Main.vue';
import { Route } from 'vue-router';
import store from '@/store/index';
Vue.use(VueRouter);

const routes: RouteConfig[] = [
  {
    path: '/',
    component: Main,
    children: [
      {
        path: '',
        name: 'TemplateList',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "template-list" */ '@/views/template/Index.vue'),
      },
      {
        path: '/proofList',
        name: 'ProofList',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "proof-list" */ '@/views/proof/Index.vue'),
        meta: { requiresAuth: true },
      },
      {
        path: '/userCenter/index',
        name: 'UserCenter',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "user-center-index" */ '@/views/userCenter/Index.vue'),
      },
    ],
  },
  {
    path: '/proofDetail',
    name: 'ProofDetail',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "proof-detail" */ '@/views/proof/Detail.vue'),
    props: (route) => ({ templateId: route.query.templateId,
        proofId: route.query.proofId,
        baseProof: route.query.baseProof,
        preProof: route.query.preProof,
        proofName: route.query.proofName,
      }),
    meta: { requiresAuth: true },
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "login" */ '@/views/login/Login.vue'),
  },
  {
    path: '/viewCode',
    name: 'ViewCode',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "view-code" */ '@/views/ViewCode.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/recycleList',
    name: 'RecycleList',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "recycle-list" */ '@/views/userCenter/RecycleList.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/addTemplate',
    name: 'Add',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "add-template" */ '@/views/template/Add.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/templList',
    name: 'TemplList',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "templ-list" */ '@/views/template/List.vue'),
    props: (route) => ({ templList: route.query.templList }),
    meta: { requiresAuth: true },
  },
  {
    path: '/recharge',
    name: 'Recharge',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "recharge" */ '@/views/userCenter/Recharge.vue'),
    props: (route) => ({ templList: route.query.templList }),
    meta: { requiresAuth: true },
  },
  {
    path: '/contect',
    name: 'Contect',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "contect" */ '@/views/userCenter/Contect.vue'),
    props: (route) => ({ templList: route.query.templList }),
    meta: { requiresAuth: true },
  },
  {
    path: '/rechargeRecord',
    name: 'RechargeRecord',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "recharge-record" */ '@/views/userCenter/RechargeRecord.vue'),
    props: (route) => ({ templList: route.query.templList }),
    meta: { requiresAuth: true },
  },
  {
    path: '/rechargeSuccess',
    name: 'RechargeSuccess',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "recharge-success" */ '@/views/userCenter/RechargeSuccess.vue'),
    props: (route) => ({ templList: route.query.templList }),
    meta: { requiresAuth: true },
  },
  {
    path: '/accountNum',
    name: 'AccountNum',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "account-num" */ '@/views/userCenter/AccountNum.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/setPasswd',
    name: 'SetPasswd',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "set-passwd" */ '@/views/userCenter/SetPasswd.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/setPhone',
    name: 'SetPhone',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "set-phone" */ '@/views/userCenter/SetPhone.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/auth/company',
    name: 'Enterprise',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "enterprise" */ '@/views/auth/Enterprise.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/auth/personal',
    name: 'Personal',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "auth-personal" */ '@/views/auth/Personal.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/auth/index',
    name: 'Auth',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "auth-index" */ '@/views/auth/Index.vue'),
    meta: { requiresAuth: true },
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to: Route, from: Route, next: any) => {
  if (to.name !== 'Login' && !localStorage.getItem('userInfo') && to.meta.requiresAuth) {
    next({ name: 'Login' });
  } else {
    next();
  }
});

export default router;
