<template>
  <div class="business-data-export">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="印刷计划：">
          <print-plan-select
            v-model.trim="filter.printPlanId"
            placeholder="请选择"
            clearable
            @change="planChange"
          ></print-plan-select>
        </el-form-item>
        <el-form-item label="课程（代码）：" label-width="110px">
          <course-select
            v-model.trim="filter.courseCode"
            placeholder="请选择"
            clearable
          ></course-select>
        </el-form-item>
        <el-form-item label="试卷编号：">
          <paper-number-select
            ref="PaperNumberSelect"
            v-model="filter.paperNumber"
            placeholder="请选择"
            clearable
          ></paper-number-select>
        </el-form-item>
        <el-form-item label="考点：" label-width="55px">
          <place-select
            v-model.trim="filter.examPlace"
            placeholder="请选择"
            clearable
          ></place-select>
        </el-form-item>
        <el-form-item label="考场：" label-width="55px">
          <room-select
            v-model.trim="filter.examRoom"
            placeholder="请选择"
            clearable
          ></room-select>
        </el-form-item>
        <el-form-item label="考生：" label-width="55px">
          <el-input
            v-model="filter.studentParams"
            placeholder="考生/学号/姓名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button type="primary" icon="icon icon-search" @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button icon="icon icon-download-act">
          <a :href="downloadUrl" download="考务数据模板.xlsx"
            >考务数据模板下载</a
          >
        </el-button>
        <el-button icon="icon icon-download" type="primary" @click="toExport">
          导出查询结果
        </el-button>
        <upload-button
          btn-icon="icon icon-share"
          btn-type="warning"
          btn-content="导入"
          :upload-url="uploadUrl"
          :upload-data="uploadData"
          :format="['xls', 'xlsx']"
          @upload-error="uplaodError"
          @upload-success="uploadSuccess"
          :disabled="!canImport"
        >
        </upload-button>
      </div>
    </div>

    <div class="part-box">
      <el-table ref="TableList" :data="dataList" border stripe>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          align="center"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          prop="printPlanName"
          label="印刷计划"
        ></el-table-column>
        <el-table-column prop="examStartDate" label="考试日期">
          <span slot-scope="scope">{{
            scope.row.examStartDate | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="examEndTime" label="考试时间">
          <span slot-scope="scope">{{
            scope.row.examEndTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column prop="courseNameCode" label="课程（代码）">
        </el-table-column>
        <el-table-column prop="paperNumber" label="试卷编码"></el-table-column>
        <el-table-column prop="examPlace" label="考点"> </el-table-column>
        <el-table-column prop="packageCount" label="卷袋数" width="80">
        </el-table-column>
        <el-table-column prop="totalSubjects" label="科次" width="80">
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          align="center"
          width="70"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-circle-right"
              @click="toPreview(scope.row)"
              title="查看详情"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="part-box-flex">
      <div>
        <span class="mr-2"
          >科次总计：<i class="color-primary">{{ totalData.totalSubjects }}</i>
          科次</span
        >
        <span
          >卷袋数量总计：<i class="color-primary">{{
            totalData.packageCount
          }}</i>
          个</span
        >
      </div>
      <el-pagination
        background
        layout="total,prev, pager, next"
        :current-page="current"
        :total="total"
        :page-size="size"
        @current-change="toPage"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { businessDataListPage, businessTotalData } from "../api";
import UploadButton from "@/components/UploadButton";

export default {
  name: "business-data-export",
  components: { UploadButton },
  data() {
    return {
      filter: {
        printPlanId: "",
        courseCode: "",
        paperNumber: "",
        examPlace: "",
        examRoom: "",
        studentParams: ""
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      curRow: {},
      totalData: {},
      curPrintPlan: {},
      // import
      downloadUrl: "/temps/考务数据模板.xlsx",
      uploadUrl: "/api/admin/sys/user/import",
      uploadData: {
        type: "FILE"
      }
    };
  },
  computed: {
    canImport() {
      return (
        this.curPrintPlan && ["NEW", "READY"].includes(this.curPrintPlan.status)
      );
    }
  },
  created() {
    this.search();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      const data = await businessDataListPage(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    async getTotalData() {
      this.totalData = await businessTotalData(this.filter);
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
      this.getTotalData();
    },
    planChange(plan) {
      this.curPrintPlan = plan;
    },
    uplaodError(errorData) {
      this.$notify.error({ title: "错误提示", message: errorData.message });
    },
    uploadSuccess(res) {
      this.$message.success("上传成功！");
      this.getList();
    },
    toExport() {},
    toPreview(row) {
      this.$ls.set("curBusiness", this.$objAssign(this.filter, row));
      this.$router.push({ name: "BusinessDataDetail" });
    }
  }
};
</script>
