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/index/Index-${process.env.VUE_APP_CATEGORY}.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'),
        props: ({ query }) => {
          return {
            selectTemplateId: query.selectTemplateId !== undefined ? Number.parseInt(query.selectTemplateId as string) : undefined,
            templateName: query.templateName,
          };
        },
        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: '/selectTemplate',
    name: 'SelectTemplate',
    // 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: "select-template" */ '@/views/selectTemplate/Index.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/searchTemplate',
    name: 'SearchTemplate',
    component: () => import(/* webpackChunkName: "search-template" */ '@/views/searchTemplate/Index.vue'),
    meta: { requiresAuth: true },
  },
  {
    path: '/viewTemplate',
    name: 'ViewTemplate',
    component: () => import(/* webpackChunkName: "view-template" */ '@/views/viewTemplate/Index.vue'),
    props: ({query}) => ({
      folderId: query.folderId !== undefined ? Number.parseInt(query.folderId as string, 10) : undefined,
      folderName: query.folderName,
    }),
    meta: { requiresAuth: true },
  },
  {
    path: '/proofImg',
    name: 'ProofImg',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    component: () => import(/* webpackChunkName: "proof-img" */ '@/views/proof/Credentials.vue'),
    meta: { requiresAuth: true },
  },
  {
    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,
      detailStr: route.query.detail,
    }),
    meta: { requiresAuth: true },
  },
  {
    path: '/addProofFromSelfEditTemplate',
    name: 'AddProofFromSelfEditTemplate',
    component: () => import(/* webpackChunkName: "AddProofFromSelfEditTemplate" */ '@/views/proof/AddProofFromSelfEditTemplate.vue'),
    props: (route) => ({
      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'),
    props: ({ query }) => ({ locationUrl: query.locationUrl }),
    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'),
    props: ({query}) => ({
      folderId: query.folderId !== undefined ? Number.parseInt(query.folderId as string, 10) : undefined,
    }),
    meta: { requiresAuth: true },
  },
  {
    path: '/editTemplate',
    name: 'EditTemplate',
    component: () => import(/* webpackChunkName: "edit-template" */ '@/views/template/EditTemplate.vue'),
    props: ({query}) => ({ id: Number.parseInt(query.id as string, 10) }),
    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: ({ query: { folderName, id } }) => ({ folderName, id }),
    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) => ({ isCallback: route.query.isCallback }),
    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;
