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

/** 项目分页查询 */
export function getProjectList(params: {
  id?: number;
  status?: string;
  name?: string;
  rootOrgId: number;
  pageNo?: number;
  pageSize?: number;
}) {
  return httpApp.post<any, ServerPageResponse<Project>>(
    "/api/ess/project/page",
    params
  );
}

/** 更新项目 */
export function updateProject(params: {
  name?: string;
  id?: number;
  rootOrgId?: number;
}) {
  return httpApp.post("/api/ess/project/save", params);
}

/** 删除项目 */
export function deleteProjects(ids: number[]) {
  return httpApp.post(
    `/api/ess/project/delete`,
    new URLSearchParams([["ids", ids.join(",")]])
  );
}

/** 取消计算 */
export function cancelProject(id: number) {
  return httpApp.post(
    `/api/ess/project/cancel`,
    new URLSearchParams([["projectId", id + ""]])
  );
}

/** 重新计算 */
export function restartProject(id: number) {
  return httpApp.post(
    `/api/ess/project/restart`,
    new URLSearchParams([["projectId", id + ""]])
  );
}

/** 项目错误报告 */
export function logsOfProject(id: number) {
  return httpApp
    .post(
      `/api/ess/project/error?` + new URLSearchParams([["projectId", id + ""]]),
      null,
      { responseType: "blob" }
    )
    .then(function (response) {
      return responseToFile(response);
    });
}

/** 导入项目数据 */
export function importProjectDataSource(projectId: number, file: File) {
  const f = new FormData();
  f.append("projectId", projectId + "");
  f.append("file", file);
  return httpApp.post(`/api/ess/task/import`, f);
}
