import { message } from 'ant-design-vue' /// es/notification
export function dateFormat(dateString, fmt) {
  const date = new Date(dateString)
  let ret
  const opt = {
    'Y+': date.getFullYear().toString(), // 年
    'm+': (date.getMonth() + 1).toString(), // 月
    'd+': date.getDate().toString(), // 日
    'H+': date.getHours().toString(), // 时
    'M+': date.getMinutes().toString(), // 分
    'S+': date.getSeconds().toString() // 秒
    // 有其他格式化字符需求可以继续添加，必须转化成字符串
  }
  for (const k in opt) {
    ret = new RegExp('(' + k + ')').exec(fmt)
    if (ret) {
      fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))
    }
  }
  return fmt
}

export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function welcome() {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 LOL', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function() {}
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = s => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

// 复制
export function copyText(text) {
  var tag = document.createElement('input')
  tag.setAttribute('id', 'cp_hgz_input')
  tag.value = text
  document.getElementsByTagName('body')[0].appendChild(tag)
  document.getElementById('cp_hgz_input').select()
  document.execCommand('copy')
  document.getElementById('cp_hgz_input').remove()
  message.success('复制成功')
}
// 截取前面的字符
export function textcontent(text, len) {
  if (text.length > len) {
    return text.substring(0, len) + '...'
  } else {
    return text
  }
}
// 根据内容类型生成复制类型
export function copyTextByContentType(val, type) {
  var text = ''
  // 如果是资讯/活动/轮播图/公告/党建
  if (type === 1) {
    text = '/pageTwo/news/detail?id=' + val + '&type=' + type
  }
  // 如果企业
  else if (type === 2) {
    text = '/pages/phoneList/detail?id=' + val + '&type=' + type
  }
  // 如果是商品
  else if (type === 3) {
    text = '/pageTwo/product/detail?id=' + val + '&type=' + type
  }
  // 如果是活动
  else if (type === 4) {
    text = '/pageTwo/news/detail?id=' + val + '&type=' + type
  }
  // 如果轮播图
  else if (type === 5) {
    text = '/pageTwo/news/detail?id=' + val + '&type=' + type
  }
  // 如果是公告
  else if (type === 6) {
    text = '/pageTwo/news/detail?id=' + val + '&type=' + type
  }
  // 如果党建
  else if (type === 7) {
    text = '/pageTwo/news/detail?id=' + val + '&type=' + type
  }
  // 如果是供货
  else if (type === 8) {
    text = '/pageTwo/product/detail?id=' + val + '&type=' + type
  }
  // 如果是需求
  else if (type === 9) {
    text = '/pageTwo/product/detail?id=' + val + '&type=' + type
  }
  // 如果是会员
  else if (type === 10) {
    text = '/pages/phoneList/detail?id=' + val + '&type=' + type
  }
  var tag = document.createElement('input')
  tag.setAttribute('id', 'cp_hgz_input')
  tag.value = text
  document.getElementsByTagName('body')[0].appendChild(tag)
  document.getElementById('cp_hgz_input').select()
  document.execCommand('copy')
  document.getElementById('cp_hgz_input').remove()
  message.success('复制成功')
}
