<template>
  <div class="mark-detail-arbitration">
    <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="评阅题目">
          <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="状态">
          <status-select
            v-model="filter.type"
            type="MARK_ARBITRATE_STATUS"
            placeholder="状态"
            :clearable="false"
          >
          </status-select>
        </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-document-checked"
          :disabled="batchDoneDisabled"
          @click="toBatchDone(multipleSelection)"
        >
          批量处理
        </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"
          @selectable="rowSelectable"
        ></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="groupQuestions"
          label="评阅题目"
          min-width="200"
        ></el-table-column>
        <el-table-column
          prop="statusName"
          label="状态"
          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="updateUserName"
          label="处理人"
          min-width="160"
        ></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
          class-name="action-column"
          label="操作"
          width="80"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              :disabled="scope.row.status === 'MARKED'"
              type="text"
              @click="toDone(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 { markArbitrationListPage, markGroupQuestions } from "../../api";
import markMinxin from "../../markMinxin";

export default {
  name: "mark-detail-arbitration",
  mixins: [markMinxin],
  props: {
    baseInfo: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      filter: {
        groupNumber: "",
        type: "WAITING",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      multipleSelection: [],
      questions: [],
    };
  },
  computed: {
    selectGroupNumber() {
      return this.multipleSelection[0]?.groupNumber;
    },
    batchDoneDisabled() {
      if (!this.multipleSelection.length) return true;
      return this.multipleSelection.some((item) => item.status === "MARKED");
    },
  },
  mounted() {
    this.filter.className = this.$ls.get("preset-className", undefined);
    this.filter.groupNumber = this.$ls.get("preset-groupNumber", "");
    this.$ls.remove("preset-className");
    this.$ls.remove("preset-groupNumber");
    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 markArbitrationListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    rowSelectable(row) {
      return (
        !this.selectGroupNumber || this.selectGroupNumber === row.groupNumber
      );
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    toDone(row) {
      this.toMarkArbitrate({
        arbitrateId: row.id,
        // examId: row.examId,
        // paperNumber: row.paperNumber,
        // groupNumber: row.groupNumber,
      });
    },
    toBatchDone() {
      if (!this.multipleSelection.length) return;

      if (
        this.multipleSelection.some(
          (item) => item.groupNumber !== this.selectGroupNumber
        )
      ) {
        this.$message.error("只能批量处理同一分组的数据");
        return;
      }

      this.toMarkArbitrate({
        examId: this.baseInfo.examId,
        paperNumber: this.baseInfo.paperNumber,
        groupNumber: this.selectGroupNumber,
      });
    },
  },
};
</script>
