<template>
  <div class="scan-statistics">
    <div class="part-box part-box-filter">
      <el-form
        ref="FilterForm"
        class="filter-form"
        label-position="left"
        label-width="85px"
        inline
      >
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="日期：">
            <el-date-picker
              v-model="filter.scanDate"
              type="date"
              value-format="timestamp"
              placeholder="日期选择"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="扫描员：">
            <el-input
              style="width: 180px"
              v-model.trim="filter.scanner"
              placeholder="扫描员"
              clearable
            ></el-input>
          </el-form-item>
        </template>

        <el-form-item label-width="0px">
          <el-button
            v-if="checkPrivilege('button', 'select')"
            type="primary"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="checkPrivilege('button', 'export')"
            type="primary"
            :loading="downloading"
            @click="toExport"
            >导出</el-button
          >
        </el-form-item>
      </el-form>
    </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="scanner" label="扫描员"></el-table-column>
        <el-table-column
          prop="pictureCount"
          label="扫描图片（张）"
        ></el-table-column>
        <el-table-column
          prop="studentCount"
          label="扫描学生数"
        ></el-table-column>
        <el-table-column prop="scanDate" label="扫描日期" width="140">
          <span slot-scope="scope">{{ scope.row.scanDate | dateFilter }}</span>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { scanStatisticsListQuery, exportScanStatistics } from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "scan-statistics",
  data() {
    return {
      filter: {
        scanDate: "",
        scanner: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      downloading: false,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      if (!this.checkPrivilege("list", "list")) return;

      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await scanStatisticsListQuery(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    async toExport() {
      if (this.downloading) return;
      this.downloading = true;

      const res = await downloadByApi(() => {
        return exportScanStatistics(this.filter);
      }).catch((e) => {
        this.$message.error(e || "下载失败，请重新尝试！");
      });
      this.downloading = false;

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