<template>
  <div class="score-archive">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select
              v-model="filter.semesterId"
              default-select
            ></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="课程：">
            <course-select
              v-model="filter.courseId"
              placeholder="课程"
              filterable
              clearable
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
            ></course-select>
          </el-form-item>
          <el-form-item label="班级：">
            <class-select
              v-model="filter.clazzId"
              placeholder="班级"
              :semester-id="filter.semesterId"
              :exam-id="filter.examId"
              :course-id="filter.courseId"
            ></class-select>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="box-justify">
        <div>
          <el-button
            v-if="checkPrivilege('button', 'BatchDownload')"
            type="primary"
            icon="el-icon-download"
            :loading="loading"
            :disabled="!filter.semesterId || !filter.examId"
            @click="toDownloadAll"
            >一键下载</el-button
          >
          <el-button
            v-if="checkPrivilege('button', 'BatchDownload')"
            type="primary"
            icon="el-icon-s-order"
            @click="toDataTask('SCORE_DOWNLOAD')"
            >下载结果查询</el-button
          >
        </div>
        <div>
          <el-button
            v-if="checkPrivilege('button', 'export')"
            type="primary"
            icon="el-icon-download"
            :loading="exportLoading"
            :disabled="!filter.semesterId || !filter.examId || !filter.courseId"
            @click="toExport"
            >成绩导出</el-button
          >
          <el-button
            v-if="checkPrivilege('button', 'export')"
            type="primary"
            icon="el-icon-s-order"
            @click="toDataTask('SCORE_EXPORT')"
            >导出结果查询</el-button
          >
        </div>
      </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="210"
        ></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, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </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>
    <!-- data-task-dialog -->
    <data-task-dialog
      v-if="
        checkPrivilege('button', 'export') ||
        checkPrivilege('button', 'BatchDownload')
      "
      ref="DataTaskDialog"
      :task-type="taskType"
    ></data-task-dialog>
  </div>
</template>

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

export default {
  name: "score-archive",
  components: { SimpleImagePreview },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        clazzId: "",
        courseId: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curImage: {},
      curRow: {},
      curPapers: [],
      downloading: false,
      syncLoading: false,
      exportLoading: false,
      loading: false,
      taskType: "",
      // 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 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("下载成功！");
    },
    toDataTask(taskType) {
      this.taskType = taskType;
      this.$refs.DataTaskDialog.open();
    },
    // 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>
