import Vue from 'vue';
import Router from 'vue-router';
import { getLoginCode } from '@/plugins/storage';

Vue.use(Router);

let router = new Router({ // todo 移除旧页面
  routes: [
    // 宣传页
    {
      path: '/',
      name: 'home',
      component: () => import('./views/Home.vue'),
    },
    // 登录
    {
      path: '/signIn',
      name: 'signIn',
      component: () => import('./views/SignIn.vue'),
    },
    {
      path: '/main',
      component: () => import(/* webpackChunkName: "main" */ './views/Main.vue'),
      children: [
        {
          path: '/categoryManage',
          name: 'categoryManage',
          component: () => import(/* webpackChunkName: "categoryManage" */ './views/category/Index.vue'),
        },
        // 个人中心
        {
          path: '/userCenter',
          name: 'userCenter',
          component: () => import(/* webpackChunkName: "userCenter" */ '@/views/userCenter/index.vue')
        },
        {
          path: '/choosePackage',
          name: 'choosePackage',
          component: () => import(/* webpackChunkName: "companyVerify" */ '@/views/userCenter/choosePackage.vue')
        },
        {
          path: '/initPassword',
          name: 'initPassword',
          component: () => import(/* webpackChunkName: "initPassword" */ '@/views/userCenter/SetPassword.vue'),
        },
        {
          path: '/companyVerify',
          name: 'companyVerify',
          component: () => import(/* webpackChunkName: "companyVerify" */ '@/views/userCenter/CompanyVerify.vue')
        },
        {
          path: '/editPassword',
          name: 'editPassword',
          component: () => import(/* webpackChunkName: "editPassword" */ '@/views/userCenter/EditPassword.vue'),
        },
        {
          path: '/editPhone',
          name: 'editPhone',
          component: () => import(/* webpackChunkName: "editPhone" */ '@/views/userCenter/EditPhone.vue'),
        },
        {
          path: '/personVerify',
          name: 'personVerify',
          component: () => import(/* webpackChunkName: "personVerify" */ '@/views/userCenter/PersonVerify.vue')
        },
        {
          path: '/templateManage',
          name: 'templateManage',
          component: () => import(/* webpackChunkName: "templateManage" */ '@/views/template/Index.vue'),
          children: [
            {
              path: '/editTemplate',
              name: 'editTemplate',
              component: () => import(/* webpackChunkName: "editTemplate" */ '@/views/template/editTemplate.vue'),
            }
          ]
        }
      ]
    }
  ],

});

router.beforeEach((to, from, next) => {
  const urls = ['/', '/signIn'];
  if (to.path === `/signIn` && getLoginCode()) {
    if (to.query && to.query.type && to.query.type == '1') {

    }
    else {

    }
    next();
  }
  if (!urls.includes(to.path) && !getLoginCode()) {
    return next({ path: "/signIn" });
  }
  else {
    next();
  }
})

export default router;
