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

export function disabledAuthFilter (code, formData) {
  if (nodeDisabledAuth(code, formData)) {
    return true
  } else {
    return globalDisabledAuth(code)
  }
}

function nodeDisabledAuth (code, formData) {
  let permissionList = []
  try {
    if (formData) {
      let bpmList = formData.permissionList
      permissionList = bpmList.filter(item => item.type == '2')
      // for (let bpm of bpmList) {
      //   if(bpm.type == '2') {
      //     permissionList.push(bpm);
      //   }
      // }
    } else {
      return false
    }
  } catch (e) {
  }
  if (permissionList.length == 0) {
    return false
  }

  let permissions = []
  for (let item of permissionList) {
    if (item.type == '2') {
      permissions.push(item.action)
    }
  }
  if (!permissions.includes(code)) {
    return false
  } else {
    for (let item2 of permissionList) {
      if (code === item2.action) {
        return true
      }
    }
  }
  return false
}

function globalDisabledAuth (code) {
  let permissionList = []
  let allPermissionList = []

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