import { http } from '@/utils/http/axios';
import type {
  PlanEmergencyListQuery,
  PlanEmergencyListResponse,
  AddEmergencyPlanParam,
  ApprovalProcessParam,
  PlanApprovalListQuery,
  PlanApprovalListResponse,
  ApprovalParam,
  ApprovalProcessResponse,
} from '@/types/emergency-plan';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';

/**
 * 分页查询应急预案列表
 */
export const getEmergencyPlanList = (data: QueryPageRequest<PlanEmergencyListQuery>) => {
  return http.request<QueryPageResponse<PlanEmergencyListResponse>>({
    url: '/emergencyPlan/queryEmergencyPlanPage',
    method: 'post',
    data,
  });
};

/**
 * 创建应急预案
 */
export const createEmergencyPlan = (data: AddEmergencyPlanParam) => {
  return http.request({
    url: '/emergencyPlan/saveEmergencyPlan',
    method: 'post',
    data,
  });
};

/**
 * 删除应急预案
 */
export const deleteEmergencyPlan = (emergencyPlanId: number) => {
  return http.request<ApprovalProcessResponse[]>({
    url: `/emergencyPlan/deleteEmergencyPlan?emergencyPlanId=${emergencyPlanId}`,
    method: 'delete',
  });
};

/**
 * 更新应急预案启停状态
 */
export const updateEmergencyPlanDisabled = (data: { planId: number; isDisabled: number }) => {
  return http.request({
    url: '/emergencyPlan/updateEmergencyPlanStatus',
    method: 'post',
    data,
  });
};

/**
 * 查询应急预案详情
 */
export const queryEmergencyPlan = (emergencyPlanId: number) => {
  return http.request({
    url: '/emergencyPlan/queryEmergencyPlanDetail',
    method: 'get',
    params: { emergencyPlanId },
  });
};

/**
 * 更新应急预案详情
 */
export const editEmergencyPlan = (data: AddEmergencyPlanParam & { id: number }) => {
  return http.request({
    url: '/emergencyPlan/updateEmergencyPlan',
    method: 'post',
    data,
  });
};

/**
 * 提交审批流程
 */
export const submitApprovalProcess = (data: ApprovalProcessParam) => {
  return http.request({
    url: '/emergencyPlan/saveApprovalProcess',
    method: 'post',
    data,
  });
};

/**
 * 查询预案审批列表
 */
export const getEmergencyApprovalList = (data: QueryPageRequest<PlanApprovalListQuery>) => {
  return http.request<QueryPageResponse<PlanApprovalListResponse>>({
    url: '/emergencyPlan/queryEmergencyPlanApprovalPage',
    method: 'post',
    data,
  });
};

/**
 * 提交审批
 */
export const submitApproval = (data: ApprovalParam) => {
  return http.request({
    url: '/emergencyPlan/saveApproval',
    method: 'post',
    data,
  });
};

/**
 * 查询审批流程
 */
export const queryApprovalProcess = (approvalTemplateId: number, sourceId: number) => {
  return http.request({
    url: '/approvalManagement/queryApprovalProcess',
    method: 'post',
    data: {
      approvalTemplateId,
      sourceId,
    },
  });
};
