import { http } from '@/utils/http/axios';
import type {
  TaskManagementListQuery,
  TaskExecutionListQuery,
  TaskManagementListResponse,
  TaskExecutionListResponse,
  TaskExecutionDetailResponse,
  SaveTaskDetailRequest,
} from '@/types/disaster-precaution';
import type { SpanTableData } from '@/views/disaster/disaster-precaution/src/type';
import type { PersonGroupItem } from '@/types/person-group/type';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';
import type { TaskManagementRuleForm } from '@/views/disaster/disaster-precaution/src/type';
/**
 * 获取任务管理表格数据
 */
export function getTaskManagementList(query: QueryPageRequest<TaskManagementListQuery>) {
  return http.request<QueryPageResponse<TaskManagementListResponse>>({
    url: '/preventInspectTask/queryPreventInspectTaskPage',
    method: 'post',
    data: query,
  });
}

/**
 * 添加任务管理项
 */
export const createTaskManagementItem = (data: Omit<TaskManagementRuleForm, 'realname'>) => {
  return http.request({
    url: '/preventInspectTask/savePreventInspectTask',
    method: 'post',
    data,
  });
};

/**
 * 编辑任务管理项
 */
export const editTaskManagementItem = (data: Omit<TaskManagementRuleForm, 'realname'>) => {
  return http.request({
    url: '/preventInspectTask/updatePreventInspectTask',
    method: 'post',
    data,
  });
};

/**
 * 查看任务管理项
 */
export const viewTaskManagementDetail = (preventInspectTaskId: number) => {
  return http.request<TaskManagementRuleForm>({
    url: '/preventInspectTask/queryPreventInspectTaskById',
    method: 'get',
    params: {
      preventInspectTaskId,
    },
  });
};

/**
 * 发布任务管理任务 批量/单个
 */
export const publishTaskManagementItem = (preventInspectTaskIds: number[] | number) => {
  return http.request({
    url: `/preventInspectTask/updateTaskEffect?preventInspectTaskIds=${preventInspectTaskIds}`,
    method: 'post',
  });
};

/**
 * 撤回任务管理任务 批量/单个
 */
export const withdrawTaskManagementItem = (preventInspectTaskIds: number[] | number) => {
  return http.request({
    url: `/preventInspectTask/updateTaskLossEffect?preventInspectTaskIds=${preventInspectTaskIds}`,
    method: 'post',
  });
};

/**
 * 删除任务管理任务 批量/单个
 */
export const deleteTaskManagementItem = (preventInspectTaskIds: number[] | number) => {
  return http.request({
    url: `/preventInspectTask/deletePreventInspectTasks?preventInspectTaskIds=${preventInspectTaskIds}`,
    method: 'post',
  });
};
/**
 * 获取任务执行表格数据
 */
export function getTaskExecutionList(query: QueryPageRequest<TaskExecutionListQuery>) {
  return http.request<QueryPageResponse<TaskExecutionListResponse>>({
    url: '/preventInspectTask/queryTaskExecutePage',
    method: 'post',
    data: query,
  });
}

/**
 * 查询检查人员
 */
export const getTaskInspectorList = (preventInspectTaskId: number) => {
  return http.request<PersonGroupItem[]>({
    url: '/preventInspectTask/queryTaskInspector',
    method: 'get',
    params: {
      preventInspectTaskId,
    },
  });
};

/**
 * 查询执行任务详情
 */
export const getTaskExecutionDetail = (preventInspectTaskId: number) => {
  return http.request<TaskExecutionDetailResponse>({
    url: '/preventInspectTask/queryTaskExecuteDetail',
    method: 'get',
    params: { preventInspectTaskId },
  });
};

/**
 * 执行任务检查
 */
export const saveTaskDetail = (data: SaveTaskDetailRequest) => {
  return http.request({
    url: '/preventInspectTask/updateTaskInspectDetail',
    method: 'post',
    data,
  });
};

/**
 * 撤回检查任务
 */
export const withdrawTaskInspect = (preventInspectTaskId: number) => {
  return http.request({
    url: '/preventInspectTask/updateTaskInspectState',
    method: 'get',
    params: {
      preventInspectTaskId,
    },
  });
};

/**
 * 撤回审批任务
 */
export const withdrawTaskApproval = (preventInspectTaskId: number) => {
  return http.request({
    url: '/preventInspectTask/updateTaskReviewState',
    method: 'get',
    params: { preventInspectTaskId },
  });
};

/**
 * 执行任务审批
 */
export const saveTaskApproval = (data: SaveTaskDetailRequest) => {
  return http.request({
    url: '/preventInspectTask/updateTaskReviewDetail',
    method: 'post',
    data,
  });
};

/**
 * 查询模板详情
 */
export const getTaskTemplateDetail = (templateId: number) => {
  return http.request<SpanTableData[]>({
    url: '/preventInspectTask/queryInspectTemplateDetail',
    method: 'get',
    params: { templateId },
  });
};

/**
 * 添加检查人员
 */
export const updateTaskInspector = (data: { id: number; inspectorIdList: number[] }) => {
  return http.request({
    url: '/preventInspectTask/updateTaskInspector',
    method: 'post',
    data,
  });
};

/**
 * 导出检查任务详情PDF
 */
export const exportTaskDetailPDF = (preventInspectTaskId: number) => {
  return http.request({
    url: `/preventInspectTask/exportTaskDetail?preventInspectTaskId=${preventInspectTaskId}`,
    method: 'post',
    responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};
