<template>
  <div class="exam-manage">
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="考试名称：">
          <el-select
            v-model="filter.examId"
            style="width: 193px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in exams"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打印时间：">
          <el-date-picker
            v-model="filter.printTime"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            type="date"
            placeholder="请选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="撤回申请：">
          <el-select
            v-model="filter.revokeStatus"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(val, key) in REVOKE_STATUS"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="dataList" border stripe>
        <el-table-column prop="examCode" label="考试ID"></el-table-column>
        <el-table-column prop="examName" label="考试名称"></el-table-column>
        <el-table-column prop="totalCourse" label="总科次"></el-table-column>
        <el-table-column prop="printTime" label="打印时间"></el-table-column>
        <el-table-column prop="printProgress" label="打印进度">
          <template slot-scope="scope">{{ scope.row.printProgress }}%</template>
        </el-table-column>
        <el-table-column
          prop="revokeStatusName"
          label="撤回申请"
        ></el-table-column>
        <el-table-column label="操作" align="center" v-if="!IS_PRINTER">
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-confirm"
              @click="toConfirm(scope.row)"
              title="确认"
              v-if="scope.row.revokeStatus === '0'"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { REVOKE_STATUS } from "@/constants/enumerate";
import { printTaskListPage, examList, printRevokeAudit } from "../api";

export default {
  name: "print-manage",
  data() {
    return {
      filter: {
        examId: "",
        revokeStatus: "",
        printTime: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      visible: false,
      REVOKE_STATUS,
      exams: [],
      dataList: [],
      IS_PRINTER: false,
      loopRunning: false,
      loopSetTs: []
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.IS_PRINTER = this.$ls
        .get("user", { roleCode: "" })
        .roleCode.includes("PRINT");
      this.getExamList();
      this.loopRunning = true;
      this.timerUpdatePage();
    },
    clearLoopSetTs() {
      if (!this.loopSetTs.length) return;
      this.loopSetTs.forEach(sett => {
        clearTimeout(sett);
      });
      this.loopSetTs = [];
    },
    async timerUpdatePage() {
      this.clearLoopSetTs();
      if (!this.loopRunning) return;

      await this.getList().catch(() => {});

      this.loopSetTs.push(
        setTimeout(() => {
          this.timerUpdatePage();
        }, 30 * 1000)
      );
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await printTaskListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async getExamList() {
      const data = await examList();
      this.exams = data.map(item => {
        return {
          id: item.id,
          name: item.examName
        };
      });
    },
    async toConfirm(row) {
      await printRevokeAudit(row.id);
      this.$message.success("确认成功！");
      this.getList();
    }
  },
  beforeDestroy() {
    this.loopRunning = false;
    this.clearLoopSetTs();
  }
};
</script>
