<template>
  <div class="wait-task-exam-task part-box part-box-pad part-box-border">
    <div class="task-head part-box part-box-flex">
      <h3>
        命题待办（<span class="color-danger">{{ total }}</span
        >）
      </h3>
      <div>
        <i class="el-icon-s-fold"></i>
      </div>
    </div>
    <div class="task-list">
      <div v-for="task in tasks" :key="task.id" class="task-item">
        <p>
          <span>试卷编号：</span>
          <span>{{ task.paperNumber }}</span>
        </p>
        <p>
          <span>课程（代码）：</span>
          <span>{{ task.courseName }}（{{ task.courseCode }}）</span>
        </p>
        <p>
          <span>命题结束时间：</span>
          <span>{{ task.endTime | timestampFilter }}</span>
        </p>
        <div class="part-box-flex">
          <el-tag :type="task.residueTips.status" effect="dark">
            {{ task.residueTips.title }}
          </el-tag>
          <span class="task-action" @click="toDo(task)"
            >立即处理 <i class="el-icon-arrow-right"></i
          ></span>
        </div>
      </div>
    </div>
    <div class="text-center">
      <el-pagination
        v-if="tasks.length"
        background
        layout="total,prev, pager, next"
        :current-page="current"
        :total="total"
        :page-size="size"
        @current-change="toPage"
      >
      </el-pagination>
    </div>

    <!-- ModifyTaskApply -->
    <modify-task-apply
      ref="ModifyTaskApply"
      :edit-type="editType"
      :instance="curTask"
      @modified="modified"
    ></modify-task-apply>
  </div>
</template>

<script>
import { waitExamTaskListPage } from "../api";
import ModifyTaskApply from "./ModifyTaskApply";
import { residueFloorTime } from "@/plugins/utils";
import { TASK_WARNING_TIME } from "@/constants/enumerate";

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

        this.$nextTick(() => {
          const curRow = this.tasks.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.tasks = data.records.map(item => {
        item.residueTips = residueFloorTime(
          item.endTime - item.systemCurrentTime,
          TASK_WARNING_TIME
        );
        return item;
      });
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toDo(task) {
      this.curTask = task;
      this.$refs.ModifyTaskApply.open();
    },
    modified() {
      this.$emit("update-list");
    }
  }
};
</script>
