<template>
  <div class="card-check">
    <div style="margin-bottom: 20px;">
      <el-button
        v-for="(val, key) in auditingStatus"
        :key="key"
        :type="key == filter.auditingStatus ? 'primary' : 'default'"
        @click="selectAuditStatus(key)"
        >{{ val }}</el-button
      >
    </div>
    <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.schId"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in schools"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="学院：" v-if="filter.auditingStatus == 0">
          <el-select
            v-model="filter.collegeId"
            style="width: 142px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in colleges"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="题卡ID：" label-width="70px">
          <el-input
            style="width: 210px;"
            v-model.trim="filter.cardCode"
            placeholder="请输入内容"
            clearable
          ></el-input>
        </el-form-item>

        <el-form-item label-width="0px">
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
          <el-button
            type="warning"
            icon="icon icon-download"
            @click="toDownloadAll"
            v-if="filter.auditingStatus == 0"
            >批量下载试卷文件</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="cards" border stripe>
        <el-table-column prop="schoolName" label="学校名称"></el-table-column>
        <!-- <el-table-column prop="title" label="学院"></el-table-column> -->
        <el-table-column prop="cardCode" label="题卡ID"></el-table-column>
        <el-table-column prop="createTime" label="申请时间"></el-table-column>
        <el-table-column prop="userName" label="申请人"></el-table-column>
        <el-table-column prop="title" label="标题">
          <template slot-scope="scope">
            <p
              v-if="filter.auditingStatus == 0"
              class="cont-link"
              @click="downloadPaper(scope.row)"
              title="点击下载试卷文件"
            >
              {{ scope.row.title }}
            </p>
            <p v-else>{{ scope.row.title }}</p>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-modify"
              @click="toEdit(scope.row)"
              title="设计题卡"
              v-if="filter.auditingStatus == 0"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-circle-right"
              @click="toPreview(scope.row)"
              title="查看"
              v-else
            ></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 { AUDITING_STATUS } from "@/constants/enumerate";
import { download } from "@/plugins/utils";
import { auditListPage, schoolList } from "../api";

export default {
  name: "card-audit",
  data() {
    return {
      filter: {
        auditingStatus: "0",
        cardCode: "",
        schId: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      visible: false,
      auditingStatus: {},
      AUDITING_STATUS,
      schools: [],
      colleges: [],
      cards: [],
      curExam: {}
    };
  },
  created() {
    let auditingStatus = {};
    Object.keys(AUDITING_STATUS)
      .filter(key => key !== "9")
      .map(key => {
        auditingStatus[key] = AUDITING_STATUS[key];
      });
    this.auditingStatus = auditingStatus;
    this.getList();
    this.getSchoolList();
  },
  methods: {
    async getSchoolList() {
      this.schools = await schoolList();
      this.filter.schId = this.$ls.get("schoolId");
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await auditListPage(datas);
      this.cards = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async downloadPaper(row) {
      let load = this.$message({
        iconClass: "el-message__icon el-icon-loading",
        message: "Loading...",
        duration: 0
      });
      const data = await download({
        type: "get",
        url: `/api/print/card/card/downloadSimplePaper?cardId=${row.cardId}`,
        fileName: `${row.schoolName}_${row.cardId}${row.attachentType}`,
        header: {
          token: this.$ls.get("token")
        }
      }).catch(error => {
        this.$message.error(error);
      });

      load.close();
      if (!data) return;
      this.$message.success("文件已开始下载！");
    },
    async toDownloadAll() {
      let load = this.$message({
        iconClass: "el-message__icon el-icon-loading",
        message: "Loading...",
        duration: 0
      });

      let filterNams = [AUDITING_STATUS[this.filter.auditingStatus]];
      if (this.filter.cardCode) filterNams.push(this.filter.cardCode);
      filterNams.push(`page-${this.current}.zip`);

      const cardIds = this.cards.map(item => item.cardId).join(",");

      const data = await download({
        type: "get",
        url: `/api/print/card/card/downloadPapers?cardIds=${cardIds}`,
        fileName: filterNams.join("-"),
        header: {
          token: this.$ls.get("token")
        }
      }).catch(error => {
        this.$message.error(error);
      });

      load.close();
      if (!data) return;
      this.$message.success("文件已开始下载！");
    },
    selectAuditStatus(val) {
      this.filter.auditingStatus = val * 1;
      this.toPage(1);
    },
    toEdit(row) {
      this.$router.push({
        name: "CardDesign",
        params: {
          cardId: row.cardId
        }
      });
    },
    toPreview(row) {
      window.open(
        this.getRouterPath({
          name: "CardPreview",
          params: {
            cardId: row.cardId,
            viewType: "view"
          }
        })
      );
    }
  }
};
</script>
