import request from '@/utils/request'

/**
 * 设备预警管理 API
 * 数据源：early_warning 预警体系（/warning/*）
 * 处置人分配复用工单接口：addManholeWorkOrder / operManholeWorkOrder（basic.js）
 */

// 预警综合搜索（名称/编号/类型/级别/状态/位置/发布人/权属单位/时间范围）
export function getWarningSearchData(query) {
  return request({
    url: '/warning/search',
    method: 'get',
    params: query
  })
}

// 待办预警列表（当前用户）
export function getWarningTodoListData(query) {
  return request({
    url: '/warning/todo/list',
    method: 'get',
    params: query
  })
}

// 预警完整详情（基本信息+GIS+处置历史+附件+归档+关联报警）
export function getWarningFullDetailData(warningId) {
  return request({
    url: `/warning/detail/full/${warningId}`,
    method: 'get'
  })
}

// 发布预警（确认异常）
export function publishWarningData(warningId) {
  return request({
    url: `/warning/publish/${warningId}`,
    method: 'post'
  })
}

export function confirmWarningData(data) {
  return request({
    url: '/warning/confirm',
    method: 'post',
    params: data
  })
}

export function misreportWarningData(data) {
  return request({
    url: '/warning/misreport',
    method: 'post',
    params: data
  })
}

export function submitWarningProcessData(data) {
  const formData = new FormData()
  formData.append('process', JSON.stringify({
    warningId: data.warningId,
    processContent: data.processContent
  }))
  ;(data.files || []).forEach(file => formData.append('files', file))
  return request({
    url: '/warning/process',
    method: 'post',
    data: formData
  })
}

export function clearWarningData(data) {
  return request({
    url: '/warning/clear',
    method: 'post',
    params: data
  })
}

export function getWarningSupervisionListData(warningId) {
  return request({
    url: `/warning/supervision/${warningId}`,
    method: 'get'
  })
}

// 解除预警（误报/工单完成）
export function resolveWarningData(data) {
  return request({
    url: '/warning/resolve',
    method: 'post',
    params: data
  })
}

// 预警升级
export function upgradeWarningData(data) {
  return request({
    url: '/warning/upgrade',
    method: 'post',
    params: data
  })
}

// 预警退回重办
export function returnWarningData(data) {
  return request({
    url: '/warning/return',
    method: 'post',
    params: data
  })
}

// 预警督办
export function supervisionWarningData(data) {
  return request({
    url: '/warning/supervision',
    method: 'post',
    params: data
  })
}

// 领导批示
export function instructionWarningData(data) {
  return request({
    url: '/warning/instruction',
    method: 'post',
    params: data
  })
}

// 处置历史记录
export function getWarningDisposalHistoryData(warningId) {
  return request({
    url: `/warning/disposal/history/${warningId}`,
    method: 'get'
  })
}

// 附件列表
export function getWarningAttachmentsData(warningId) {
  return request({
    url: `/warning/attachments/${warningId}`,
    method: 'get'
  })
}

export function getWarningAttachmentPreviewData(attachmentId) {
  return request({
    url: `/warning/attachments/${attachmentId}/preview`,
    method: 'get',
    responseType: 'blob'
  })
}

export function getWarningAttachmentDownloadData(attachmentId) {
  return request({
    url: `/warning/attachments/${attachmentId}/download`,
    method: 'get',
    responseType: 'blob'
  })
}

// 流程可视化数据（路线图节点+边）
export function getWarningDiagramData(warningId) {
  return request({
    url: `/warning/diagram/${warningId}`,
    method: 'get'
  })
}

// 仪表盘统计（总数/类型分布/级别分布/近7天趋势）
export function getWarningDashboardStatisticsData() {
  return request({
    url: '/warning/dashboard/statistics',
    method: 'get'
  })
}

// 处置中预警列表
export function getWarningProcessingListData(query) {
  return request({
    url: '/warning/processing/list',
    method: 'get',
    params: query
  })
}

// 已完成预警列表
export function getWarningCompletedListData(query) {
  return request({
    url: '/warning/completed/list',
    method: 'get',
    params: query
  })
}

// 预警归档信息
export function getWarningArchiveData(warningId) {
  return request({
    url: `/warning/archive/${warningId}`,
    method: 'get'
  })
}

// 修改预警（处理反馈时上传附件：warning JSON + files）
export function updateWarningData(data) {
  const formData = new FormData()
  const warning = data.warning || {}
  if (warning.warningId) {
    formData.append('warning', JSON.stringify(warning))
  } else {
    const { warningId, ...rest } = warning
    formData.append('warning', JSON.stringify(rest))
  }
  formData.append('alarmIds', JSON.stringify(data.alarmIds || []))
  ;(data.files || []).forEach((file) => {
    formData.append('files', file)
  })
  return request({
    url: '/warning/update',
    method: 'put',
    data: formData
  })
}
