import Vue from 'vue'
import moment from 'moment'
import 'moment/locale/zh-cn'
moment.locale('zh-cn')

Vue.filter('NumberFormat', function (value) {
  if (!value) {
    return '0'
  }
  const intPartFormat = value.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
  return intPartFormat
})

Vue.filter('AmountFormat', function (val) {
  if (!val) {
    return '0.00'
  }
  val = val.toString().replace(/\$|,/g, '')
  var sign = false
  if (val === Math.abs(val)) {
    sign = true
  }
  val = Math.floor(val * 100 + 0.50000000001)
  var cents = val % 100
  val = Math.floor(val / 100).toString()
  if (cents < 10) {
    cents = '0' + cents
  }
  for (var i = 0; i < Math.floor((val.length - (1 + i)) / 3); i++) {
    val = val.substring(0, val.length - (4 * i + 3)) + ',' + val.substring(val.length - (4 * i + 3))
  }
  return (sign ? '-' : '') + val + '.' + cents
})

Vue.filter('dayjs', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})

Vue.filter('moment', (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') => {
  if (!dataStr) return ''
  return moment(dataStr).format(pattern)
})
