import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import { canIShow } from '@/permission'
import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/login',
    meta: { title: '首页' },
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: {
          title: '首页看板',
          icon: 'el-icon-house',
          roles: 'dashboard',
          func: [
            {
              name: '头部概览',
              roles: 'dashboard.head'
            }
          ]
        }
      },
      {
        path: 'audience',
        component: () => import('@/views/audienceManage/index'),
        name: 'audienceManage',
        meta: {
          title: '观众管理',
          icon: 'el-icon-user',
          roles: 'audience',
          func: [
            {
              name: '搜索观众',
              roles: 'audience.search'
            },
            {
              name: '按展会筛选',
              roles: 'audience.select'
            },
            {
              roles: 'audience.sendInvite',
              name: '发送邀请函'
            },
            {
              roles: 'audience.changePage',
              name: '观众换页'
            },
            {
              name: '观众导入',
              roles: 'audience.import'
            }
          ]
        }
      },
      {
        path: 'preRegister',
        component: () => import('@/views/preRegManage/index'),
        name: 'preRegManage',
        meta: {
          title: '预登记表单',
          icon: 'el-icon-tickets',
          roles: 'preReg'
        },
        redirect: '/preRegister/list',
        children: [
          {
            path: 'list',
            component: () => import('@/views/preRegManage/list'),
            name: 'preRegManageList',
            meta: {
              title: '表单管理',
              icon: 'el-icon-edit',
              roles: 'preReg.list',
              collapse: false,
              func: [
                {
                  roles: 'preReg.creat',
                  name: '创建表单'
                },
                {
                  roles: 'preReg.search',
                  name: '搜索表单'
                },
                {
                  roles: 'preReg.handelEdit',
                  name: '表单编辑'
                },
                {
                  roles: 'preReg.handelDisable',
                  name: '表单禁用'
                },
                {
                  roles: 'preReg.handelDelete',
                  name: '表单删除'
                },
                {
                  roles: 'preReg.changePage',
                  name: '表单换页'
                }
              ]
            }
          },
          {
            path: 'edit/:id',
            component: () => import('@/views/preRegManage/edit.vue'),
            name: 'preRegManagEdit',
            meta: {
              title: '表单编辑',
              icon: 'el-icon-edit',
              hidden: true,
              roles: 'preReg.edit',
              collapse: true,
              func: [
                {
                  roles: 'preReg.save',
                  name: '表单保存'
                },
                {
                  roles: 'preReg.copy',
                  name: '表单复制'
                },
                {
                  roles: 'preReg.editEdit',
                  name: '表单内容编辑'
                },
                {
                  roles: 'preReg.editName',
                  name: '表单名称编辑'
                },
                {
                  roles: 'preReg.editDesc',
                  name: '表单描述编辑'
                },
                {
                  roles: 'preReg.saveCompEdit',
                  name: '保存组件'
                }
              ]
            }
          },
          {
            path: 'add',
            component: () => import('@/views/preRegManage/edit'),
            name: 'preRegManagAdd',
            meta: {
              title: '表单新增',
              icon: 'el-icon-document-add',
              roles: 'preReg.addNew',
              collapse: true
            }
          },
          {
            path: 'comp',
            component: () => import('@/views/preRegManage/compEdit.vue'),
            name: 'preRegManagcomp',
            meta: {
              title: '我的组件',
              icon: 'el-icon-menu',
              roles: 'preReg.comp',
              func: [
                {
                  roles: 'preReg.saveComp',
                  name: '保存组件'
                }, {
                  roles: 'preReg.copyComp',
                  name: '复制组件'
                }, {
                  roles: 'preReg.addComp',
                  name: '新增组件'
                }
              ]
            }
          }
        ]
      },
      {
        path: 'exhibitor',
        component: () => import('@/views/exhibitorManage/index'),
        name: 'ExhibitorManage',
        meta: {
          title: '展会管理',
          icon: 'el-icon-office-building',
          roles: 'exhibitor'
        },
        redirect: '/exhibitor/list',
        children: [
          {
            component: () => import('@/views/exhibitorManage/exhibitorList'),
            path: 'list',
            name: 'exhibitorManageList',
            meta: {
              title: '展会管理',
              icon: 'el-icon-edit',
              roles: 'exhibitor.list',
              func: [
                {
                  roles: 'exhibitor.addList',
                  name: '添加展会'
                },
                {
                  roles: 'exhibitor.search',
                  name: '搜索展会'
                },
                {
                  roles: 'exhibitor.handelView',
                  name: '预览展会'
                },
                {
                  roles: 'exhibitor.copyLink',
                  name: '复制表单连接'
                },
                {
                  roles: 'exhibitor.handelEdit',
                  name: '编辑展会'
                },
                {
                  roles: 'exhibitor.handelDisable',
                  name: '禁用展会'
                },
                {
                  roles: 'exhibitor.handelDelete',
                  name: '删除展会'
                },
                {
                  roles: 'exhibitor.handel',
                  name: '展会翻页'
                }
              ] }
          },
          {
            component: () => import('@/views/exhibitorManage/exhibitorSetting'),
            name: 'exhibitorEdit',
            path: 'edit/:id',
            meta: {
              title: '配置展会',
              icon: 'el-icon-setting',
              roles: 'exhibitor.setting',
              hidden: true,
              func: [
                {
                  roles: 'exhibitor.save',
                  name: '保存展会'
                },
                {
                  roles: 'exhibitor.copyright',
                  name: '是否显示底部信息开关'
                }
              ]
            }
          },
          {
            component: () => import('@/views/exhibitorManage/exhibitorSetting'),
            name: 'exhibitorAdd',
            path: 'add',
            meta: {
              title: '添加展会',
              icon: 'el-icon-document-add',
              roles: 'exhibitor.add'
            }
          }
        ]
      },
      {
        path: 'invitation',
        component: () => import('@/views/invitationManage/index'),
        name: 'invitationManage',
        meta: {
          title: '邀请函模板管理',
          icon: 'el-icon-files',
          roles: 'invitation'
        },
        redirect: '/invitation/list',
        children: [{
          path: 'list',
          component: () => import('@/views/invitationManage/list'),
          name: 'invitationManageList',
          meta: {
            title: '邀请函模板管理',
            icon: 'el-icon-edit',
            roles: 'invitation.index',
            collapse: false,
            func: [
              {
                roles: 'invitation.addList',
                name: '添加邀请函模板'
              },
              {
                roles: 'invitation.goto',
                name: '点击进入模板'
              },
              {
                roles: 'invitation.changePage',
                name: '邀请函模板翻页'
              }
            ]
          }
        },
        {
          path: 'add',
          component: () => import('@/views/invitationManage/edit'),
          name: 'invitationAdd',
          meta: {
            title: '邀请函模板新增',
            icon: 'el-icon-document-add',
            roles: 'invitation.add',
            collapse: true
          }
        },
        {
          path: 'edit/:id',
          component: () => import('@/views/invitationManage/edit'),
          name: 'invitationEdit',
          meta: {
            title: '邀请函模板编辑',
            icon: 'el-icon-edit',
            hidden: true,
            roles: 'invitation.edit',
            collapse: true,
            func: [
              {
                roles: 'invitation.save',
                name: '修改邀请函模板'
              },
              {
                roles: 'invitation.desc',
                name: '修改邀请函描述'
              },
              {
                roles: 'invitation.rename',
                name: '重命名邀请函模板'
              },
              {
                roles: 'invitation.select',
                name: '选择展会'
              },
              {
                roles: 'invitation.editor',
                name: '允许操作编辑器'
              }
            ]
          }
        }
        ]
      },
      {
        path: 'setting',
        component: () => import('@/views/setting/index'),
        name: 'setting',
        meta: {
          title: '信息与配置',
          icon: 'el-icon-setting',
          roles: 'setting'
        },
        redirect: '/setting/exhibitor',
        children: [
          {
            path: 'account',
            component: () => import('@/views/setting/accountSetting'),
            name: 'accountSetting',
            meta: {
              title: '系统账号管理',
              icon: 'el-icon-user',
              roles: 'setting.account',
              func: [
                {
                  name: '添加账号',
                  roles: 'setting.account.add'
                },
                {
                  name: '操作账号',
                  roles: 'setting.account.handel'
                },
                {
                  name: '搜索账号',
                  roles: 'setting.account.search'
                },
                {
                  name: '账号翻页',
                  roles: 'setting.account.changePage'
                }
              ]
            }
          },
          {
            path: 'roles',
            component: () => import('@/views/setting/rolesSetting.vue'),
            name: 'accountRoles',
            meta: {
              title: '系统角色管理',
              icon: 'el-icon-suitcase-1',
              roles: 'setting.roles',
              func: [
                {
                  name: '添加角色',
                  roles: 'setting.roles.add'
                }, {
                  name: '操作角色',
                  roles: 'setting.roles.handel'
                },
                {
                  name: '搜索角色',
                  roles: 'setting.roles.search'
                },
                {
                  name: '角色翻页',
                  roles: 'setting.roles.changePage'
                },
                {
                  name: '角色权限配置',
                  roles: 'setting.roles.permission'
                },
                {
                  name: '角色权限保存',
                  roles: 'setting.roles.save'
                }
              ]
            }
          },
          {
            path: 'system',
            component: () => import('@/views/setting/systemSetting'),
            name: 'systemSetting',
            meta: {
              title: '系统信息配置',
              icon: 'el-icon-setting',
              roles: 'setting.system',
              func: [
                {
                  name: '修改发件邮箱地址',
                  roles: 'setting.system.sentEmailAddress'
                },
                {
                  name: '修改发件邮箱密码/授权码',
                  roles: 'setting.system.sentEmailPassword'
                },
                {
                  name: '修改发件邮箱服务器地址',
                  roles: 'setting.system.sentEmailServer'
                },
                {
                  name: '修改发件邮箱SMTP端口号',
                  roles: 'setting.system.sentEmailPort'
                },
                {
                  name: '修改发件邮箱SSL加密',
                  roles: 'setting.system.sentEmailSSL'
                }, {
                  name: '保存设置',
                  roles: 'setting.system.save'
                }
              ]
            }
          }
        ]
      },
      {
        path: '404',
        component: () => import('@/views/errorPage/404'),
        name: '404',
        meta: {
          title: '页面不存在哦',
          icon: 'el-icon-delete-location',
          hidden: true,
          roles: '404'
        }
      },
      {
        path: '401',
        component: () => import('@/views/errorPage/401'),
        name: '401',
        meta: {
          title: '页面无权限哦',
          icon: 'el-icon-delete-location',
          hidden: true,
          roles: '401'
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: {
      title: '登录',
      icon: 'el-icon-user',
      hidden: true,
      roles: 'login'
    }
  },
  {
    path: '/user/form/:url',
    name: 'userForm',
    component: () => import('@/views/user/form.vue'),
    hidden: true,
    meta: {
      title: '用户表单',
      icon: 'el-icon-user',
      hidden: true,
      roles: 'user.form'
    }
  },
  {
    path: '/user/register',
    name: 'userRegister',
    component: () => import('@/views/user/register.vue'),
    hidden: true,
    meta: {
      title: '用户注册',
      icon: 'el-icon-user',
      hidden: true,
      roles: 'user.register'
    }
  },
  { 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) => {
  if (router.app.$store.getters.token) {
    canIShow(to.meta.roles).then((res) => {
      if (res) {
        next()
      } else {
        next({
          name: '401'
        })
      }
    })
  } else {
    next()
  }

})
export default router
