<template>
  <div class="wait-task">
    <div class="part-box part-box-pad">
      <el-table ref="TableList" :data="dataList">
        <el-table-column
          type="index"
          label="序号"
          width="50"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号"></el-table-column>
        <el-table-column prop="type" label="课程（代码）">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column prop="cardRuleName" label="题卡规则"></el-table-column>
        <el-table-column prop="taskName" label="流程节点">
          <span slot-scope="scope">
            {{ scope.row.taskName | flowTaskNameFilter }}
          </span>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column class-name="action-column" label="操作" width="100px">
          <template slot-scope="scope">
            <el-button class="btn-primary" type="text" @click="toDo(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
      ref="ModifyTaskApply"
      :instance="curTask"
      @modified="taskModified"
    ></modify-task-apply>
  </div>
</template>

<script>
import { mapActions } from "vuex";
import { waitExamTaskListPage } from "../api";
import ModifyTaskApply from "../components/ModifyTaskApply";

export default {
  name: "wait-task",
  components: { ModifyTaskApply },
  data() {
    return {
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curTask: {}
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    ...mapActions("exam", ["updateWaitTaskCount"]),
    async initData() {
      const cachePageInfo = this.$ls.get("cachePageInfo");
      if (cachePageInfo) {
        this.current = cachePageInfo.page;
        await this.getList();

        this.$nextTick(() => {
          const curRow = this.dataList.find(
            item => item.id === cachePageInfo.curRowId
          );
          if (!curRow) return;
          this.toDo(curRow);
        });
      } else {
        this.toPage(1);
      }
      this.$ls.remove("cachePageInfo");
    },
    async getList() {
      const datas = {
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await waitExamTaskListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toDo(task) {
      this.curTask = { ...task, source: "REVIEW" };
      this.$refs.ModifyTaskApply.open();
    },
    taskModified() {
      this.getList();
      this.updateWaitTaskCount();
    }
  },
  beforeRouteLeave(to, from, next) {
    if (to.name === "CardDesign") {
      this.$ls.set("cachePageInfo", {
        page: this.current,
        curRowId: this.curTask.id
      });
    } else {
      this.$ls.remove("cachePageInfo");
    }
    next();
  }
};
</script>
