<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"></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
            ></exam-select>
          </el-form-item>
          <el-form-item label="班级：">
            <class-select
              v-model="filter.clazzId"
              type="ALL_CLAZZ"
              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.examId || !filter.courseCode
            "
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'sync')"
          type="success"
          icon="el-icon-refresh"
          :loading="syncLoading"
          :disabled="!filter.semesterId || !filter.examId"
          @click="toSync"
          >同步</el-button
        >
        <el-button
          v-if="checkPrivilege('button', 'BatchDownload')"
          type="primary"
          icon="el-icon-download"
          :loading="loading"
          :disabled="!filter.semesterId || !filter.courseCode"
          @click="toDownloadAll"
          >一键下载</el-button
        >
        <el-button
          v-if="checkPrivilege('button', 'export')"
          type="primary"
          icon="el-icon-download"
          :loading="exportLoading"
          :disabled="!filter.semesterId || !filter.courseCode"
          @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="考试时间"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="studentCode"
          label="学号"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="examNumber"
          label="准考证号"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="orgName"
          label="院系"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="majorName"
          label="专业"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="clazzName"
          label="班级"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="courseName"
          label="课程名"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="totalScore"
          label="成绩"
          width="80"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="160px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'Preview')"
              class="btn-primary"
              type="text"
              @click="toViewSheetPaper(scope.row)"
              >原图</el-button
            >
            <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"
              :disabled="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
      v-if="checkPrivilege('link', 'Preview')"
      :cur-image="curImage"
      @on-prev="toPrevImage"
      @on-next="toNextImage"
      ref="SimpleImagePreview"
    ></simple-image-preview>
  </div>
</template>

<script>
import {
  scoreListPage,
  scoreExport,
  scoreBatchDownload,
  scoreDownload,
  scoreSync,
  scorePreview
} 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: "",
        examId: "",
        clazzId: "",
        courseCode: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curImage: {},
      curRow: {},
      curPapers: [],
      downloading: false,
      syncLoading: false,
      exportLoading: false,
      loading: false,
      // img view
      curImageIndex: 0,
      imageList: []
    };
  },
  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.syncLoading) return;
      this.syncLoading = true;
      const res = await scoreSync({
        semesterId: this.filter.semesterId,
        courseCode: this.filter.courseCode,
        examId: this.filter.examId
      }).catch(() => {});
      this.syncLoading = 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.exportLoading) return;
      this.exportLoading = true;
      const res = await scoreExport(this.filter).catch(() => {});
      this.exportLoading = false;
      if (!res) return;
      this.$message.success("导出任务已经提交");
    },
    async toDownload(row) {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return scoreDownload(row.id);
      }).catch(e => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

      if (!res) return;
      this.$message.success("下载成功！");
    },
    // img view
    toViewSheetPaper(row) {
      const dataList = row.sheetUrls ? JSON.parse(row.sheetUrls) : [];
      this.curImageIndex = 0;
      this.imageList = dataList.map((item, index) => {
        return { url: item, name: index + 1 };
      });
      this.selectImage(this.curImageIndex);
      this.$refs.SimpleImagePreview.open();
    },
    async toViewPaper(row) {
      const res = await scorePreview(row.id).catch(() => {});
      if (!res) return;

      const dataList = res || [];

      if (!dataList || !dataList.length) {
        this.$message.error("数据缺失！");
        return;
      }

      this.curImageIndex = 0;
      this.imageList = dataList.map((item, index) => {
        return { url: item, name: index + 1 };
      });
      this.selectImage(this.curImageIndex);
      this.$refs.SimpleImagePreview.open();
    },
    selectImage(index) {
      this.curImage = this.imageList[index];
    },
    toPrevImage() {
      if (this.curImageIndex === 0) {
        this.curImageIndex = this.imageList.length - 1;
      } else {
        this.curImageIndex--;
      }

      this.selectImage(this.curImageIndex);
    },
    toNextImage() {
      if (this.curImageIndex === this.imageList.length - 1) {
        this.curImageIndex = 0;
      } else {
        this.curImageIndex++;
      }

      this.selectImage(this.curImageIndex);
    }
  }
};
</script>
