// / 下载模板
import axios from "axios";
const FLIE_URL = import.meta.env.VITE_BASE_URL;
import { ElMessage } from "element-plus";
export const GetdownloadDataRosterTemplateApi = async () => {
  const token = localStorage.getItem("token") || "";
  try {
    const response = await axios.get(FLIE_URL + "/quota/downloaQuotaTemplate", {
      responseType: "blob", // 确保接收的是二进制数据
      headers: {
        satoken: token // 将 token 添加到请求头
      }
    });
    // 创建 Blob 对象并生成下载链接
    const blob = new Blob([response.data], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    });
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    link.download = "模板.xlsx"; // 文件名
    document.body.appendChild(link);
    link.click();
    // 清理 DOM
    document.body.removeChild(link);
    URL.revokeObjectURL(link.href);
    ElMessage.success("模板下载成功");
  } catch (error) {
    ElMessage.error("模板下载失败，请重试");
  }
};

export const postUpdateDeptApi = async data => {
  const token = localStorage.getItem("token") || "";
  return axios.post(`${FLIE_URL}/quota/importQuotaTemplate`, data, {
    headers: {
      "Content-Type": "multipart/form-data", // 指定内容类型
      satoken: token // 将 Token 添加到请求头
    }
  });
};

// 考核指标下载
export const assessmentDownloadDataRosterTemplateApi = async type => {
  const token = localStorage.getItem("token") || "";
  const params = { type };
  try {
    const response = await axios.get(
      FLIE_URL + "/assessment/downloadAssessmentTemplate",
      {
        responseType: "blob", // 确保接收的是二进制数据
        headers: {
          satoken: token // 将 token 添加到请求头
        },
        params
      }
    );
    // 创建 Blob 对象并生成下载链接
    const blob = new Blob([response.data], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    });
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    link.download = "模板.xlsx"; // 文件名
    document.body.appendChild(link);
    link.click();
    // 清理 DOM
    document.body.removeChild(link);
    URL.revokeObjectURL(link.href);
    ElMessage.success("模板下载成功");
  } catch (error) {
    ElMessage.error("模板下载失败，请重试");
  }
};
// 上传
export const postImportAssessmentTemplate = async data => {
  const token = localStorage.getItem("token") || "";
  return axios.post(`${FLIE_URL}/assessment/importAssessmentTemplate`, data, {
    headers: {
      "Content-Type": "multipart/form-data", // 指定内容类型
      satoken: token // 将 Token 添加到请求头
    }
  });
};
// 上传文件获取回显的数据
export const getListTable = async data => {
  const token = localStorage.getItem("token") || "";
  return axios.post(`${FLIE_URL}/quota/importQuotaDisplayBackData`, data, {
    headers: {
      "Content-Type": "multipart/form-data", // 指定内容类型
      satoken: token // 将 Token 添加到请求头
    }
  });
};
// 考核管理导入指标名单回显数据
// export const importAssessmentQuotaDisplayBackData = async data => {
//   const token = localStorage.getItem("token") || "";
//   return axios.post(
//     `${FLIE_URL}/assessment/importAssessmentQuotaDisplayBackData`,
//     data,
//     {
//       headers: {
//         "Content-Type": "multipart/form-data", // 指定内容类型
//         satoken: token // 将 Token 添加到请求头
//       }
//     }
//   );
// };

export const importAssessmentQuotaDisplayBackData = async data => {
  const token = localStorage.getItem("token") || "";
  try {
    const response = await axios.post(
      `${FLIE_URL}/assessment/importAssessmentQuotaDisplayBackData`,
      data,
      {
        headers: {
          "Content-Type": "multipart/form-data", // 指定内容类型
          satoken: token // 将 Token 添加到请求头
        }
      }
    );
    return response;
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || "导入失败，请重试");
  }
};
