<template>
  <div class="approve-record-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="toPage(1)"
          ></secp-select>
          <el-form-item label="状态：">
            <el-select
              v-model="filter.status"
              style="width: 120px"
              placeholder="请选择状态"
              clearable
            >
              <el-option
                v-for="(val, key) in AUDITING_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="机构：">
            <org-select
              v-model="filter.teachingRoomId"
              style="width: 100%"
              placeholder="所属机构"
            ></org-select>
          </el-form-item>
          <el-form-item label="提交人：">
            <el-input
              style="width: 142px"
              v-model.trim="filter.teacherUserName"
              placeholder="请输入提交人"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="提交时间：">
            <el-date-picker
              v-model="createTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="提交开始时间"
              end-placeholder="提交结束时间"
              value-format="timestamp"
              align="right"
              unlink-panels
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="当前处理人：">
            <el-input
              style="width: 142px"
              v-model.trim="filter.pendApproveUserName"
              placeholder="请输入当前处理人"
              clearable
            ></el-input>
          </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
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          prop="semesterName"
          label="学期"
          min-width="210"
        ></el-table-column>
        <el-table-column
          prop="examName"
          label="考试"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          min-width="160"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）" min-width="260">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column
          prop="teacherUserName"
          label="提交人"
          min-width="160"
        ></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="statusStr" label="流程状态"></el-table-column>
        <el-table-column prop="setupStr" label="当前节点"></el-table-column>
        <el-table-column
          prop="pendApproveUserName"
          label="当前处理人"
          min-width="200"
        >
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'preview')"
              class="btn-primary"
              type="text"
              @click="toPreview(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>

    <!-- ModifyTaskApply -->
    <modify-task-apply
      v-if="checkPrivilege('link', 'preview')"
      ref="ModifyTaskApply"
      type="PREVIEW"
      :row-data="curExamTask"
    ></modify-task-apply>
  </div>
</template>

<script>
import { approveRecordListPage } from "../api";
import { examTaskGetOne } from "../../exam/api";
import { AUDITING_STATUS } from "@/constants/enumerate";
import pickerOptions from "@/constants/datePickerOptions";
import ModifyTaskApply from "../../exam/components/taskApply/ModifyTaskApply.vue";

export default {
  name: "approve-record-manage",
  components: { ModifyTaskApply },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        courseId: "",
        paperNumber: "",
        status: "",
        teachingRoomId: "",
        teacherUserName: "",
        pendApproveUserName: "",
        startTime: null,
        endTime: null,
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      AUDITING_STATUS,
      curExamTask: {},
      // date-picker
      createTime: [],
      pickerOptions,
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      if (this.createTime) {
        datas.startTime = this.createTime[0];
        datas.endTime = this.createTime[1];
      }
      const data = await approveRecordListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toPreview(row) {
      const types = {
        exam_task: this.previewExmaTask,
      };

      const func = types[row.objectTable];
      if (!func) {
        this.$message.error("操作错误！");
        return;
      }
      func(row);
    },
    async previewExmaTask(data) {
      this.curExamTask = await examTaskGetOne(data.objectId);
      this.$refs.ModifyTaskApply.open();
    },
  },
};
</script>
