import { formatDate } from '@/utils/tools'

const gasstationImage = item => {
  return item
}

const formateTData = (date, fmt) => {
  if (date) {
    const sign = date.indexOf('T') >= 0 ? 'T' : ' '
    const times = date.split(sign)
    const time = times[1] && times[1].split(':')

    if (fmt == 'date') {
      return times[0]
    }
    return times[0] + ' ' + (time[0] || '00') + ':' + (time[1] || '00') + (fmt == 'all' ? (':' + (time[2] || '00')) : '')
  }

  return ''
}

const formateZeroToBar = (value, type = 'int') => {
  if (type === 'int') {
    return value || '-'
  } else {
    return value ? (value > 0 && value < 1) ? Number(value).toFixed(2) : parseInt(value) : '-'
  }
}

const formateTextDeleteNULL = (value) => {
  return value ? (value === 'null' ? '-' : value) : '-'
}

const formateMoney = (value) => {
  return currency(value, '', '')
}

const currency = (value, unit, currency, decimals) => {
  const digitsRE = /(\d{3})(?=\d)/g
  value = parseFloat(value)
  if (!isFinite(value) || (!value && value === 0)) return '-'
  currency = currency != null ? currency : '￥'
  decimals = decimals != null ? decimals : 2
  var stringified = Math.abs(value).toFixed(decimals)
  var _int = decimals
    ? stringified.slice(0, -1 - decimals)
    : stringified
  var i = _int.length % 3
  var head = i > 0
    ? (_int.slice(0, i) + (_int.length > 3 ? ',' : ''))
    : ''
  var _float = decimals
    ? stringified.slice(-1 - decimals)
    : ''
  var sign = value < 0 ? '-' : ''
  return sign + currency + head +
    _int.slice(i).replace(digitsRE, '$1,') +
    _float +
    (unit ? ' ' + unit : '')
}

const stockStatus = (value) => {
  if (value == 0) return '已取消'
  else if (value == 1) return '已下单'
  else if (value == 2) return '已确认'
  else if (value == 3) return '已出港'
  else if (value == 4) return '已签收'
  else if (value == 5) return '已完成'
}
const printerStatus = (value) => {
  if (value == 0) return '离线'
  else if (value == 1) return '在线'
  else if (value == 2) return '缺纸'
}

export {
  gasstationImage,
  printerStatus,
  formateTData,
  formatDate,
  currency,
  stockStatus,
  formateMoney,
  formateZeroToBar,
  formateTextDeleteNULL
}
