// 工具函数集合 — 统一导出

export { showLoading, hideLoading, showToast, request, get, post } from './request.js'
export { storage } from './storage.js'
export { fmtDictName, getDictColor } from './dict.js'

export const formatTime = (time, format = 'YYYY-MM-DD HH:mm:ss') => {
  if (!time) return '-'
  const date = new Date(time)
  if (isNaN(date.getTime())) return '-'
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hour = String(date.getHours()).padStart(2, '0')
  const minute = String(date.getMinutes()).padStart(2, '0')
  const second = String(date.getSeconds()).padStart(2, '0')

  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hour)
    .replace('mm', minute)
    .replace('ss', second)
}

export const formatAmount = (amount, isCent = true) => {
  if (amount === null || amount === undefined || isNaN(amount)) return '0.00'
  const numAmount = parseFloat(amount)
  const yuan = isCent ? numAmount / 100 : numAmount
  return yuan.toFixed(2)
}

/**
 * 复制文本到剪贴板
 * @param {string} text - 要复制的文本
 */
export const copyText = async (text) => {
  if (!text && text !== 0) {
    showToast('内容为空，无法复制')
    return
  }
  try {
    await uni.setClipboardData({ data: String(text) })
    showToast('已复制', 'success')
  } catch (e) {
    console.error('复制失败:', e)
    showToast('复制失败')
  }
}
