<template>
  <div class="score-archive">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="考试时间：">
            <semester-select
              v-model="filter.semesterId"
              placeholder="考试时间"
            ></semester-select>
          </el-form-item>
          <el-form-item label="学院：">
            <college-select
              v-model="filter.orgId"
              :semester-id="filter.semesterId"
              cascader
              placeholder="学院"
            ></college-select>
          </el-form-item>
          <el-form-item label="专业：">
            <major-select
              v-model="filter.majorId"
              :college-id="filter.orgId"
              cascader
              placeholder="专业"
            ></major-select>
          </el-form-item>
          <el-form-item label="班级：">
            <class-select
              v-model="filter.clazzId"
              :major-id="filter.majorId"
              cascader
              placeholder="班级"
            ></class-select>
          </el-form-item>
          <el-form-item label="课程：">
            <course-select
              v-model="filter.courseCode"
              placeholder="课程"
              filterable
              clearable
            ></course-select>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            :disabled="!filter.semesterId || !filter.orgId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          type="success"
          icon="el-icon-refresh"
          :loading="loading"
          @click="toSync"
          >同步</el-button
        >
        <el-button
          v-if="checkPrivilege('button', 'BatchDownload')"
          type="primary"
          icon="el-icon-download"
          :loading="loading"
          @click="toDownloadAll"
          >一键下载</el-button
        >
        <el-button
          v-if="checkPrivilege('button', 'export')"
          type="primary"
          icon="el-icon-download"
          :loading="loading"
          @click="toExport"
          >成绩导出</el-button
        >
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="semesterName" label="考试时间"></el-table-column>
        <el-table-column prop="name" label="姓名" width="120"></el-table-column>
        <el-table-column prop="studentCode" label="学号"></el-table-column>
        <el-table-column prop="orgName" label="院系"></el-table-column>
        <el-table-column prop="majorName" label="专业"></el-table-column>
        <el-table-column prop="clazzName" label="班级"></el-table-column>
        <el-table-column prop="courseName" label="课程名"></el-table-column>
        <el-table-column
          prop="totalScore"
          label="成绩"
          width="80"
        ></el-table-column>
        <el-table-column class-name="action-column" label="操作" width="160px">
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'Preview')"
              class="btn-primary"
              type="text"
              @click="toViewPaper(scope.row)"
              >查看原卷</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'download')"
              class="btn-primary"
              type="text"
              :loading="downloading"
              @click="toDownload(scope.row)"
              >下载</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- image-preview -->
    <simple-image-preview
      :cur-image="curImage"
      simple
      ref="SimpleImagePreview"
    ></simple-image-preview>
  </div>
</template>

<script>
import {
  scoreListPage,
  scoreExport,
  scoreBatchDownload,
  scoreDownload,
  scoreSync
} from "../api";
// import { downloadPaper } from "../components/downloadPaper";
import { downloadByApi } from "@/plugins/download";
import SimpleImagePreview from "@/components/SimpleImagePreview";

export default {
  name: "score-archive",
  components: { SimpleImagePreview },
  data() {
    return {
      filter: {
        semesterId: "",
        orgId: "",
        majorId: "",
        clazzId: "",
        courseCode: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curImage: {},
      curRow: {},
      curPapers: [],
      downloading: false,
      loading: false
    };
  },
  mounted() {
    // this.getList();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await scoreListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toSync() {
      if (this.loading) return;
      this.loading = true;
      const res = await scoreSync({ examId: "" }).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.$message.success("同步任务已经提交");
    },
    async toDownloadAll() {
      if (this.loading) return;
      this.loading = true;
      const res = await scoreBatchDownload(this.filter).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.$message.success("下载任务已经提交");
    },
    async toExport() {
      if (this.loading) return;
      this.loading = true;
      const res = await scoreExport(this.filter).catch(() => {});
      this.loading = false;
      if (!res) return;
      this.$message.success("导出任务已经提交");
    },
    toViewPaper(row) {
      if (!row.sheetUrls) {
        this.$message.error("原卷缺失！");
        return;
      }
      this.curImage = { url: row.sheetUrls };
      this.$refs.SimpleImagePreview.open();
    },
    async toDownload(row) {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return scoreDownload(row.studentCode);
      }, `${row.name}-原卷.jpg`).catch(e => {
        console.log(e);
      });
      this.downloading = false;

      if (!res) {
        this.$message.error("下载失败，请重新尝试！");
        return;
      }

      this.$message.success("下载成功！");
    }
  }
};
</script>
