import request from '@/utils/request'

// ---------- 预案主表 ----------
export function listPlans(query) {
  return request({
    url: '/api/plans',
    method: 'get',
    params: query
  })
}

export function getPlan(planId) {
  return request({
    url: `/api/plans/${planId}`,
    method: 'get'
  })
}

export function addPlan(data) {
  return request({
    url: '/api/plans',
    method: 'post',
    data
  })
}

export function updatePlan(planId, data) {
  return request({
    url: `/api/plans/${planId}`,
    method: 'put',
    data
  })
}

export function deletePlans(planIds) {
  return request({
    url: `/api/plans/${planIds}`,
    method: 'delete'
  })
}

export function publishPlan(planId) {
  return request({
    url: `/api/plans/${planId}/publish`,
    method: 'post'
  })
}

// ---------- 执行对象 ----------
export function listPlanParticipants(planId) {
  return request({
    url: `/api/plans/${planId}/participants`,
    method: 'get'
  })
}

export function addPlanParticipant(planId, data) {
  return request({
    url: `/api/plans/${planId}/participants`,
    method: 'post',
    data
  })
}

export function updatePlanParticipant(planId, participantId, data) {
  return request({
    url: `/api/plans/${planId}/participants/${participantId}`,
    method: 'put',
    data
  })
}

export function deletePlanParticipants(planId, participantIds) {
  return request({
    url: `/api/plans/${planId}/participants/${participantIds}`,
    method: 'delete'
  })
}

// ---------- 行动项 ----------
export function listPlanSteps(planId) {
  return request({
    url: `/api/plans/${planId}/steps`,
    method: 'get'
  })
}

export function addPlanStep(planId, data) {
  return request({
    url: `/api/plans/${planId}/steps`,
    method: 'post',
    data
  })
}

export function updatePlanStep(planId, stepId, data) {
  return request({
    url: `/api/plans/${planId}/steps/${stepId}`,
    method: 'put',
    data
  })
}

export function deletePlanSteps(planId, stepIds) {
  return request({
    url: `/api/plans/${planId}/steps/${stepIds}`,
    method: 'delete'
  })
}

// ---------- 行动项资源 ----------
export function listStepResources(planId, stepId) {
  return request({
    url: `/api/plans/${planId}/steps/${stepId}/resources`,
    method: 'get'
  })
}

export function addStepResource(planId, stepId, data) {
  return request({
    url: `/api/plans/${planId}/steps/${stepId}/resources`,
    method: 'post',
    data
  })
}

export function updateStepResource(planId, stepId, resourceId, data) {
  return request({
    url: `/api/plans/${planId}/steps/${stepId}/resources/${resourceId}`,
    method: 'put',
    data
  })
}

export function deleteStepResources(planId, stepId, resourceIds) {
  return request({
    url: `/api/plans/${planId}/steps/${stepId}/resources/${resourceIds}`,
    method: 'delete'
  })
}

// ---------- 版本 ----------
export function listPlanVersions(planId) {
  return request({
    url: `/api/plans/${planId}/versions`,
    method: 'get'
  })
}

export function createPlanVersion(planId, data) {
  return request({
    url: `/api/plans/${planId}/versions`,
    method: 'post',
    data
  })
}

export function restorePlanVersion(planId, versionId) {
  return request({
    url: `/api/plans/${planId}/versions/${versionId}/restore`,
    method: 'post'
  })
}

// ---------- 使用记录 ----------
export function listPlanUsage(planId) {
  return request({
    url: `/api/plans/${planId}/usage`,
    method: 'get'
  })
}

/**
 * 记录预案使用记录
 * @param {Number} planId 预案ID
 * @param {Object} data 使用记录数据
 * @returns {Promise}
 */
export function addPlanUsage(planId, data) {
  return request({
    url: `/api/plans/${planId}/usage`,
    method: 'post',
    data
  })
}