import { URLS } from "./URL";
import { $ajax } from ".";

export async function downloadExcelTemplate(categoryId: number) {
  const res = await $ajax({
    url: URLS.fileExport,
    type: "get",
    params: {
      category_id: categoryId,
    },
    config: {
      responseType: "blob",
    },
  });
  const blob = new Blob([res!.data]);
  downlaod(blob, "模板.xlsx");
}

export function downlaod(blob: Blob, name: string) {
  const aEle = document.createElement("a");
  aEle.href = URL.createObjectURL(blob);
  aEle.target = "__blank";
  aEle.download = name;
  document.body.appendChild(aEle);
  aEle.click();
  URL.revokeObjectURL(aEle.href);
}
