import { format, parseISO } from 'date-fns'
import { zhCN } from 'date-fns/locale'

export function formatDateTime(date: string | Date | null | undefined): string {
  if (!date) {
    return '-'
  }
  
  try {
    const dateObj = typeof date === 'string' ? parseISO(date) : date
    return format(dateObj, 'yyyy-MM-dd HH:mm:ss', { locale: zhCN })
  } catch (error) {
    console.error('时间格式化失败:', error)
    return '-'
  }
}

export function formatDate(date: string | Date | null | undefined): string {
  if (!date) {
    return '-'
  }
  
  try {
    const dateObj = typeof date === 'string' ? parseISO(date) : date
    return format(dateObj, 'yyyy-MM-dd', { locale: zhCN })
  } catch (error) {
    console.error('日期格式化失败:', error)
    return '-'
  }
}

export function formatTime(date: string | Date | null | undefined): string {
  if (!date) {
    return '-'
  }
  
  try {
    const dateObj = typeof date === 'string' ? parseISO(date) : date
    return format(dateObj, 'HH:mm:ss', { locale: zhCN })
  } catch (error) {
    console.error('时间格式化失败:', error)
    return '-'
  }
}
