import Vue from 'vue';
import Router from 'vue-router';
import store from "@/store"

Vue.use(Router);

/* Layout */
import Layout from '../layout';

/* 路由器模块 */
// import componentsRouter from './modules/components'

/**
 * constantRoutes
 * 没有权限要求的基本页
 * 所有角色都可以访问
 * */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('../views/login/index'),
    name: 'login',
    hidden: true
  },
  {
    path: '/404',
    component: () => import('../views/errorPage/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('../views/errorPage/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: { name: 'lessee' },
    children: [
      {
        path: 'lessee',
        component: () => import('../views/lessee/lessee'),
        name: 'lessee',
        meta: {
          title: 'lessee',
          icon: 'lessee',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'lesseeAdd',
        component: () => import('../views/lessee/lesseeAdd'),
        name: 'lesseeAdd',
        meta: {
          title: 'lesseeAdd',
          icon: 'lesseeAdd',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'lesseeEdit',
        component: () => import('../views/lessee/lesseeEdit'),
        name: 'lesseeEdit',
        meta: {
          title: 'lesseeEdit',
          icon: 'lesseeEdit',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'lesseeMsg',
        component: () => import('../views/lessee/lesseeMsg'),
        name: 'lesseeMsg',
        meta: {
          title: 'lesseeMsg',
          icon: 'lesseeMsg',
          noCache: true,
          affix: true
        }
      },
      //试用租户详情
      {
        path: 'trialDetail',
        component: () => import('../views/lessee/trialDetail'),
        name: 'trialDetail',
        meta: {
          title: 'trialDetail',
          icon: 'trialDetail',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'accountDetail',
        component: () => import('../views/account/accountDetail'),
        name: 'accountDetail',
        meta: {
          title: 'accountDetail',
          icon: 'account',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'accountEdit',
        component: () => import('../views/account/accountEdit'),
        name: 'accountEdit',
        meta: {
          title: 'accountEdit',
          icon: 'account',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'accountNewly',
        component: () => import('../views/account/accountNewly'),
        name: 'accountNewly',
        meta: {
          title: 'accountNewly',
          icon: 'account',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'accountList',
        component: () => import('../views/account/accountList'),
        name: 'accountLIst',
        meta: {
          title: 'accountList',
          icon: 'account',
          noCache: true,
          affix: true
        }
      },
      {
        path: 'businessSettings',
        component: () => import('../views/businessSettings/businessSettings'),
        name: 'businessSettings',
        meta: {
          title: 'businessSettings',
          icon: 'businessSettings',
          noCache: true,
          affix: true
        }
      },
      //新闻管理
      {
        path: 'new',
        component: () => import('../views/new/new'),
        name: 'new',
        meta: {
          title: 'new',
        },
        children: [
          {
            path: 'newList',
            component: () => import('../views/new/newList'),
            name: 'newList',
            meta: {
              title: 'newList',
            },
            hidden: true
          },
          {
            path: 'newType',
            component: () => import('../views/new/newType'),
            name: 'newType',
            meta: {
              title: 'newType',
            },
            hidden: true
          },

        ]
      },
      {
        path: 'newAdd',
        component: () => import('../views/new/newAdd'),
        name: 'newAdd',
        meta: {
          title: 'newAdd',
        },
        hidden: true
      },
      {
        path: 'newDetails',
        component: () => import('../views/new/newDetails'),
        name: 'newDetails',
        meta: {
          title: 'newDetails',
        },
        hidden: true
      },
      {
        path: 'newTenantDetails',
        component: () => import('../views/new/newTenantDetails'),
        name: 'newTenantDetails',
        meta: {
          title: 'newTenantDetails',
        },
        hidden: true
      },
      //在线帮助
      // {
      //   path: 'help',
      //   component: () => import('../views/help/help'),
      //   name: 'help',
      //   meta: {
      //     title: 'help',
      //   },
      //   children: [
      //     {
      //       path: 'helpList',
      //       component: () => import('../views/help/helpList'),
      //       name: 'helpList',
      //       meta: {
      //         title: 'helpList',
      //       },
      //       hidden: true
      //     }
      //   ]
      // },
      // {
      //   path: 'helpAdd',
      //   component: () => import('../views/help/helpAdd'),
      //   name: 'helpAdd',
      //   meta: {
      //     title: 'helpAdd',
      //   },
      //   hidden: true
      // },
      // {
      //   path: 'helpEdit',
      //   component: () => import('../views/help/helpAdd'),
      //   name: 'helpEdit',
      //   meta: {
      //     title: 'helpEdit',
      //   },
      //   hidden: true
      // },
      // {
      //   path: 'helpDetails',
      //   component: () => import('../views/help/helpDetails'),
      //   name: 'helpDetails',
      //   meta: {
      //     title: 'helpDetails',
      //   },
      //   hidden: true
      // },
    ]
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  });

const router = createRouter();
router.beforeEach((to, from, next) => {
  const {baseInfo} = store.getters
  if (Object.keys(baseInfo).length === 0) {
    store.dispatch('user/getBaseInfo')
  }
  next();
})

router.beforeEach((to, from, next) => {
  const { baseInfo } = store.getters
  if (Object.keys(baseInfo).length === 0) {
    store.dispatch('user/getBaseInfo')
  }
  next();
})

export default router;
