import { http } from '@/utils/http/axios';
import type {
  DisposalManagementListResponse,
  DisposalManagementListQuery,
  DisposalManagementTableQuery,
  DisposalManagementTableResponse,
  LossReportReportTaskQuery,
  LossReportReportTaskResponse,
  LossRecordTableQuery,
  LossRecordTableResponse,
  DisposalManagementCreateQuery,
  DisasterReportCreateQuery,
  DisasterReportEditQuery,
  LossRecordCreateQuery,
  LossRecordEditQuery,
  DisposalRectificationCreateQuery,
  DisposalRectificationFormData,
  DisasterLossDetailQuery,
  DisasterLossDetailResponse,
  UpdateDisasterHandleTaskQuery,
} from '@/types/disaster-control';
import type { QueryPageResponse, QueryPageRequest } from '@/types/basic-query';
/**
 * 获取台风灾害管理表格数据
 */
export const getDisasterControlCollapseData = (query: QueryPageRequest<DisposalManagementListQuery>) => {
  return http.request<QueryPageResponse<DisposalManagementListResponse>>({
    url: '/disasterHandle/queryDisasterHandleTaskPage',
    method: 'post',
    data: query,
  });
};

export const getDisasterControlTableData = (query: DisposalManagementTableQuery) => {
  return http.request<DisposalManagementTableResponse[]>({
    url: '/disasterHandle/queryDisasterReportTask',
    method: 'post',
    data: query,
  });
};

/**
 * 创建灾害处置任务
 */
export const createDisasterHandleTask = (query: DisposalManagementCreateQuery) => {
  return http.request({
    url: '/disasterHandle/saveDisasterHandleTask',
    method: 'post',
    data: query,
  });
};

/**
 * 更新灾害处置任务
 */
export const updateDisasterHandleTask = (query: UpdateDisasterHandleTaskQuery) => {
  return http.request({
    url: '/disasterHandle/updateDisasterHandleTask',
    method: 'put',
    data: query,
  });
};

/**
 * 创建灾害处置上报任务
 */
export const createDisasterReportTask = (query: DisasterReportCreateQuery) => {
  return http.request({
    url: '/disasterHandle/saveDisasterReportTask',
    method: 'post',
    data: query,
  });
};

/**
 * 发布灾害处置任务
 */
export const publishDisasterHandleTask = (handleTaskId: number) => {
  return http.request({
    url: `/disasterHandle/publishDisasterHandleTask?handleTaskId=${handleTaskId}`,
    method: 'post',
  });
};

/**
 * 撤回灾害处置任务
 */
export const withdrawDisasterHandleTask = (handleTaskId: number) => {
  return http.request({
    url: `/disasterHandle/revokeDisasterHandleTask?handleTaskId=${handleTaskId}`,
    method: 'post',
  });
};

/**
 * 删除灾害处置任务
 */
export const deleteDisasterHandleTask = (handleTaskId: number) => {
  return http.request({
    url: `/disasterHandle/deleteDisasterHandleTask?handleTaskId=${handleTaskId}`,
    method: 'delete',
  });
};

/**
 * 删除灾害处置上报任务
 */
export const deleteDisasterReportTask = (reportTaskId: number) => {
  return http.request({
    url: `/disasterHandle/deleteDisasterReportTask?reportTaskId=${reportTaskId}`,
    method: 'delete',
  });
};

/**
 * 编辑灾害处置上报任务
 */
export const editDisasterReportTask = (query: DisasterReportEditQuery) => {
  return http.request({
    url: '/disasterHandle/updateDisasterReportTask',
    method: 'put',
    data: query,
  });
};

/**
 * 获取损失上报-上报任务数据
 */
export const getReportTaskList = (query: QueryPageRequest<LossReportReportTaskQuery>) => {
  return http.request<QueryPageResponse<LossReportReportTaskResponse>>({
    url: '/disasterHandle/queryDisasterReportTaskPage',
    method: 'post',
    data: query,
  });
};

export const getLossRecordTableData = (query: LossRecordTableQuery) => {
  return http.request<LossRecordTableResponse[]>({
    url: '/disasterHandle/queryLossRecord',
    method: 'post',
    data: query,
  });
};

/**
 * 添加上报责任人
 */
export const addReporter = (reportTaskId: number, reporterIds: number[]) => {
  return http.request({
    url: `/disasterHandle/addDisasterReportTaskReporters?reportTaskId=${reportTaskId}&reporterIds=${reporterIds.join(
      ',',
    )}`,
    method: 'post',
  });
};

/**
 * 添加整改人
 */
export const addFixer = (reportRecordId: number, fixPrincipals: number[]) => {
  return http.request({
    url: `/disasterHandle/addLossRecordFixPrincipals?reportRecordId=${reportRecordId}&fixPrincipals=${fixPrincipals.join(
      ',',
    )}`,
    method: 'post',
  });
};

/**
 * 创建损失记录
 */
export const createLossRecord = (query: LossRecordCreateQuery) => {
  return http.request({
    url: '/disasterHandle/saveLossRecord',
    method: 'post',
    data: query,
  });
};

/**
 * 编辑损失记录
 */
export const editLossRecord = (query: LossRecordEditQuery) => {
  return http.request({
    url: '/disasterHandle/updateLossRecord',
    method: 'put',
    data: query,
  });
};

/**
 * 删除损失记录
 */
export const deleteLossRecord = (reportRecordId: number) => {
  return http.request({
    url: `/disasterHandle/deleteLossRecord?reportRecordId=${reportRecordId}`,
    method: 'delete',
  });
};

/**
 * 创建处置整改
 */
export const createDisposalRectification = (query: DisposalRectificationCreateQuery) => {
  return http.request({
    url: '/disasterHandle/saveLossFixRecord',
    method: 'post',
    data: query,
  });
};

/**
 * 查询整改列表
 */
export const getRectificationList = (reportRecordIds: number[]) => {
  return http.request<{ disasterLossFixRecordList: DisposalRectificationFormData[] }[]>({
    url: `/disasterHandle/queryLossFixRecord?reportRecordIds=${reportRecordIds.join(',')}`,
    method: 'get',
  });
};

/**
 * 查询灾害损失明细
 */
export const getDisasterLossDetail = (data: DisasterLossDetailQuery) => {
  return http.request<DisasterLossDetailResponse>({
    url: '/disasterHandle/queryDisasterLossDetailByTask',
    method: 'post',
    data,
  });
};

/**
 * 导出损失明细xml
 */
export const exportLossRecordToXML = (data: DisasterLossDetailQuery) => {
  return http.request(
    {
      url: '/disasterHandle/exportDisasterLossDetailByTask',
      method: 'post',
      data,
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};
