import request from '@/utils/request'

// ============ 排水分区 ============
export function getPartitionPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/partition/page', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function getPartitionById(id) {
  return request({ url: '/api/drainage/partition/' + id, method: 'get' })
}
export function getPartitionList() {
  return request({ url: '/api/drainage/partition/list', method: 'get' })
}
export function addPartition(data) {
  return request({ url: '/api/drainage/partition', method: 'post', data })
}
export function updatePartition(data) {
  return request({ url: '/api/drainage/partition', method: 'put', data })
}
export function deletePartition(id) {
  return request({ url: '/api/drainage/partition/' + id, method: 'delete' })
}
export function batchDeletePartition(ids) {
  return request({ url: '/api/drainage/partition/batch', method: 'delete', data: ids })
}

// ============ 排水管网 ============
export function getPipeNetworkPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/pipeNetwork/page', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function addDrainagePipeNetwork(data) {
  return request({ url: '/api/drainage/pipeNetwork', method: 'post', data })
}
export function updateDrainagePipeNetwork(data) {
  return request({ url: '/api/drainage/pipeNetwork', method: 'put', data })
}
export function deleteDrainagePipeNetwork(id) {
  return request({ url: '/api/drainage/pipeNetwork/' + id, method: 'delete' })
}

// ============ 工程设施 ============
export function getFacilityPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/facility/page', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function addFacility(data) {
  return request({ url: '/api/drainage/facility', method: 'post', data })
}
export function updateFacility(data) {
  return request({ url: '/api/drainage/facility', method: 'put', data })
}
export function deleteFacility(id) {
  return request({ url: '/api/drainage/facility/' + id, method: 'delete' })
}

// ============ 易积水点 ============
export function getWaterloggingPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/waterlogging/page', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function addWaterlogging(data) {
  return request({ url: '/api/drainage/waterlogging', method: 'post', data })
}
export function updateWaterlogging(data) {
  return request({ url: '/api/drainage/waterlogging', method: 'put', data })
}
export function deleteWaterlogging(id) {
  return request({ url: '/api/drainage/waterlogging/' + id, method: 'delete' })
}

// ============ 监测设备管理 ============
export function getEquipmentPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/equipment/base/page', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function getEquipmentById(id) {
  return request({ url: '/api/drainage/equipment/base/' + id, method: 'get', headers: { 'X-Silent-Error': 'true' } })
}
export function getEquipmentList() {
  return request({ url: '/api/drainage/equipment/base/list', method: 'get' })
}
export function addEquipment(data) {
  return request({ url: '/api/drainage/equipment/base', method: 'post', data })
}
export function updateEquipment(data) {
  return request({ url: '/api/drainage/equipment/base', method: 'put', data })
}
export function deleteEquipment(id) {
  return request({ url: '/api/drainage/equipment/base/' + id, method: 'delete' })
}
export function batchDeleteEquipment(ids) {
  return request({ url: '/api/drainage/equipment/base/batch', method: 'delete', data: ids })
}
export function getEquipmentTypeList(parentTypeId) {
  const params = {}
  if (parentTypeId) params.parentTypeId = parentTypeId
  return request({ url: '/api/drainage/equipmentType/list', method: 'get', params })
}

// ============ 设备状态 ============
export function addEquipmentStatus(data) {
  return request({ url: '/api/drainage/equipment/status', method: 'post', data })
}
export function updateEquipmentStatus(data) {
  return request({ url: '/api/drainage/equipment/status', method: 'put', data })
}

// ============ 预警管理 ============

// 分页查询当前预警（今天的预警）
export function getWarningCurrentPage(pageNum, pageSize, params) {
  return request({
    url: '/api/drainage/warning/current/page',
    method: 'get',
    params: { pageNum, pageSize, ...params }
  })
}

// 分页查询历史预警
export function getWarningHistoryPage(pageNum, pageSize, params) {
  return request({
    url: '/api/drainage/warning/history/page',
    method: 'get',
    params: { pageNum, pageSize, ...params }
  })
}

// 查询预警详情
export function getWarningById(id) {
  return request({
    url: `/api/drainage/warning/${id}`,
    method: 'get'
  })
}

// 发布预警
export function addWarning(data) {
  return request({
    url: '/api/drainage/warning',
    method: 'post',
    data
  })
}

// 修改预警
export function updateWarning(data) {
  return request({
    url: '/api/drainage/warning',
    method: 'put',
    data
  })
}

// 更新预警状态
export function updateWarningStatus(id, data) {
  return request({
    url: `/api/drainage/warning/${id}/status`,
    method: 'put',
    params: data
  })
}

// 删除预警
export function deleteWarning(id) {
  return request({
    url: `/api/drainage/warning/${id}`,
    method: 'delete'
  })
}

// 发布预警（带人员）
export function publishWarning(data) {
  return request({
    url: '/api/drainage/warning/publish',
    method: 'post',
    data
  })
}

// 处理预警（指定人员处理，status 0→1）
export function firstApproveWarning(id, data) {
  return request({
    url: `/api/drainage/warning/${id}/firstApprove`,
    method: 'put',
    params: data
  })
}

// 审批预警（管理员审批，status 1→2）
export function finalApproveWarning(id, data) {
  return request({
    url: `/api/drainage/warning/${id}/finalApprove`,
    method: 'put',
    params: data
  })
}

// 排水分区关联设备
export function bindPartitionEquipment(data) {
  return request({ url: '/api/drainage/partition/bindEquipment', method: 'post', data })
}

// 查询所有已被关联的设备ID（排水分区）
export function getBoundEquipmentIds(excludeId) {
  const params = {}
  if (excludeId) params.excludeId = excludeId
  return request({ url: '/api/drainage/partition/boundEquipmentIds', method: 'get', params, headers: { 'X-Silent-Error': 'true' } })
}

// 查询所有已被关联的设备ID（排水管网）
export function getPipeNetworkBoundEquipmentIds(excludeId) {
  const params = {}
  if (excludeId) params.excludeId = excludeId
  return request({ url: '/api/drainage/pipeNetwork/boundEquipmentIds', method: 'get', params, headers: { 'X-Silent-Error': 'true' } })
}

// 排水管网关联设备
export function bindPipeNetworkEquipment(data) {
  return request({ url: '/api/drainage/pipeNetwork/bindEquipment', method: 'post', data })
}

// 工程设施关联设备
export function bindFacilityEquipment(data) {
  return request({ url: '/api/drainage/facility/bindEquipment', method: 'post', data })
}

// 易积水点关联设备
export function bindWaterloggingEquipment(data) {
  return request({ url: '/api/drainage/waterlogging/bindEquipment', method: 'post', data })
}

// 报警数据表查询（alarm_data表）
export function getAlarmDataPage(pageNum, pageSize, params) {
  return request({ url: '/alarmData/findByPage', method: 'get', params: { pageNum, pageSize, ...params } })
}

// 处理报警（审核提交）
export function handleAlarm(alarmId, handleUser, handleRemark) {
  return request({
    url: `/api/alarm/handle/${alarmId}`,
    method: 'put',
    params: { handleUser, handleRemark }
  })
}

// 根据ID获取报警详情
export function getAlarmById(id) {
  return request({
    url: `/api/alarm/${id}`,
    method: 'get'
  })
}

// 雷达流量计 - 获取设备最新监测数据
export function getRadarDataByCode(deviceCode) {
  return request({ url: '/api/radar/data/latest', method: 'get', params: { deviceCode } })
}

// 雷达流量计 - 历史数据分页查询（用于监测曲线）
export function getRadarDataHistory(deviceCode, startTime, endTime) {
  return request({ url: '/api/radar/data/page', method: 'get', params: { deviceCode, startTime, endTime, pageNum: 1, pageSize: 9999 } })
}

// ============ 泵站 (water_pump_station) ============
export function getPumpStationPage(pageNum, pageSize, params) {
  return request({ url: '/api/drainage/pumpStation/page', method: 'get', params: { pageNum, pageSize, ...params } })
}

// 根据ID查询泵站详情
export function getPumpStationById(id) {
  return request({ url: '/api/drainage/pumpStation/' + id, method: 'get', headers: { 'X-Silent-Error': 'true' } })
}

// 泵站近三个月月度统计
export function getPumpStationMonthlyStats(ids, sinceDate) {
  const params = { ids }
  if (sinceDate) params.sinceDate = sinceDate
  return request({ url: '/api/drainage/pumpStation/monthlyStats', method: 'get', params })
}

// 工程设施关联泵站
export function bindPumpStation(data) {
  return request({ url: '/api/drainage/facility/bindPumpStation', method: 'post', data })
}

// ============ 工单（运维派单）============
// 新增工单（派单）
export function addWorkOrder(data) {
  return request({ url: '/manhole/workOrder/addWorkOrder', method: 'post', data })
}

// ============ 预警阈值配置 ============
export function getThresholdPage(pageNum, pageSize, params) {
  return request({ url: '/warningThreshold/findByPage', method: 'get', params: { pageNum, pageSize, ...params } })
}
export function getThresholdModulePage(pageNum, pageSize, typeName, params) {
  return request({ url: '/warningThreshold/findByModulePage', method: 'get', params: { pageNum, pageSize, typeName, ...params } })
}
export function getThresholdList(params) {
  return request({ url: '/warningThreshold/getWarningThresholdList', method: 'get', params })
}
export function getThresholdById(id) {
  return request({ url: '/warningThreshold/getById/' + id, method: 'get' })
}
export function saveThreshold(data) {
  return request({ url: '/warningThreshold/save', method: 'post', data })
}
export function updateThreshold(data) {
  return request({ url: '/warningThreshold/update', method: 'post', data })
}
export function deleteThreshold(ids) {
  return request({ url: '/warningThreshold/deleteBatch', method: 'post', data: ids })
}
