import request from '@/utils/request'

const baseUrl = '/emergency/dispatch/tasks'

export function listDispatchTasks(params) {
  return request({
    url: `${baseUrl}/list`,
    method: 'get',
    params
  })
}

export function listMyDispatchTasks(params) {
  return request({
    url: `${baseUrl}/my`,
    method: 'get',
    params
  })
}

export function getDispatchTask(id) {
  return request({
    url: `${baseUrl}/${id}`,
    method: 'get'
  })
}

export function getDispatchTaskByNo(taskNo) {
  return request({
    url: `${baseUrl}/no/${taskNo}`,
    method: 'get'
  })
}

export function addDispatchTask(data) {
  return request({
    url: baseUrl,
    method: 'post',
    data
  })
}

export function updateDispatchTask(id, data) {
  return request({
    url: `${baseUrl}/${id}`,
    method: 'put',
    data
  })
}

export function removeDispatchTasks(ids) {
  return request({
    url: `${baseUrl}/${ids}`,
    method: 'delete'
  })
}

export function publishDispatchTask(id) {
  return request({
    url: `${baseUrl}/${id}/publish`,
    method: 'post'
  })
}

export function markDispatchTaskInProgress(id) {
  return request({
    url: `${baseUrl}/${id}/in-progress`,
    method: 'post'
  })
}

export function closeDispatchTask(id, data) {
  return request({
    url: `${baseUrl}/${id}/close`,
    method: 'post',
    data
  })
}

export function abortDispatchTask(id, data) {
  return request({
    url: `${baseUrl}/${id}/abort`,
    method: 'post',
    data
  })
}

export function addDispatchParticipants(id, participants) {
  return request({
    url: `${baseUrl}/${id}/participants`,
    method: 'post',
    data: participants
  })
}

export function removeDispatchParticipant(id, participantId) {
  return request({
    url: `${baseUrl}/${id}/participants/${participantId}`,
    method: 'delete'
  })
}

export function upsertDispatchSteps(id, steps) {
  return request({
    url: `${baseUrl}/${id}/steps`,
    method: 'put',
    data: steps
  })
}

export function attachDispatchResourceLinks(id, links) {
  return request({
    url: `${baseUrl}/${id}/resource-links`,
    method: 'post',
    data: links
  })
}

export function listDispatchDisposals(id) {
  return request({
    url: `${baseUrl}/${id}/disposals`,
    method: 'get'
  })
}

export function addDispatchDisposal(id, data) {
  return request({
    url: `${baseUrl}/${id}/disposals`,
    method: 'post',
    data
  })
}

export function updateDispatchDisposal(taskId, disposalId, data) {
  return request({
    url: `${baseUrl}/${taskId}/disposals/${disposalId}`,
    method: 'put',
    data
  })
}

export function listDispatchLogs(id) {
  return request({
    url: `${baseUrl}/${id}/logs`,
    method: 'get'
  })
}

export function listDispatchParticipants(id) {
  return request({
    url: `${baseUrl}/${id}/participants`,
    method: 'get'
  })
}

export function listDispatchSteps(id) {
  return request({
    url: `${baseUrl}/${id}/steps`,
    method: 'get'
  })
}

export function startDispatchStep(taskId, stepId) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/start`,
    method: 'post'
  })
}

export function completeDispatchStep(taskId, stepId, remark) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/complete`,
    method: 'post',
    data: remark ? { remark } : {}
  })
}

export function failDispatchStep(taskId, stepId, reason) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/fail`,
    method: 'post',
    data: reason ? { reason } : {}
  })
}

export function listDispatchResourceLinks(taskId) {
  return request({
    url: `${baseUrl}/${taskId}/resource-links`,
    method: 'get'
  })
}

export function listDispatchStepResources(taskId, stepId) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/resource-links`,
    method: 'get'
  })
}

export function allocateMaterialToStep(taskId, stepId, data) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/materials`,
    method: 'post',
    data
  })
}

export function allocateEquipmentToStep(taskId, stepId, data) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/equipment`,
    method: 'post',
    data
  })
}

export function allocateVehicleToStep(taskId, stepId, data) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/vehicles`,
    method: 'post',
    data
  })
}

export function removeStepResource(taskId, stepId, linkId) {
  return request({
    url: `${baseUrl}/${taskId}/steps/${stepId}/resource-links/${linkId}`,
    method: 'delete'
  })
}

// 物资生命周期管理
export function releaseMaterialLink(taskId, linkId) {
  return request({
    url: `${baseUrl}/${taskId}/resource-links/${linkId}/release`,
    method: 'post'
  })
}

/**
 * 消耗物资链接
 * @param {Number|String} taskId - 任务ID
 * @param {Number|String} linkId - 资源链接ID
 * @returns {Promise} 请求Promise
 */
export function consumeMaterialLink(taskId, linkId) {
  return request({
    url: `${baseUrl}/${taskId}/resource-links/${linkId}/consume`,
    method: 'post'
  })
}

/**
 * 归还物资链接
 * @param {Number|String} taskId - 任务ID
 * @param {Number|String} linkId - 资源链接ID
 * @returns {Promise} 请求Promise
 */
export function returnMaterialLink(taskId, linkId) {
  return request({
    url: `${baseUrl}/${taskId}/resource-links/${linkId}/return`,
    method: 'post'
  })
}