import { SYS_BUTTON_AUTH, USER_AUTH } from '@/store/mutation-types'

const hasPermission = {
  install(Vue) {
    Vue.directive('has', {
      inserted: (el, binding, vnode) => {
        //节点权限处理，如果命中则不进行全局权限处理
        if (!filterNodePermission(el, binding, vnode)) {
          filterGlobalPermission(el, binding, vnode)
        }
      }
    })
  }
}

/**
 * 全局权限控制
 */
export function filterNodePermission(el, binding, vnode) {
  var permissionList = []
  try {
    var obj = vnode.context.$props.formData
    if (obj) {
      let bpmList = obj.permissionList
      for (var bpm of bpmList) {
        if (bpm.type !== '2') {
          permissionList.push(bpm)
        }
      }
    }
  } catch (e) {
    console.error(e)
  }
  if (permissionList === null || permissionList === '' || permissionList === undefined || permissionList.length <= 0) {
    //el.parentNode.removeChild(el)
    return false
  }
  let permissions = []
  for (var item of permissionList) {
    if (item.type !== '2') {
      permissions.push(item.action)
    }
  }
  if (!permissions.includes(binding.value)) {
    //el.parentNode.removeChild(el)
    return false
  } else {
    for (var item2 of permissionList) {
      if (binding.value === item2.action) {
        return true
      }
    }
  }
  return false
}

/**
 * 全局权限控制
 */
export function filterGlobalPermission(el, binding) {
  var permissionList = []
  var allPermissionList = []

  //let authList = Vue.ls.get(USER_AUTH);
  let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || '[]')
  for (var auth of authList) {
    if (auth.type !== '2') {
      permissionList.push(auth)
    }
  }
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || '[]')
  for (var gauth of allAuthList) {
    if (gauth.type !== '2') {
      allPermissionList.push(gauth)
    }
  }
  //设置全局配置是否有命中
  var invalidFlag = false //无效命中
  if (
    allPermissionList != null &&
    allPermissionList !== '' &&
    allPermissionList !== undefined &&
    allPermissionList.length > 0
  ) {
    for (var itemG of allPermissionList) {
      if (binding.value === itemG.action) {
        if (itemG.status === '0') {
          invalidFlag = true
          break
        }
      }
    }
  }
  if (invalidFlag) {
    return
  }
  if (permissionList === null || permissionList === '' || permissionList === undefined || permissionList.length <= 0) {
    el.parentNode.removeChild(el)
    return
  }
  let permissions = []
  for (var item of permissionList) {
    if (item.type !== '2') {
      permissions.push(item.action)
    }
  }
  if (!permissions.includes(binding.value)) {
    el.parentNode.removeChild(el)
  }
}

export default hasPermission
