import { httpApp } from "@/plugins/axiosApp";
import { responseToFile } from "@/utils/utils";
import qs from "qs";
import { Course, ImportResponse, ServerPageResponse } from "@/types";

/** 科目分页查询 */
export function getCourseList(params: {
  code?: string;
  name?: string;
  enable?: boolean;
  type?: string;
  rootOrgId: number;
  pageNo?: number;
  pageSize?: number;
}) {
  return httpApp.post<any, ServerPageResponse<Course>>(
    "/api/ess/course/page",
    params
  );
}

/** 更新科目 */
export function updateCourse(params: {
  code?: string;
  name?: string;
  enable?: boolean;
  id?: number;
  type?: string;
  rootOrgId?: number;
}) {
  return httpApp.post("/api/ess/course/save", params);
}

/** 禁用、启用科目 */
export function toggleCourses(enable: boolean, ids: number[]) {
  return httpApp.post(
    `/api/ess/course/${enable ? "enable" : "disable"}`,
    new URLSearchParams([["ids", ids.join(",")]])
  );
}

/** 导入科目 */
export function importCourses(rootOrgId: number, file: File) {
  const f = new FormData();
  f.append("rootOrgId", rootOrgId + "");
  f.append("file", file);
  return httpApp.post<any, ImportResponse>(`/api/ess/course/import`, f);
}

/** 导出科目 */
export function exportCourses(params: {
  rootOrgId: number;
  code?: string;
  type?: string;
  name?: string;
  enable?: boolean;
}) {
  return httpApp
    .get(
      `/api/ess/course/export?` + qs.stringify(params),
      // new URLSearchParams([
      //   ["rootOrgId", params.rootOrgId + ""],
      //   ["code", params.code],
      //   ["name", params.name],
      //   ["type", params.type],
      //   ["enable", params.enable + ""],
      // ]),
      { responseType: "blob" }
    )
    .then(function (response) {
      return responseToFile(response);
    });
}
