import { downloadByApi } from "@/plugins/download";
import { $post } from "@/plugins/axios";
import { IMPORT_TEMPLATE_TYPE } from "@/constants/enumerate";

export default {
  data() {
    return {
      tempDownloading: false,
    };
  },
  methods: {
    async downloadTemplate(type) {
      if (this.tempDownloading) return;
      this.tempDownloading = true;

      const res = await downloadByApi(() => {
        return $post(
          "/api/admin/common/download_import_template",
          {},
          {
            responseType: "blob",
            params: {
              type: IMPORT_TEMPLATE_TYPE[type],
            },
          }
        );
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.tempDownloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
  },
};
