<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"
          :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"
          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"
              type="text"
              @click="toSimpleReset([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>
  </div>
</template>

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

export default {
  name: "mark-detail-issue",
  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: {},
    };
  },
  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.taskId));
    },
    async toSimpleReset(row) {
      const confirm = await this.$confirm("确定要重置当前问题卷吗？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

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

      await markIssueReset({
        taskIds,
      });
      this.$message.success("操作成功！");
      this.getList();
    },
  },
};
</script>
