
import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export default function checkPermission(value) {
  if (value && value instanceof Array && value.length > 0) {
    const roles = store.getters && store.getters.roles
    const permissionRoles = value

    const hasPermission = roles.some(role => {
      return permissionRoles.includes(role)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like v-permission="['admin','editor']"`)
    return false
  }
}

/**
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export function checkDataPermission() {
  var sessionData =
      store.getters.fmeaId && sessionStorage.getItem(store.getters.fmeaId),
    userId = store.getters.userId,
    compilerUserId = sessionData && JSON.parse(sessionData).compilerUserId,
    counselorUserId = sessionData && JSON.parse(sessionData).counselorUserId
  if (compilerUserId && compilerUserId === userId) {
    // 如果编制者Id===登录用户Id，那么该用户有编辑的权限
    return true
  } 
  // else if (counselorUserId && counselorUserId.indexOf(userId) > -1) {
  //   // 如果登录用户在organizeId="NR0001"部门下的roleCode==="counselor"
  //   // 那么该用户拥有该部门下的全部DFMEA,PFMEA.CONFIG模块的编辑权限
  //   return true
  // } 
  else {
    return false
  }
}
