<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.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-input
            v-model.trim="filter.loginName"
            placeholder="评卷员姓名"
            clearable
          >
          </el-input>
        </el-form-item>
        <el-form-item label="班级">
          <el-input
            v-model.trim="filter.className"
            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">
          <span slot-scope="scope">
            {{ scope.row.resetting ? "--" : scope.row.markedCount }}
          </span>
        </el-table-column>
        <el-table-column prop="currentCount" label="正在评卷" width="100">
          <span slot-scope="scope">
            {{ scope.row.resetting ? "--" : scope.row.currentCount }}
          </span>
        </el-table-column>
        <el-table-column
          prop="topCount"
          label="设置评卷数"
          width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="140"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              :disabled="scope.row.resetting"
              @click="toReset(scope.row)"
              >{{ scope.row.resetting ? "重置中" : "重置" }}</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], 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>
      <p class="tips-info">
        说明：<br />
        1.评卷过程发现某些试题评卷工作任务重需要增加评卷员，可以选择对应评阅题目后绑定评卷员；<br />
        2.重置 - 将评卷员已评任务删除； <br />
        3.解绑 - 将该评卷员与这个分组关系解除绑定，他不能评这个分组； <br />
        4.回收 - 将评卷员已领未评的任务，放到未评池中。
      </p>
    </div>

    <!-- ModifyMarkerTaskCount -->
    <modify-marker-task-count
      ref="ModifyMarkerTaskCount"
      :ids="curIds"
      :top-count="curRow.topCount"
      @modified="getList"
    ></modify-marker-task-count>
    <!-- ModifyMarkerBind -->
    <modify-marker-bind
      ref="ModifyMarkerBind"
      :courseCode="baseInfo.courseCode"
      :data="curData"
      @modified="markerSelected"
    ></modify-marker-bind>
  </div>
</template>

<script>
import {
  markMarkerListPage,
  markMarkerReset,
  markMarkerUnbind,
  markMarkerRecycle,
  markGroupQuestions,
  markMarkerBind,
} 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: {
        groupNumber: "",
        loginName: "",
        className: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      curIds: [],
      curData: {},
      questions: [],
      multipleSelection: [],
      downloading: false,
      userParams: {},
    };
  },
  mounted() {
    this.filter.className = this.$ls.get("preset-className", "");
    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 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() {
      if (!this.filter.groupNumber) {
        this.$message.error("请选择评阅题目");
        return;
      }

      this.curData = {
        examId: this.baseInfo.examId,
        paperNumber: this.baseInfo.paperNumber,
        groupNumber: this.filter.groupNumber,
      };
      this.$refs.ModifyMarkerBind.open();
    },
    async markerSelected(users) {
      if (!users.length) return;
      const datas = { ...this.curData, userIds: users.map((item) => item.id) };
      const res = await markMarkerBind(datas).catch(() => {});
      if (!res) return;
      this.$message.success("绑定成功！");
      this.getList();
    },
    // 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, row) {
      if (!ids.length) return;

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