<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>
        </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="college"
          label="开课学院"
          min-width="160"
        ></el-table-column>
        <el-table-column
          prop="courseNameCode"
          label="课程（代码）"
          min-width="300"
        >
        </el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号" width="200">
        </el-table-column>
        <el-table-column prop="cardType" label="题卡类型" width="100">
          <span slot-scope="scope">{{
            scope.row.cardType | cardTypeFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="studentCount" label="考生数" width="100">
        </el-table-column>
        <el-table-column prop="sheetCount" label="已扫张数" width="100">
        </el-table-column>
        <el-table-column prop="absentCount" label="缺考数" width="100">
        </el-table-column>
        <el-table-column prop="scanUploadRateStr" 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 } from "../../../constants/enumerate";
import { scanTaskStatListPage, scanTaskStatExport } from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "scan-task-stat",
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        openCollegeId: "",
        courseId: "",
        paperNumber: "",
        cardType: "",
      },
      dataList: [],
      CARD_TYPE,
      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 scanTaskStatListPage(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 scanTaskStatExport(this.filter);
      }, "").catch((e) => {
        this.$message.error(e || "导出失败，请重新尝试！");
      });
      this.loading = false;

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