import { getStatusName, getAlertTypeName, getAlertLevelName } from '../api/warningApi'

// 获取预警ID
export function getWarningId(): number {
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  return Number(currentPage.options.alertId || 0)
}

// 复制到剪贴板
export function copyToClipboard(text: string): void {
  if (uni.setClipboardData) {
    uni.setClipboardData({
      data: text,
      success: () => {
        uni.showToast({
          title: '复制成功',
          icon: 'success'
        })
      },
      fail: () => {
        uni.showToast({
          title: '复制失败',
          icon: 'none'
        })
      }
    })
  }
}

// 显示确认弹窗
export function showConfirmModal(
  title: string,
  content: string,
  confirmCallback: () => void
): void {
  uni.showModal({
    title,
    content,
    success: (res) => {
      if (res.confirm) {
        confirmCallback()
      }
    }
  })
}

// 格式化日期时间
export function formatDateTime(dateString: string): string {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 格式化日期
export function formatDate(dateString: string): string {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  })
}

// 获取状态标签样式
export function getStatusStyle(status: string): string {
  switch (status) {
    case 'unhandled':
      return 'bg-red-100 text-red-600 border-red-200'
    case 'handling':
      return 'bg-blue-100 text-blue-600 border-blue-200'
    case 'resolved':
      return 'bg-green-100 text-green-600 border-green-200'
    case 'false_alarm':
      return 'bg-gray-100 text-gray-600 border-gray-200'
    default:
      return 'bg-gray-100 text-gray-600 border-gray-200'
  }
}

// 导航到详情页
export function navigateToDetail(alertId: number): void {
  uni.navigateTo({
    url: `/pages/warning/detail?id=${alertId}`
  })
}

// 导航到处理页
export function navigateToHandle(alertId: number): void {
  uni.navigateTo({
    url: `/pages/warning/handle?id=${alertId}`
  })
}

// 生成预警列表项的显示数据
export function generateListItemData(item: any) {
  return {
    id: item.alertId,
    alertCode: item.alertCode,
    alertType: item.alertType,
    alertTypeName: getAlertTypeName(item.alertType),
    alertLevel: item.alertLevel,
    alertLevelName: getAlertLevelName(item.alertLevel),
    status: item.status,
    statusName: getStatusName(item.status),
    statusStyle: getStatusStyle(item.status),
    stationName: item.stationName,
    alertTime: formatDateTime(item.alertTime),
    snapshotPathUrl: item.snapshotPathUrl || []
  }
}
