<template>
  <div class="mark-detail-marker">
    <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.className" placeholder="班级" clearable>
            <el-option :value="1">班级1</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评阅题目">
          <el-select
            v-model="filter.groupQuestion"
            placeholder="评阅题目"
            clearable
          >
            <el-option :value="1">班级1</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评卷员">
          <el-input
            v-model.trim="filter.loginName"
            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" @click="toBind"> 绑定评卷员 </el-button>
        <el-button
          type="primary"
          :disabled="!multipleSelection.length"
          @click="toBatchRecycle"
        >
          回收
        </el-button>
        <el-button
          type="primary"
          :disabled="!multipleSelection.length"
          @click="toSetTaskCount(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"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="courseName" label="评卷员" min-width="100">
          <template slot-scope="scope">
            <el-tag size="medium" type="info">
              {{ scope.row.name }}（{{ scope.row.orgName }}）
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="groupQuestions"
          label="评阅题目"
          min-width="200"
        ></el-table-column>
        <el-table-column
          prop="markedCount"
          label="已评数量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="currentCount"
          label="正在评卷"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="taskCount"
          label="任务数"
          width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              @click="toReset(scope.row)"
              >重置</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toUnbind(scope.row)"
              >解绑</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toSimpleRecycle(scope.row)"
              >回收</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              @click="toSetTaskCount([scope.row.markUserGroupId])"
              >设置评卷数</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>
      <p class="tips-info">
        说明：评卷过程发现某些试题评卷工作任务重需要增加评卷员，可以选择对应评阅题目后绑定评卷员
      </p>
    </div>

    <!-- ModifyMarkerTaskCount -->
    <modify-marker-task-count
      ref="ModifyMarkerTaskCount"
      :ids="curIds"
      @modified="getList"
    ></modify-marker-task-count>
    <!-- ModifyMarkerBind -->
    <modify-marker-bind
      ref="ModifyMarkerBind"
      :data="curData"
      @modified="getList"
    ></modify-marker-bind>
  </div>
</template>

<script>
import {
  markMarkerListPage,
  markMarkerReset,
  markMarkerUnbind,
  markMarkerRecycle,
} from "../api";
import ModifyMarkerTaskCount from "./ModifyMarkerTaskCount.vue";
import ModifyMarkerBind from "./ModifyMarkerBind.vue";

export default {
  name: "mark-detail-marker",
  props: {
    baseInfo: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  components: { ModifyMarkerTaskCount, ModifyMarkerBind },
  data() {
    return {
      filter: {
        groupQuestion: "",
        className: "",
        loginName: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      curIds: [],
      curData: {},
      multipleSelection: [],
      downloading: false,
    };
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await markMarkerListPage(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.map((item) => item.markUserGroupId);
    },
    toBind() {
      this.curData = {
        examId: this.baseInfo.examId,
        paperNumber: this.baseInfo.paperNumber,
        // TODO: groupNumber
      };
      this.$refs.ModifyMarkerBind.open();
    },
    // reset marker
    async toReset(row) {
      const confirm = await this.$confirm("确定要重置当前评卷员？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await markMarkerReset({
        markUserGroupId: row.markUserGroupId,
      });
      this.$message.success("操作成功！");
      this.getList();
    },
    // unbind marker
    async toUnbind(row) {
      const confirm = await this.$confirm("确定要解绑当前评卷员？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      await markMarkerUnbind({
        markUserGroupId: row.markUserGroupId,
      });
      this.$message.success("操作成功！");
      this.getList();
    },
    // recycle marker
    async toBatchRecycle() {
      if (!this.multipleSelection.length) return;

      const confirm = await this.$confirm("确定要回收选中的评卷员？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.toRecycle(this.multipleSelection);
    },
    async toSimpleRecycle(row) {
      const confirm = await this.$confirm("确定要回收当前评卷员？", "提示", {
        type: "warning",
      }).catch(() => {});
      if (confirm !== "confirm") return;

      this.toRecycle([row.markUserGroupId]);
    },
    async toRecycle(markUserGroupIds) {
      if (!markUserGroupIds.length) return;

      await markMarkerRecycle({
        markUserGroupIds,
      });
      this.$message.success("操作成功！");
      this.getList();
    },
    // set task count
    toSetTaskCount(ids) {
      if (!ids.length) return;

      this.curIds = ids;
      this.$refs.ModifyMarkerTaskCount.open();
    },
  },
};
</script>
