<template>
  <div>
    <div class="d1">
      第一步：下载模板并编辑完成学生分数<el-link
        type="primary"
        icon="el-icon-download"
        @click="()=>downExcel()"
        >下载模版</el-link
      >
    </div>
    <div class="d1">
      第二步：上传完成编辑的模板文件并导入
      <el-upload
        class="upload-demo"
        ref="upload"
        action="/api/web/report/importSubjectiveScore"
        :multiple="false"
        :data="{ id: id }"
        :with-credentials="true"
        :limit="1"
        :on-change="change"
        :on-success="upSuccess"
        :on-error="upError"
      >
        <!-- accept="application/vnd.ms-excel" -->
        <el-button class="btn" size="mini" type="primary"
          >选择文件并上传</el-button
        >
      </el-upload>
    </div>
  </div>
</template>

<script>
import { downloadTemplate } from "@/api/report";
import { downloadFile } from "@/utils";
export default {
  name: "downUpData",
  props: {
    id: {
      type: String,
      default: "",
    },
    classObject: {
      type: Object,
      default: ()=> {
        return {}
      },
    }
  },
  data() {
    return {
      file: null,
    };
  },
  methods: {
    async submitUpload() {
      this.$refs.upload.submit();

      // const formData = new FormData()
      // formData.append('id',this.componentId)
      // formData.append('file',new File(this.file.raw))
      // let {status,info} = await uploadExcel(formData);
      // if(status===0){
      //   this.$message.success(info);
      //   this.$emit("upSuccess")
      // } else {
      //   this.$message.error(info);
      // }
    },
    upSuccess(res) {
      if (res && res.code == 0 && res.success) {
        this.$message.success("上传成功");
        this.$emit("upSuccess");
      } else {
        this.$message.error(res.message);
      }
    },
    upError(res) {
      if (res && res.status == 0) {
        this.$message.success("上传成功");
        this.$emit("upSuccess");
      } else {
        this.$message.error(res.message);
      }
    },
    change(file) {
      this.file = file;
    },
    async downExcel() {
      let data = await downloadTemplate({
        id: this.id,
      });
      if (data && !data.code) {
        let filename = "模板.xlsx";
        if (this.classObject) {
          let className = this.classObject.classObject?.[0].label;
          filename = className + "_" + this.classObject.subjectName + "_" + this.classObject.examinationName + "_主观题分数导入.xlsx";
        }
        let blob = new Blob([data], {
          type: "application/vnd.ms-excel;charset=utf-8",
        });
        downloadFile(filename, blob);
      } else {
        this.$message.error(data.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.d1 {
  padding: 10px;
}
.btn {
  margin-top: 20px;
  border-radius: 8px;
  font-weight: normal;
}
</style>
