import store from '@/store'

const userInfo = store.userStore.userInfo || {}
const codes = userInfo.userRoleInfoList.map(i => i.code) // 当前用户角色
/**
 * 获取当前用户角色是否有权限看到地区树
 * @param roles 用户的角色
 * @param type 默认是or或者，and并且
 */
export const getIsShowRegionTree = (roles, type = 'or') => {
  var result = type === 'and'
  roles.forEach(data => {
    if (codes.includes(data) && type === 'or') {
      result = true
    } else if (!codes.includes(data) && type === 'and') {
      result = false
    }
  })
  return result
}
/**
 * 获取当前用户角色的地区树的参数
 * @param roles: {[key:string]:boolean}需要判断的角色,key:角色code,value:是否否只是本级
 */
export const getTreeParams = (roles) => {
  if (codes.includes('SUPER_ADMIN') && Object.keys(roles).includes('SUPER_ADMIN')) { // 超管
    return {
      regionCode: 330500,
      regionLevel: 2
    }
  } else if (codes.includes('VISITOR') && Object.keys(roles).includes('VISITOR')) { // 访客
    return {
      regionCode: 330500,
      regionLevel: 2
    }
  } else if (codes.includes('REGION_MANAGER') && Object.keys(roles).includes('REGION_MANAGER')) { // 区管
    return {
      regionCode: Number(userInfo.regionCode),
      regionLevel: userInfo.regionCode === '330500' && !roles['REGION_MANAGER'] ? 2 : 3
    }
  } else if (codes.includes('EXPERT_ADMIN') && Object.keys(roles).includes('EXPERT_ADMIN')) { // 专家管理员
    return {
      regionCode: Number(userInfo.regionCode),
      regionLevel: userInfo.regionCode === '330500' && !roles['EXPERT_ADMIN'] ? 2 : 3
    }
  } else {
    return {
      regionCode: Number(userInfo.regionCode),
      regionLevel: 3
    }
  }
}
