import request from '@/utils/request'

function buildWarningFormData(payload = {}) {
  const formData = new FormData()
  const warning = payload.warning || {}
  // 后端 @RequestPart("warning") 要求单个名为 "warning" 的 part，内容为整段 JSON 字符串
  if (warning.warningId) {
    formData.append('warning', JSON.stringify(warning))
  } else {
    const { warningId, ...rest } = warning
    formData.append('warning', JSON.stringify(rest))
  }

  // 后端 @RequestPart("alarmIds") 要求单个名为 "alarmIds" 的 part，内容为 JSON 数组字符串
  formData.append('alarmIds', JSON.stringify(payload.alarmIds || []))

  ;(payload.files || []).forEach((file) => {
    formData.append('files', file)
  })

  return formData
}

// 待办预警列表
export function getTodoListData(query) {
  return request({
    url: '/warning/todo/list',
    method: 'get',
    params: query
  })
}

// 预警发布列表/综合查询
export function getdbyjlistData(query) {
  return request({
    url: '/warning/search',
    method: 'get',
    params: query
  })
}

// 保存预警草稿
export function submitYjxxData(data) {
  return request({
    url: '/warning/save',
    method: 'post',
    data: buildWarningFormData(data)
  })
}

// 修改预警
export function updateYjxxData(data) {
  return request({
    url: '/warning/update',
    method: 'put',
    data: buildWarningFormData(data)
  })
}

// 获取字典数据
export function getAlertTypeDictionaryData(query) {
  return request({
    url: '/system/dict/data/list',
    method: 'get',
    params: query
  })
}

// 预警详情
export function getWarningDetailData(warningId) {
  return request({
    url: `/warning/detail/full/${warningId}`,
    method: 'get'
  })
}

// 发布预警
export function releaseYjxxData(warningId) {
  return request({
    url: `/warning/publish/${warningId}`,
    method: 'post'
  })
}

// 删除预警
export function deleteYjxxData(warningId) {
  return request({
    url: '/warning/delete',
    method: 'delete',
    params: {
      warningId
    }
  })
}

// 预警统计
export function getWarningStatisticsData() {
  return request({
    url: '/warning/dashboard/statistics',
    method: 'get'
  })
}

// 预警事项列表
export function getWarningItemListData(query) {
  return request({
    url: '/warning/item/list',
    method: 'get',
    params: query
  })
}

// 根据类型和级别查询原因
export function getWarningReasonListData(query) {
  return request({
    url: '/warning/reason/query',
    method: 'get',
    params: query
  })
}

// 关联报警
export function updateWarningAlarm(data) {
  const params = { ...data };
  // Spring @RequestParam List<String> 期望逗号分隔，axios 默认序列化 alarmIds[]=x 会 400
  if (Array.isArray(params.alarmIds)) {
    params.alarmIds = params.alarmIds.join(',');
  }
  return request({
    url: '/warning/link/alarms',
    method: 'post',
    params
  })
}

// 获取已关联报警列表
export function getLinkedAlarmListData(warningId) {
  return request({
    url: `/warning/link/alarms/${warningId}`,
    method: 'get'
  })
}

// 报警分页列表
export function getAlarmPageData(query) {
  return request({
    url: '/alarmData/findByPage',
    method: 'get',
    params: query
  })
}

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

// 预警解除
export function resolveWarningData(data) {
  return request({
    url: '/warning/resolve',
    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 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 getWarningProcessData(warningId) {
  return request({
    url: `/warning/process/${warningId}`,
    method: 'get'
  })
}

// 预警流程统计
export function getWarningProcessStatisticsData(warningId) {
  return request({
    url: `/warning/process/statistics/${warningId}`,
    method: 'get'
  })
}

// 预警流程可视化路线图
export function getWarningDiagramData(warningId) {
  return request({
    url: `/warning/diagram/${warningId}`,
    method: 'get'
  })
}
