import { get, post, del } from "./api.js";

export const FILTER_NAMES = ["隧道工程", "桥梁工程", "路基工程","路面工程","管线工程","交安工程"];

export const DEFAULT_PROJECT_ID = "5e4bde33ec084f1a8673eb59b190dce7";

export const progressApi = {
  getTree(parentId) {
    const params = { projectId: DEFAULT_PROJECT_ID };
    if (parentId) params.parentId = parentId;
    return get("/api/bim/wbs", params).then((res) => {
      const raw = res?.data?.data || [];
      if (!parentId) {
        return raw.filter((item) => FILTER_NAMES.includes(item.name));
      }
      return raw;
    });
  },

  getPjProgress({ projectId, positionIds, period }) {
    console.log("getPjProgress请求:", { projectId, positionIds, period });
    return post("/api/bim/getPjDayListByPositionId", { projectId, positionIds, period }).then((res) => {
      console.log("getPjProgress响应:", res);
      return res?.data?.data || [];
    });
  },

  saveBatch({ partIds, codeIds, modelId }) {
    return post("/api/partCode/batch", { partIds, codeIds, modelId }).then((res) => res);
  },

  deleteBatch({ partIds, codeIds, modelId }) {
    return del("/api/partCode/batch", { partIds, codeIds, modelId }).then((res) => res);
  },

  getByPartId(partId, modelId) {
    const params = modelId ? { modelId } : undefined;
    return get(`/api/partCode/byPart/${partId}`, params).then((res) => {
      return res?.data?.data || res?.data || [];
    });
  },

  getByCodeId(codeId) {
    return get(`/api/partCode/byCode/${codeId}`).then((res) => {
      return res?.data?.data || [];
    });
  },

  findActAmtByPositionId(projectId, positionId, period) {
    return post("/api/bim/findActAmtByPositiond", { projectId, positionId, period }).then((res) => {
      console.log("findActAmtByPositionId响应:", res);
      return res?.data?.data || [];
    });
  },

  calculateProgress(date) {
    return get("/api/bim/progressData", { date }).then((res) => res);
  },

  getProgress(date) {
    return get("/api/bim/progressData/result",{ date }).then((res) => res);
  },

  getCodeWbsMappings() {
    return get("/api/partCode/codeWbsMappings").then((res) => {
      if (res?.code === 200) return res?.data || [];
      throw new Error(res?.message || "Failed to fetch codeWbsMappings");
    });
  },
};
