<template>
  <div class="approve-record-manage">
    <div class="part-box part-box-filter part-box-border">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <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="teacherUserName"
          label="提交人"
          min-width="120"
        ></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="120"
        >
        </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
            >
            <el-button
              v-if="checkPrivilege('link', 'delete')"
              class="btn-danger"
              type="text"
              @click="toDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

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

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

export default {
  name: "approve-record-manage",
  components: { ModifyTaskApply },
  data() {
    return {
      filter: {
        status: "",
        teachingRoomId: "",
        teacherUserName: "",
        pendApproveUserName: "",
        startTime: null,
        endTime: null
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      AUDITING_STATUS,
      curExamTask: {},
      // date-picker
      createTime: [],
      pickerOptions
    };
  },
  mounted() {
    this.toPage(1);
  },
  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();
    },
    async 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();
    },
    toDelete(row) {
      this.$confirm(`确定要删除当前记录吗?`, "提示", {
        type: "warning"
      })
        .then(async () => {
          await deleteApproveRecord({
            id: row.id,
            enable: 0
          });
          this.$message.success("操作成功！");
          this.getList();
        })
        .catch(() => {});
    }
  }
};
</script>
