//各套餐的默认权限组
export let permission = {
  EXPO_MANAGEMENT: ['audience.search','audience.select','audience.import','audience.changePage','preReg.search','preReg.handelDisable','preReg.handelDelete','preReg.changePage','preReg.save','preReg.copy','preReg.saveComp','preReg.copyComp','preReg.addComp','exhibitor.search','exhibitor.handelDisable','exhibitor.handelDelete','exhibitor.save','exhibitor.copyright','setting.system.save'],
  EXPO_INVITATION_MANAGEMENT: ['audience.search','audience.select','audience.sendInvite','audience.changePage','preReg.search','preReg.handelDisable','preReg.handelDelete','preReg.changePage','preReg.save','preReg.copy','preReg.saveComp','preReg.copyComp','preReg.addComp','audience.import','invitation.addList','invitation.changePage','setting.system.save'],
  // 默认权限,啥也没买就能看的
  DEFAULT: ['dashboard','dashboard.head','audience','preReg','preReg.addNew','preReg.comp','preReg.edit','preReg.list','preReg.handelEdit','preReg.creat','preReg.editEdit','preReg.editName','preReg.editDesc','exhibitor','exhibitor.list','exhibitor.setting','exhibitor.add','exhibitor.addList','exhibitor.handelView','exhibitor.handelEdit','invitation','invitation.add','invitation.edit','invitation.index','invitation.addList','invitation.goto','invitation.desc','invitation.rename','invitation.select','invitation.editor','setting','setting.system','setting.system.sentEmailAddress','setting.system.sentEmailPassword','setting.system.sentEmailServer','setting.system.sentEmailPort','setting.system.sentEmailSSL','404','401','login','user.form','user.register']
}
let allPermission = permission.DEFAULT
let is_init = false
//root就是Vue实例，可在其上使用路由总线状态管理等。
import {root} from '@/main'
// 控件锁定的模板
let mask = `
  <div class="el-icon-lock" style="z-index: 999;position: absolute;width: 100%;height: 100%;left: 0;top:0;color:#ffffff;background: #00000044;display: flex;align-items: center;justify-content: center;font-size: inherit;"></div>
`
export function canIShow(roles) {
  return new Promise((resolve, reject) => {
    let timer = setTimeout(()=>{
      if(!is_init){
        if(root.$store.getters.user.permission.includes('expo_management')) {
          allPermission = allPermission.concat(permission.EXPO_MANAGEMENT)
        }
        if(root.$store.getters.user.permission.includes('expo_invitation_management')) {
          allPermission = allPermission.concat(permission.EXPO_INVITATION_MANAGEMENT)
        }
        is_init = true
      }
      if(allPermission.includes(roles)){
        resolve(true)
      }else {
        resolve(false)
      }
      clearTimeout( timer)
    },0)
  })
}
/**
 * 控件权限控制定指令
 * @type {{inserted: directive.inserted, update: directive.update}}
 */
let directive = {
  inserted: function(el, binding, vnode) {
    canIShow(binding.value).then((res)=>{
      if(!res) {
        let node = new DOMParser().parseFromString(mask, 'text/html')
        node.body.firstElementChild.onclick = (event)=>{
          event.stopPropagation()
          root.$bus.$emit('showPacket',true)
        }
        if(window.getComputedStyle(el).position !== 'relative' && window.getComputedStyle(el).position !== 'absolute') {
          el.style.position = 'relative'
        }
        el.style.overflow = 'hidden'
        el.append(node.body.firstElementChild)
      }
    })
  },
  update: function(el, binding, vnode) {

  },
}
export default directive

