<template>
  <div class="end-score-manage">
    <div class="part-box part-box-pad box-justify">
      <p></p>
      <div>
        <el-button type="success" @click="toImport">导入期末成绩</el-button>
        <el-button type="success" @click="toSetBlue">设置试卷蓝图</el-button>
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table :data="dataList">
        <el-table-column type="index" label="序号" width="70"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="studentCode" label="学号"></el-table-column>
        <el-table-column prop="score" label="成绩" width="80">
        </el-table-column>
        <el-table-column prop="scoreDetailContent" label="成绩明细">
        </el-table-column>
        <el-table-column class-name="action-column" label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              :class="scope.row.enable ? 'btn-danger' : 'btn-primary'"
              type="text"
              @click="toEnable(scope.row)"
              >{{ scope.row.enable ? "禁用" : "启用" }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- ModifyEndScore -->
    <modify-end-score
      ref="ModifyEndScore"
      :instance="curRow"
      @modified="getList"
    ></modify-end-score>
    <!-- ImportFile -->
    <import-file
      ref="ImportFile"
      title="导入期末成绩"
      :upload-url="uploadUrl"
      :upload-data="filter"
      :format="['xls', 'xlsx']"
      :download-handle="downloadHandle"
      :download-filename="dfilename"
      :auto-upload="false"
      @upload-success="uploadSuccess"
    ></import-file>
    <!-- SetBlueDialog -->
    <set-blue-dialog ref="SetBlueDialog" :course="course"> </set-blue-dialog>
  </div>
</template>

<script>
import {
  endScoreListPage,
  endScoreEnable,
  endScoreTemplateDownload,
} from "../api";
import ModifyEndScore from "./ModifyEndScore.vue";
import SetBlueDialog from "./SetBlueDialog.vue";
import ImportFile from "@/components/ImportFile.vue";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "end-score-manage",
  components: { ModifyEndScore, SetBlueDialog, ImportFile },
  props: {
    course: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      filter: {
        examId: "",
        courseCode: "",
        paperNumber: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      // import
      uploadUrl: "/api/admin/course/degree/final_score/import",
      dfilename: "期末成绩导入模板.xlsx",
      downloading: false,
    };
  },
  mounted() {
    this.filter = this.$objAssign(this.filter, this.course);
    this.toPage(1);
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await endScoreListPage(datas);
      this.dataList = data.records.map((item) => {
        const nitem = { ...item };
        if (item.scoreDetail) {
          nitem.scoreDetail = JSON.parse(item.scoreDetail);
          nitem.scoreDetailContent = nitem.scoreDetail
            .map((item) => item.score)
            .join();
        }

        return nitem;
      });
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toImport() {
      this.$refs.ImportFile.open();
    },
    toSetBlue() {
      this.$refs.SetBlueDialog.open();
    },
    toEdit(row) {
      this.curRow = { ...row };
      this.$refs.ModifyEndScore.open();
    },
    uploadSuccess({ data }) {
      let msg = `${data.success}`;
      if (data.error) {
        msg = +`，${data.error}`;
      }
      this.$message.success(msg);
      this.getList();
    },
    async downloadHandle() {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        const datas = {
          examId: this.course.examId,
          courseCode: this.course.courseCode,
          paperNumber: this.course.paperNumber,
        };
        return endScoreTemplateDownload(datas);
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
    async toEnable(row) {
      const action = row.enable ? "禁用" : "启用";
      const confirm = await this.$confirm(
        `确定要${action}考生【${row.name}】的成绩吗?`,
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      const enable = !row.enable;
      await endScoreEnable({
        id: row.id,
        enable,
      });
      row.enable = enable;
      this.$message.success("操作成功！");
    },
  },
};
</script>
