import { http } from "@/utils/http";
import type { CodeInfo, QueryCodeLogInfo, QueryCodeResult } from "types/code";
import { filterEmptyObject } from "./utils";

/**
 * 获取编码模版
 */
export const getCodeTemplateAPI = () => {
  return http.request<Blob>("get", "/api/code/template", {
    responseType: "blob" // 设置响应类型为 blob
  });
};

/**
 * 获取查询模版
 */
export const getQueryTemplateAPI = () => {
  return http.request<Blob>("get", "/api/code/queryTemplate", {
    responseType: "blob" // 设置响应类型为 blob
  });
};

/**
 * 导入编码
 */
export const getAddCodeAPI = (id: string) => {
  return http.request<null>("get", `/api/code/addCode/${id}`);
};

/**导入编码 */
export const postAddCodeAPI = (data: AddCodeParams) => {
  return http.request<null>("post", `/api/code/add`, {
    data
  });
};

/**删除编码 */
export const deleteCodeAPI = (id: string) => {
  return http.request<null>("delete", `/api/code/delete/${id}`);
};

/**
 * 批量删除编码
 */
export const deleteCodeListAPI = (data: { ids: string[] }) => {
  return http.request<null>("post", `/api/code/deleteList`, { data });
};

/**
 * 更新编码参数
 */
interface AddCodeParams {
  /**编码 */
  code: string;
  /** 编码描述 */
  description: string;
}

/**更新编码 */
export const putUpdateCodeAPI = (data: AddCodeParams, id: string) => {
  return http.request<null>("put", `/api/code/update/${id}`, {
    data
  });
};

/**获取编码详情 */
export const getCodeInfoAPI = (id: string) => {
  return http.request<CodeInfo>("get", `/api/code/info/${id}`);
};

/**
 * 获取编码列表参数
 */
interface GetCodeListParams {
  /** 当前页码 */
  page: number;
  /** 每页条数 */
  pageSize: number;
  /** 编码 */
  code: string;
  /** 编码描述 */
  description: string;
}
/**
 * 获取编码列表
 * @param params
 * @returns
 */
export const getCodeListAPI = (params: GetCodeListParams) => {
  return http.request<QueryListResult<CodeInfo>>("get", "/api/code/list", {
    params: filterEmptyObject(params)
  });
};

/**查询编码 */
export const postCodeInfoAPI = (data: { query_text: string }) => {
  return http.request<QueryCodeResult>("post", `/api/code/query`, {
    data
  });
};

/**查询编码 */
export const getQueryCodeAPI = (id: string) => {
  return http.request<QueryCodeResult>("get", `/api/code/query/${id}`);
};

/**
 * 获取编码日志列表
 * @param params
 * @returns
 */
export const getCodeLogListAPI = (params: {
  page: number;
  pageSize: number;
  startTime?: string;
  endTime?: string;
}) => {
  return http.request<QueryListResult<QueryCodeLogInfo>>(
    "get",
    `/api/code/logList`,
    {
      params: filterEmptyObject(params)
    }
  );
};

/**获取查询编码日志详情 */
export const getCodeLogInfoAPI = (id: string) => {
  return http.request<QueryCodeLogInfo>("get", `/api/code/logInfo/${id}`);
};

/**获取所有查询编码列表 */
export const getCodeLogListAllAPI = (params: {
  startTime?: string;
  endTime?: string;
}) => {
  return http.request<QueryListResult<QueryCodeLogInfo>>(
    "get",
    `/api/code/logList/all`,
    {
      params: filterEmptyObject(params)
    }
  );
};
