<template>
  <div>
    <div class="part-box part-box-filter">
      <el-form ref="FilterForm" label-position="left" label-width="90px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="search"
          ></secp-select>
          <el-form-item label="题卡类型：" label-width="90px">
            <el-select
              v-model="filter.cardType"
              style="width: 120px"
              placeholder="题卡类型"
              clearable
            >
              <el-option
                v-for="(val, key) in CARD_TYPE"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select
              v-model="filter.finishStatus"
              placeholder="状态"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="(val, key) in TASK_STATUS"
                :key="key"
                :value="key"
                :label="val"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="box-justify">
        <div></div>
        <div>
          <el-button
            v-if="checkPrivilege('button', 'export')"
            type="primary"
            icon="el-icon-download"
            :loading="loading"
            @click="toExport"
            >导出印刷进度</el-button
          >
        </div>
      </div>
    </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="courseNameCode"
          label="课程（代码）"
          min-width="300"
        >
        </el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号" min-width="200">
        </el-table-column>
        <el-table-column prop="cardTypeStr" label="题卡类型" width="100">
        </el-table-column>
        <el-table-column prop="college" label="开课学院" min-width="160">
        </el-table-column>
        <el-table-column prop="totalExamination" label="总考场数" width="100">
        </el-table-column>
        <el-table-column prop="createMethod" label="已生成/未生成" width="120">
          <template slot-scope="scope">
            <span class="color-primary">{{ scope.row.generate }}</span>
            <span class="mlr-1">/</span>
            <span class="color-danger">{{ scope.row.unGenerated }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createMethod" label="已印刷/未印刷" width="120">
          <template slot-scope="scope">
            <span class="color-primary">{{ scope.row.printed }}</span>
            <span class="mlr-1">/</span>
            <span class="color-danger">{{ scope.row.unPrinted }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createMethod" label="已打回/已作废" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.invalid }}</span>
            <span class="mlr-1">/</span>
            <span class="color-danger">{{ scope.row.reject }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="finishStatusStr" label="状态" width="100">
        </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>
  </div>
</template>

<script>
import { CARD_TYPE, TASK_STATUS } from "../../../constants/enumerate";
import { printTaskStatListPage, printTaskStatExport } from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "print-task-stat",
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        openCollegeId: "",
        courseId: "",
        paperNumber: "",
        cardType: "",
        finishStatus: "",
      },
      dataList: [],
      CARD_TYPE,
      TASK_STATUS,
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      loading: false,
    };
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await printTaskStatListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    async toExport() {
      if (this.loading) return;
      this.loading = true;
      const res = await downloadByApi(() => {
        return printTaskStatExport(this.filter);
      }, "").catch((e) => {
        this.$message.error(e || "导出失败，请重新尝试！");
      });
      this.loading = false;

      if (!res) return;
      this.$message.success("导出成功！");
    },
  },
};
</script>
