import { http } from '@/utils/http/axios';
import type { QueryPageRequest, QueryPageResponse } from '@/types/basic-query';
import type {
  EmergencySupplyListQuery,
  EmergencySupplyListResponse,
  EmergencySupplyDetailResponse,
  AddEmergencyItemForm,
  EditEmergencyItemForm,
  ChangeRecordListRes,
  InventoryTaskListRes,
  InventoryCheckListQuery,
  InventoryCheckListResponse,
  SupplyRequestListQuery,
  SupplyRequestListItem,
  SupplyRequestDetailItem,
  ReceiveSupplyRequestDetailForm,
  AddSupplyQuery,
  NotifyDepartmentForm,
} from '@/types/emergency-supplier';

/**
 * 获取物资清单列表
 */
export function getEmergencySupplyList(query: QueryPageRequest<EmergencySupplyListQuery>) {
  return http.request<QueryPageResponse<EmergencySupplyListResponse>>({
    url: '/emergencySupplies/queryEmergencySuppliesInfoPage',
    method: 'post',
    data: query,
  });
}
/**
 * 删除应急物资
 */
export const deleteEmergencySupplyList = (supplyId: number) => {
  return http.request({
    url: `/emergencySupplies/deleteEmergencySupply?supplyId=${supplyId}`,
    method: 'post',
  });
};
/**
 * 查询应急物资详情
 */
export const getEmergencySupplyDetail = (supplyId: number) => {
  return http.request<EmergencySupplyDetailResponse>({
    url: `/emergencySupplies/queryEmergencySupplyInfo?supplyId=${supplyId}`,
    method: 'get',
  });
};
/**
 * 添加应急物资
 */
export const addEmergencySupply = (data: AddEmergencyItemForm) => {
  return http.request({
    url: '/emergencySupplies/saveEmergencySupply',
    method: 'post',
    data,
  });
};
/**
 * 修改应急物资
 */
export const editEmergencySupply = (data: EditEmergencyItemForm) => {
  return http.request({
    url: '/emergencySupplies/updateEmergencySupply',
    method: 'post',
    data,
  });
};
/**
 * 查询物资变更记录
 */
export const getSupplyChangeRecord = (supplyId: number) => {
  return http.request<ChangeRecordListRes[]>({
    url: `/emergencySupplies/querySupplyChangeRecord?supplyId=${supplyId}`,
    method: 'post',
  });
};
/**
 * 获取盘点任务列表
 */
export const getInventoryTaskList = () => {
  return http.request<InventoryTaskListRes[]>({
    url: '/emergencySupplies/queryInventoryTaskList',
    method: 'get',
  });
};

/**
 * 查询盘点物资
 */
export const getInventoryCheckList = (query: QueryPageRequest<InventoryCheckListQuery>) => {
  return http.request<QueryPageResponse<InventoryCheckListResponse>>({
    url: '/emergencySupplies/queryInventorySuppliesPage',
    method: 'post',
    data: query,
  });
};
/**
 * 盘点任务确认
 */
export const confirmInventoryTask = (taskId: number) => {
  return http.request({
    url: `/emergencySupplies/updateInventoryTaskConfirm?taskId=${taskId}`,
    method: 'post',
  });
};
/**
 * 查询是否可以盘点
 */
export const getInventoryTaskConfirm = (taskId: number) => {
  return http.request<{ taskState: number }>({
    url: '/emergencySupplies/queryInventoryTaskById',
    method: 'get',
    params: { taskId },
  });
};
/**
 * 发起盘点任务
 */
export const saveInventoryTask = (taskName: string, endTime: string) => {
  return http.request<{ taskState: number }>({
    url: '/emergencySupplies/saveInventoryTask',
    method: 'post',
    data: { taskName, endTime },
  });
};

/**
 * 物资报废
 */
export const discardEmergencySupply = (data: {
  supplyId: number;
  quantity: number;
  scrapReason: string;
  scrapImage: string;
}) => {
  return http.request({
    url: '/emergencySupplies/scrapEmergencySupplies',
    method: 'post',
    data,
  });
};
/**
 * 导出物资报废记录
 */
export const exportDiscardRecord = (startTime: string, endTime: string) => {
  return http.request(
    {
      url: '/emergencySupplies/exportSuppliesScrapRecord',
      method: 'post',
      data: { startTime, endTime },
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};

/**
 * 分页查询应急物资申领计划列表
 */
export const getSupplyRequestList = (data: QueryPageRequest<SupplyRequestListQuery>) => {
  return http.request<QueryPageResponse<SupplyRequestListItem>>({
    url: '/emergencySupplies/queryEmergencySuppliesRequestPlanPage',
    method: 'post',
    data,
  });
};
/**
 * 通过id查询应急物资申领计划信息
 */
export const getSupplyRequestInfoById = (planId: number) => {
  return http.request<SupplyRequestListItem>({
    url: `/emergencySupplies/queryEmergencySuppliesRequestPlan?planId=${planId}`,
    method: 'get',
  });
};
/**
 * 删除应急物资申领计划
 */
export const deleteSupplyRequest = (planId: number) => {
  return http.request({
    url: `/emergencySupplies/deleteEmergencySuppliesRequestPlan?planId=${planId}`,
    method: 'delete',
  });
};
/**
 * 创建物资申领计划
 */
export const createSupplyRequest = (data: SupplyRequestListItem) => {
  return http.request({
    url: '/emergencySupplies/saveEmergencySuppliesRequestPlan',
    method: 'post',
    data,
  });
};
/**
 * 编辑物资申领计划
 */
export const updateSupplyRequest = (data: SupplyRequestListItem) => {
  return http.request({
    url: '/emergencySupplies/updateEmergencySuppliesRequestPlan',
    method: 'put',
    data,
  });
};
/**
 * 查询应急物资申领计划详情
 */
export const getSupplyRequestDetail = (planId: number) => {
  return http.request<SupplyRequestDetailItem[]>({
    url: `/emergencySupplies/queryEmergencySuppliesRequestDetail?planId=${planId}`,
    method: 'get',
  });
};
/**
 * 创建需求物资
 */
export const createSupplyRequestDetail = (data: AddSupplyQuery) => {
  return http.request({
    url: '/emergencySupplies/saveRequestSupply',
    method: 'post',
    data,
  });
};
/**
 * 编辑需求物资
 */
export const updateSupplyRequestDetail = (data: AddSupplyQuery) => {
  return http.request({
    url: '/emergencySupplies/updateRequestSupply',
    method: 'put',
    data,
  });
};
/**
 * 删除需求物资
 */
export const deleteSupplyRequestDetail = (infoId: number) => {
  return http.request({
    url: `/emergencySupplies/deleteRequestSupply?infoId=${infoId}`,
    method: 'delete',
  });
};
/**
 * 领用物资
 */
export const receiveSupplyRequestDetail = (data: ReceiveSupplyRequestDetailForm) => {
  return http.request({
    url: '/emergencySupplies/receiveEmergencySupplies',
    method: 'post',
    data,
  });
};
/**
 * 下载申领记录
 */
export const exportSupplyRequestRecord = (planId: number) => {
  return http.request(
    {
      url: `/emergencySupplies/downloadRequestRecord?planId=${planId}`,
      method: 'get',
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  );
};
/**
 * 获取物资名称列表
 */
export const getSupplyNameList = (planId: number) => {
  return http.request<string[]>({
    url: `/emergencySupplies/queryEmergencySuppliesNameList?planId=${planId}`,
    method: 'get',
  });
};

/**
 * 通知领用物资
 */
export const notifySupplyRequestDetail = (data: NotifyDepartmentForm) => {
  return http.request({
    url: '/emergencySupplies/noticeReceiveSupplies',
    method: 'post',
    data,
  });
};
