<template>
  <div class="mark-detail-issue">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="类型">
          <status-select
            v-model="filter.problemType"
            type="MARK_PROBLEM_TYPE"
            placeholder="类型"
            clearable
          >
          </status-select>
        </el-form-item>
        <el-form-item label="评阅题目">
          <el-select
            v-model="filter.groupNumber"
            placeholder="评阅题目"
            clearable
          >
            <el-option
              v-for="group in questions"
              :key="group.groupNumber"
              :value="group.groupNumber"
              :label="group.groupQuestions"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="filter.status" placeholder="状态" clearable>
            <el-option value="BACK" label="已打回"></el-option>
            <el-option value="WAITING" label="未打回"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密号">
          <el-input
            v-model.trim="filter.secretNumber"
            placeholder="密号"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          type="primary"
          icon="el-icon-top-right"
          :disabled="!multipleSelection.length"
          @click="toBatchReset"
        >
          批量打回
        </el-button>
      </div>
    </div>

    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          fixed="left"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="studentName" label="姓名" min-width="100">
        </el-table-column>
        <el-table-column
          prop="studentCode"
          label="学号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="secretNumber"
          label="密号"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="groupQuestions"
          label="评阅题目"
          min-width="200"
        ></el-table-column>
        <el-table-column
          prop="userName"
          label="评卷员"
          min-width="100"
        ></el-table-column>
        <el-table-column prop="createTime" label="提交时间" width="170">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          prop="problemTypeName"
          label="问题类型"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="updateUserName"
          label="处理人"
          min-width="100"
        ></el-table-column>
        <el-table-column prop="updateTime" label="处理时间" width="170">
          <span slot-scope="scope">{{
            scope.row.updateTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          prop="statusName"
          label="状态"
          width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="120"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              :disabled="scope.row.status === 'BACK'"
              type="text"
              @click="toSimpleReset(scope.row)"
              >打回</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toViewSheetPaper(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
      :cur-image="curImage"
      @on-prev="toPrevImage"
      @on-next="toNextImage"
      ref="SimpleImagePreview"
    ></simple-image-preview>
  </div>
</template>

<script>
import {
  markIssueListPage,
  markIssueReset,
  markGroupQuestions,
} from "../../api";
import SimpleImagePreview from "@/components/SimpleImagePreview";

export default {
  name: "mark-detail-issue",
  components: { SimpleImagePreview },
  props: {
    baseInfo: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      filter: {
        problemType: "",
        groupNumber: "",
        status: "",
        secretNumber: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      questions: [],
      multipleSelection: [],
      chartData: {},
      // img view
      curImage: {},
      curImageIndex: 0,
      imageList: [],
    };
  },
  mounted() {
    this.getQuestions();
    this.toPage(1);
  },
  methods: {
    async getQuestions() {
      const res = await markGroupQuestions({
        examId: this.baseInfo.examId,
        paperNumber: this.baseInfo.paperNumber,
      });
      this.questions = res || [];
    },
    async getList() {
      const datas = {
        ...this.filter,
        examId: this.baseInfo.examId,
        paperNumber: this.baseInfo.paperNumber,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await markIssueListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async toBatchReset() {
      if (!this.multipleSelection.length) return;

      const confirm = await this.$confirm(
        "确定要重置选中的问题卷吗？",
        "提示",
        {
          type: "warning",
        }
      ).catch(() => {});
      if (confirm !== "confirm") return;

      this.toReset(this.multipleSelection.map((item) => item.id));
    },
    async toSimpleReset(row) {
      const confirm = await this.$confirm("确定要重置当前问题卷吗？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.toReset([row.id]);
    },
    async toReset(ids) {
      if (!ids.length) return;

      await markIssueReset({
        ids: ids.join(),
      });
      this.$message.success("操作成功！");
      this.getList();
    },
    // img view
    toViewSheetPaper(row) {
      this.curImageIndex = 0;
      this.imageList = (row.sheetUrls || []).map((url, index) => {
        return { url, index: 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>
