<template>
  <div class="upload-structure">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select v-model="filter.semesterId"></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
            ></exam-select>
          </el-form-item>
        </template>
        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          prop="thirdRelateId"
          label="云阅卷考试ID"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="thirdRelateName"
          label="云阅卷考试名称"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）" min-width="200">
          <span slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </span>
        </el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="paperType"
          label="试卷类型"
          width="100"
        ></el-table-column>
        <el-table-column prop="statusStr" label="状态" width="100">
          <span
            slot-scope="scope"
            :class="{ 'color-danger': scope.row.status === 'INIT' }"
            >{{ scope.row.statusStr }}</span
          >
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="220px"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-primary"
              type="text"
              :disabled="scope.row.taskStatus === 'RUNNING'"
              @click="toSetParams(scope.row)"
              >评卷参数设置</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              :disabled="
                !scope.row.paperInfoJson || scope.row.taskStatus === 'RUNNING'
              "
              @click="toSetAnswer(scope.row)"
              >设置客观题标答</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              :disabled="
                !scope.row.paperInfoJson || scope.row.taskStatus === 'RUNNING'
              "
              @click="toBindMarkLeader(scope.row)"
              >绑定科组长</el-button
            >
            <el-button
              class="btn-primary"
              type="text"
              :disabled="
                !scope.row.paperInfoJson || scope.row.taskStatus === 'RUNNING'
              "
              @click="toBindMarker(scope.row)"
              >绑定评卷员</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'transfer')"
              class="btn-primary"
              type="text"
              @click="toTransfer(scope.row)"
              >{{ scope.row.transferId ? "打回" : "转办" }}</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'Upload')"
              class="btn-primary"
              type="text"
              :disabled="scope.row.taskStatus === 'RUNNING'"
              @click="toUpload(scope.row)"
              >上传标答文件</el-button
            >
            <el-button
              v-if="checkPrivilege('link', 'Preview')"
              class="btn-primary"
              type="text"
              :disabled="!scope.row.paperAnswer"
              @click="toViewAnswer(scope.row, scope.row.paperType)"
              >查看标答文件</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>

    <UploadPaperAnswerDialog
      v-if="checkPrivilege('link', 'Upload')"
      ref="UploadPaperAnswerDialog"
      :instance="curTask"
      @modified="delayUpdateList"
    />
    <ModifyMarkParams
      ref="ModifyMarkParams"
      :instance="curTask"
      @modified="delayUpdateList"
    />
    <ModifyPaperMarker
      ref="ModifyPaperMarker"
      :instance="curTask"
      @close="delayUpdateList"
    />
    <ModifyObjectiveAnswer
      ref="ModifyObjectiveAnswer"
      :instance="curTask"
      @modified="delayUpdateList"
    />
    <SelectTypeUser
      ref="SelectTypeUser"
      special-privilege="SUBJECT_HEADER"
      :user-ids="curMarkLeaders"
      :user-limit-count="0"
      @confirm="headerBind"
    />
    <SelectTransferUser
      v-if="checkPrivilege('link', 'transfer')"
      ref="SelectTransferUser"
      :task="curTask"
      @modified="getList"
    />
  </div>
</template>

<script>
import { examStructureListPage, examBindMarkLeader } from "../api";
import UploadPaperAnswerDialog from "../components/UploadPaperAnswerDialog";
import ModifyMarkParams from "../components/markParam/ModifyMarkParams";
import ModifyObjectiveAnswer from "../components/markParam/ModifyObjectiveAnswer.vue";
import ModifyPaperMarker from "../components/markParam/ModifyPaperMarker.vue";
import SelectTypeUser from "../components/SelectTypeUser.vue";
import SelectTransferUser from "../components/SelectTransferUser.vue";

export default {
  name: "upload-structure",
  components: {
    UploadPaperAnswerDialog,
    ModifyMarkParams,
    ModifyObjectiveAnswer,
    SelectTypeUser,
    ModifyPaperMarker,
    SelectTransferUser
  },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curTask: {},
      curMarkLeaders: []
    };
  },
  mounted() {
    this.toPage(1);
  },
  methods: {
    delayUpdateList() {
      setTimeout(() => {
        this.getList();
      }, 1000);
    },
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await examStructureListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toSetParams(row) {
      this.curTask = row;
      this.$refs.ModifyMarkParams.open();
    },
    toSetAnswer(row) {
      this.curTask = row;
      this.$refs.ModifyObjectiveAnswer.open();
    },
    toUpload(row) {
      this.curTask = row;
      this.$refs.UploadPaperAnswerDialog.open();
    },
    toViewAnswer(row, paperType) {
      if (!row.paperAnswer) {
        this.$message.error("暂时还没有标答可预览！");
        return;
      }

      const paperAnswer = JSON.parse(row.paperAnswer);
      const paper = paperAnswer.find(item => item.paperType === paperType);
      if (!paper) {
        this.$message.error("标答文件不存在！");
        return;
      }

      window.open(paper.answerUrl);
    },
    toBindMarkLeader(row) {
      this.curTask = row;
      const markLeaders = row.markLeader ? JSON.parse(row.markLeader) : [];
      this.curMarkLeaders = markLeaders.map(item => item.id);
      this.$refs.SelectTypeUser.open();
    },
    toBindMarker(row) {
      this.curTask = row;
      this.$refs.ModifyPaperMarker.open();
    },
    async headerBind(users) {
      await examBindMarkLeader({
        id: this.curTask.id,
        markLeader: users.map(item => {
          return {
            id: item.id,
            loginName: item.loginName
          };
        })
      });
      this.$message.success("绑定成功！");
      this.getList();
    },
    toTransfer(row) {
      this.curTask = row;
      this.$refs.SelectTransferUser.open();
    }
  }
};
</script>
