import { http } from '@/utils/http/axios';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';
import {
  DrillPlanItem,
  DrillPlanListSearch,
  CreateEmergencyDrillRuleForm,
  DrillPlanItemDetail,
  DrillPlanRecord,
  ExecuteDrillPlanRuleForm,
  DrillRecordRuleForm,
  DrillApprovalItem,
} from '@/views/emergency/emergency-drill/types';
import type { DrillSignlistQuery, DrillSignListResponse } from '@/types/emergency-drill';

export const queryEnergencyDrillPlanList = (params: QueryPageRequest<DrillPlanListSearch>) => {
  return http.request<QueryPageResponse<DrillPlanItem>>({
    url: '/emergencyDrill/queryEmergencyDrillPlanPage',
    method: 'post',
    data: params,
  });
};

export const createEmergencyDrillPlan = (params: CreateEmergencyDrillRuleForm) => {
  return http.request({
    url: '/emergencyDrill/saveEmergencyDrillPlan',
    method: 'post',
    data: {
      drillScope: params.drillScope,
      drillContent: params.drillContent,
      dueCompleteTime: params.dueCompleteTime,
      responsibleDeptIdList: '[' + params.responsibleDeptIdList?.join(',') + ']',
      coordinateDeptIdList: params.coordinateDeptIdList ? '[' + params.coordinateDeptIdList?.join(',') + ']' : '',
      emergencyPlanId: params.emergencyPlanId,
      approvalTemplateId: params.approvalTemplateId,
    },
  });
};

export const saveEmergencyDrillExecute = (params: ExecuteDrillPlanRuleForm) => {
  return http.request({
    url: '/emergencyDrill/executeEmergencyDrillPlan',
    method: 'post',
    data: {
      drillPlanId: params.drillPlanId,
      drillTime: params.drillTime,
      drillLocation: params.drillLocation,
      personInChargeId: params.personInChargeId,
      drillDeptIdList: params.drillDeptIdList ? '[' + params.drillDeptIdList?.join(',') + ']' : '',
      drillScript: params.drillScript,
      saveOrSubmit: 0,
    },
  });
};
export const submitEmergencyDrillExecute = (params: ExecuteDrillPlanRuleForm) => {
  return http.request({
    url: '/emergencyDrill/executeEmergencyDrillPlan',
    method: 'post',
    data: {
      drillPlanId: params.drillPlanId,
      drillTime: params.drillTime,
      drillLocation: params.drillLocation,
      personInChargeId: params.personInChargeId,
      drillDeptIdList: params.drillDeptIdList ? '[' + params.drillDeptIdList?.join(',') + ']' : '',
      drillScript: params.drillScript,
      saveOrSubmit: 1,
    },
  });
};

export const saveEmergencyDrillRecord = (params: DrillRecordRuleForm) => {
  return http.request({
    url: '/emergencyDrill/saveEmergencyDrillRecord',
    method: 'POST',
    data: {
      ...params,
      saveOrSubmit: 0,
    },
  });
};

export const submitEmergencyDrillRecord = (params: DrillRecordRuleForm) => {
  return http.request({
    url: '/emergencyDrill/saveEmergencyDrillRecord',
    method: 'POST',
    data: {
      ...params,
      saveOrSubmit: 1,
    },
  });
};

export const deleteEmergencyDrillPlan = (id) => {
  return http.request({
    url: `/emergencyDrill/deleteEmergencyDrillPlan?drillPlanId=${id}`,
    method: 'delete',
  });
};

export const queryEmergencyDrillPlanDetail = (id) => {
  return http.request<DrillPlanItemDetail>({
    url: `/emergencyDrill/queryEmergencyDrillPlanDetail?drillPlanId=${id}`,
    method: 'get',
  });
};

export const queryEmergencyDrillRecordInEdit = (id) => {
  return http.request<DrillPlanRecord>({
    url: `/emergencyDrill/queryEmergencyDrillRecord`,
    method: 'post',
    data: { drillPlanId: id, editOrView: 0 },
  });
};

export const queryEmergencyDrillRecordInView = (id) => {
  return http.request<DrillPlanRecord>({
    url: `/emergencyDrill/queryEmergencyDrillRecord`,
    method: 'post',
    data: { drillPlanId: id, editOrView: 1 },
  });
};

export const exportEmergencyDrillRecord = (id) => {
  return http.request(
    {
      url: `/emergencyDrill/exportEmergencyDrillRecord?drillPlanId=${id}`,
      method: 'post',
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};

// 查询应急预案名
export const queryEmergencyPlanDetail = (id) => {
  return http.request<any>({
    url: `/emergencyPlan/queryEmergencyPlanDetail?emergencyPlanId=${id}`,
    method: 'get',
  });
};
/**
 * 查询演练会签列表
 */
export const getDrillSignList = (data: QueryPageRequest<DrillSignlistQuery>) => {
  return http.request<QueryPageResponse<DrillSignListResponse>>({
    url: '/emergencyDrill/queryEmergencyDrillSignListPage',
    method: 'post',
    data,
  });
};

/**
 * 确认演练脚本
 */
export const signDrillScript = (data: { drillPlanId: number; planToParticipateCount: number }) => {
  return http.request({
    url: '/emergencyDrill/signEmergencyDrillScript',
    method: 'post',
    data,
  });
};

/**
 * 演练记录审批
 */
export const approveDrillRecord = (data: { drillPlanId: number; approvalStatus: number; returnReason?: string }) => {
  return http.request({
    url: '/emergencyDrill/approveEmergencyDrillRecord',
    method: 'post',
    data,
  });
};

/**
 * 查询审批模板
 */
export const queryDrillApproval = (approvalTemplateId: number, drillId: number) => {
  return http.request<DrillApprovalItem[]>({
    url: `/approvalManagement/queryApprovalProcess`,
    method: 'post',
    data: {
      approvalTemplateId,
      sourceType: 2,
      sourceId: drillId,
    },
  });
};

/**
 * 获取签到名单文件
 */
export const getSignListFile = (drillPlanId: number) => {
  return http.request(
    {
      url: `/emergencyDrill/downloadSignList?drillPlanId=${drillPlanId}`,
      method: 'get',
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};

export const queryAllEmergencyPlan = () => {
  return http.request<{ id: number; planName: string; status: number }[]>({
    url: `/emergencyPlan/queryAllEmergencyPlan`,
    method: 'get',
  });
};
