/**
 * create 2020/6/22  haojianliang
*/

// 处理饼图数据
export function hanlderPieData(data = [], key) {
  return data.map((item, index) => item[key] || item[index])
}
// 处理图片打不开
export function handlerErrorAvatar() {
  const img = event.srcElement
  addClass(img, 'no-avatar')
  img.onerror = null // 防止闪图
}
// 处理趋势图数据
export function hanlderTrendData(data = [], key) {
  return data.map(item => item[key] || 0)
}

// 合并两个数组，将id相同的数组合并去重
export function combineArr(listData = [], records = [], id) {
  const arrTemp = []
  listData.map(data => {
    records.map(item => {
      if (data[id] === item[id]) {
        data = { ...data, ...item }
      }
    })
    arrTemp.push(data)
  })
  return arrTemp
}
//  dom添加class
export function addClass(el, cls) {
  if (!el) return
  var curClass = el.className
  var classes = (cls || '').split(' ')

  for (var i = 0, j = classes.length; i < j; i++) {
    var clsName = classes[i]
    if (!clsName) continue

    if (el.classList) {
      el.classList.add(clsName)
    } else if (!hasClass(el, clsName)) {
      curClass += ' ' + clsName
    }
  }
  if (!el.classList) {
    el.className = curClass
  }
}

// 判断dom 是否有某个class
export function hasClass(el, cls) {
  if (!el || !cls) return false
  if (cls.indexOf(' ') !== -1) throw new Error('className should not contain space.')
  if (el.classList) {
    return el.classList.contains(cls)
  } else {
    return (' ' + el.className + ' ').indexOf(' ' + cls + ' ') > -1
  }
}
