export const DASH = '—'

export function formatPrice(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return String(val)
  return n % 1 === 0 ? String(n) : n.toFixed(2)
}

export function formatWanAmount(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return DASH
  return `${(n / 10000).toFixed(2)} 万元`
}

export function formatYuanAmount(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return DASH
  return `${n.toFixed(2)} 元`
}

export function formatCount(val, unit) {
  if (val == null || val === '') return DASH
  return `${val} ${unit}`
}

export function formatShortDate(ymd) {
  if (!ymd) return ''
  const s = String(ymd).slice(0, 10)
  const parts = s.split('-')
  if (parts.length >= 3) {
    return `${parts[1]}-${parts[2]}`
  }
  return s
}

export function formatDateTime(val) {
  if (!val) return DASH
  const s = String(val)
  return s.length >= 19 ? s.slice(0, 19) : s.slice(0, 10)
}

export function formatCurrency(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return DASH
  return `¥ ${n.toFixed(2)}`
}

export function toWanNumber(val) {
  if (val == null || val === '') return 0
  const n = Number(val)
  if (Number.isNaN(n)) return 0
  return Number((n / 10000).toFixed(4))
}

export function formatPercent(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return DASH
  return `${n % 1 === 0 ? n : n.toFixed(1)} %`
}

export function formatWanPerHead(val) {
  if (val == null || val === '') return DASH
  const n = Number(val)
  if (Number.isNaN(n)) return DASH
  return `${(n / 10000).toFixed(2)} 万元/头`
}
