import moment from 'moment'

export function date(time) {
  if (!time) {
    return ''
  }
  return moment(time).format('YYYY-MM-DD')
}

export function datetime(time) {
  if (!time) {
    return ''
  }
  return moment(time).format('YYYY-MM-DD HH:mm')
}

export function datetimeseconds(time) {
  if (!time) {
    return ''
  }
  return moment(time).format('YYYY-MM-DD HH:mm:ss')
}

export function chineseAmount(amount) {
  if (amount && amount > 100) {
    return (amount / 10000).toFixed(2) + ' 亿元'
  }

  return amount + ' 万元'
}

export function fixed(amount, digits) {
  if (isNaN(amount) || amount === null || amount === undefined) {
    return '-'
  }

  return amount.toFixed(digits)
}

export function duration(time, now = new Date()) {
  const diff = moment(now).diff(moment(time)) // 做差
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const mins = Math.floor(diff / (1000 * 60))
  if (days > 0) {
    return days + '天'
  } else if (hours > 0) {
    return hours + '小时'
  } else if (mins > 0) {
    return mins + '分钟'
  }
}
