// 数组去重
export function arrayUnique(arr) {
  arr.filter((element, index, arr) => {
    return arr.indexOf(element) === index
  })
}

// 数组去重，数组中值是对象
export function arrayUnique2(arr, name) {
  const hash = {}
  return arr.reduce(function(item, next) {
    hash[next[name]] ? '' : hash[next[name]] = true && item.push(next)
    return item
  }, [])
}

// 时间转时间戳
export function timeToStamp(time) {
  const date = new Date(time)
  return date.getTime()
}

// 数组的深度拷贝
export function arrDeepCopy(arr) {
  const newArr = []
  for (const prop in arr) newArr[prop] = typeof arr[prop] === 'object' ? arrDeepCopy(arr[prop]) : arr[prop]
  return newArr
}

// 对象的深度拷贝
export function objDeepCopy(obj) {
  const newObj = {}
  for (const prop in obj) newObj[prop] = typeof obj[prop] === 'object' ? objDeepCopy(obj[prop]) : obj[prop]
  return newObj
}

function zeroFill(i) {
  if (i >= 0 && i <= 9) {
    return '0' + i
  } else {
    return i
  }
}
// 获取当前时间(yyyy-mm-dd hh:mm:ss)
export function getNowTime() {
  const date = new Date()
  const month = zeroFill(date.getMonth() + 1)
  const day = zeroFill(date.getDate())
  const hour = zeroFill(date.getHours())
  const minute = zeroFill(date.getMinutes())
  const second = zeroFill(date.getSeconds())
  const time = date.getFullYear() + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  return time
}
export function getNowTime2() {
  const date66 = new Date()
  const month66 = zeroFill(date66.getMonth() + 1)
  const day66 = zeroFill(date66.getDate())
  const date = date66.getFullYear() + '-' + month66 + '-' + day66
  // var date = '2019-3-31'
  var arr = date.split('-')
  var year = arr[0] // 获取当前日期的年份
  var month = arr[1] // 获取当前日期的月份
  var day = arr[2] // 获取当前日期的日
  // var days = new Date(year, month, 0)
  // days = days.getDate() // 获取当前日期中月的天数
  var year2 = year
  var month2 = parseInt(month) - 1
  if (month2 === 0) {
    year2 = parseInt(year2) - 1
    month2 = 12
  }
  var day2 = day
  var days2 = new Date(year2, month2, 0)
  days2 = days2.getDate()
  if (day2 > days2) {
    day2 = days2
  }
  if (month2 < 10) {
    month2 = '0' + month2
  }
  var t2 = year2 + '-' + month2 + '-' + day2
  return t2
}
// 获取当前时间(yyyy-mm-dd)
export function getNowShotTime() {
  const date = new Date()
  const month = zeroFill(date.getMonth() + 1)
  const day = zeroFill(date.getDate())
  const time = date.getFullYear() + '-' + month + '-' + day
  return time
}
// 获取当前时间(yyyy/mm/dd)
export function getNowShotTime1() {
  const date = new Date()
  const month = zeroFill(date.getMonth() + 1)
  const day = zeroFill(date.getDate())
  const time = date.getFullYear() + '/' + month + '/' + day
  return time
}
// 获取当前0晨
export function getNowTime1() {
  const date = new Date()
  const month = zeroFill(date.getMonth() + 1)
  const year = zeroFill(date.getFullYear())
  const day = zeroFill(date.getDate())
  let hour = zeroFill(date.getHours())
  hour = hour < 10 ? ('0' + hour) : hour
  let minute = zeroFill(date.getMinutes())
  minute = minute < 10 ? ('0' + minute) : minute
  let second = zeroFill(date.getSeconds())
  second = second < 10 ? ('0' + second) : second
  // const hour = '00'
  // const minute = '00'
  // const second = '00'
  const time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  return time
}
export function getNowTime3(date) {
  const month = zeroFill2(date.getMonth() + 1)
  const day = zeroFill(date.getDate())
  return date.getFullYear() + '-' + month + '-' + day
}
function zeroFill2(i) {
  if (i >= 0 && i <= 9) {
    if (i === 0) {
      return '01'
    } else {
      return '0' + i
    }
  } else {
    return i
  }
}
// 工时统计上个月26-本月25
export function getTime26() {
  const date = new Date()
  const day = date.getDate()
  if (day >= 25) {
    var month = zeroFill(date.getMonth() + 1)
    if (month === 0) {
      month = 12
      const day = 26
      const time = date.getFullYear() - 1 + '-' + month + '-' + day
      return time
    } else {
      const day = 26
      const time = date.getFullYear() + '-' + month + '-' + day
      return time
    }
  } else {
    var month2 = zeroFill(date.getMonth() + 1) - 1
    if (month2 === 0) {
      month2 = 12
      const day = 26
      const time = date.getFullYear() - 1 + '-' + month2 + '-' + day
      return time
    } else {
      const day = 26
      const time = date.getFullYear() + '-' + month2 + '-' + day
      return time
    }
  }
}
export function getTime25() {
  const date = new Date()
  const day = date.getDate()
  if (day >= 25) {
    const month = zeroFill(date.getMonth() + 2)
    const day2 = 25
    const time = date.getFullYear() + '-' + month + '-' + day2
    return time
  } else {
    const month = zeroFill(date.getMonth() + 1)
    const day2 = 25
    const time = date.getFullYear() + '-' + month + '-' + day2
    return time
  }
}
// 判断类型
export function type(target) {
  const ret = typeof (target)
  const template = {
    '[object Array]': 'array',
    '[object Object]': 'object',
    '[object String]': 'string - object',
    '[object Number]': 'Number - object',
    '[object Boolean]': 'Boolean - object'
  }
  if (target === null) {
    return 'null'
  }
  if (ret === 'object') {
    const str = Object.prototype.toString.call(target)
    return template[str]
  } else {
    return ret
  }
}

export function SectionToChinese(section) {
  const chnNumChar = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  const chnUnitChar = ['', '十', '百', '千']
  let strIns = ''
  let chnStr = ''
  let unitPos = 0
  let zero = true
  while (section > 0) {
    const v = section % 10
    if (v === 0) {
      if (!zero) {
        zero = true
        chnStr = chnNumChar[v] + chnStr
      }
    } else {
      zero = false
      strIns = chnNumChar[v]
      strIns += chnUnitChar[unitPos]
      chnStr = strIns + chnStr
    }
    unitPos++
    section = Math.floor(section / 10)
  }
  return chnStr
}

export function sleep(time) {
  return new Promise(resolve => {
    setTimeout(resolve, time)
  })
}

export function groupBy(datas, keys) {
  const list = datas || []
  const groups = []
  const result = []
  list.forEach(v => {
    const key = {}
    keys.forEach(k => {
      key[k] = v[k]
    })
    let group = groups.find(v => {
      return v._key === JSON.stringify(key)
    })
    if (!group) {
      group = {
        _key: JSON.stringify(key),
        key: key
      }
      groups.push(group)
    }
    group.data1 = group.data1 || 0
    group.key.数量 = group.data1 += v.数量
    group.data2 = group.data2 || ''
    group.key.机床图号_组号 = group.data2 += (v.机床图号 || '') + '-' + (v.组号 ? v.组号.split('组')[0] : '') + '<br/>'
    group.data3 = group.data3 || 0
    group.key.合计 = group.data3 += v.合计
  })
  groups.map(v => {
    result.push(v.key)
  })
  return result
}
// 去除js浮点加法bug
export function numAdd(arg1, arg2) {
  var r1, r2, m
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return (arg1 * m + arg2 * m) / m
}
// 去除js浮点减法bug
export function numSubtract(arg1, arg2) {
  var r1, r2, m, n
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  n = (r1 >= r2) ? r1 : r2
  return ((arg1 * m - arg2 * m) / m).toFixed(n)
}
// 去除js浮点乘法bug
export function numMultiply(arg1, arg2) {
  var m = 0
  var s1 = arg1.toString()
  var s2 = arg2.toString()
  try {
    m += s1.split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  try {
    m += s2.split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}
// 去除js浮点除法bug
export function numDivide(arg1, arg2) {
  var t1 = 0
  var t2 = 0
  var r1, r2
  try {
    t1 = arg1.toString().split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  try {
    t2 = arg2.toString().split('.')[1].length
  } catch (e) {
    console.log(e)
  }
  r1 = Math.Number(arg1.toString().replace('.', ''))
  r2 = Math.Number(arg2.toString().replace('.', ''))
  return (r1 / r2) * Math.pow(10, t2 - t1)
}
