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" */ '@/components/category/Index.vue'),
        },
        // 个人中心
        {
          path: '/userCenter',
          name: 'userCenter',
          component: () => import(/* webpackChunkName: "userCenter" */ '@/views/userCenter/index.vue')
        },
        // 选择套餐
        {
          path: '/choosePackage',
          name: 'choosePackage',
          component: () => import(/* webpackChunkName: "companyVerify" */ './views/user/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: '/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;
