<template>
  <div class="student-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="90px" inline>
        <el-form-item prop="semesterId" label="学期：">
          <semester-select v-model="filter.semesterId"></semester-select>
        </el-form-item>
        <el-form-item prop="examTypeId" label="考试：">
          <exam-type-select
            v-model="filter.examTypeId"
            :semester-id="filter.semesterId"
          ></exam-type-select>
        </el-form-item>
        <el-form-item label="下载时间：">
          <el-date-picker
            v-model="filter.operateTime"
            type="date"
            placeholder="下载时间"
            value-format="timestamp"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否使用：">
          <el-select v-model="filter.used" placeholder="是否使用" clearable>
            <el-option
              v-for="(val, key) in BOOLEAN_TYPE"
              :key="key"
              :value="key * 1"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下载用途：">
          <el-select v-model="filter.useScene" placeholder="下载用途" clearable>
            <el-option
              v-for="item in useScenes"
              :key="item.name"
              :value="item.name"
              :label="item.desc"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="!filter.semesterId || !filter.examTypeId"
            @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button type="primary" icon="el-icon-delete" @click="toSign"
          >标记用途</el-button
        >
      </div>
    </div>
    <div class="part-box part-box-pad">
      <el-table
        ref="TableList"
        :data="dataList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="operateTime" label="下载时间" width="170">
          <span slot-scope="scope">{{
            scope.row.operateTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          prop="userName"
          label="下载人员"
          width="120"
        ></el-table-column>
        <el-table-column prop="fileName" label="文件" min-width="150">
          <template slot-scope="scope">
            <div
              class="cont-link"
              title="点击下载文件"
              @click="toDownload(scope.row)"
            >
              {{ scope.row.fileName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="schoolName" label="学校"></el-table-column>
        <el-table-column prop="semesterName" label="学期"></el-table-column>
        <el-table-column prop="examTypeName" label="考试类型"></el-table-column>
        <el-table-column
          prop="colleges"
          label="学院数量"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="subjects"
          label="科目门数"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="students"
          label="考试科次"
          width="100"
        ></el-table-column>
        <el-table-column prop="used" label="是否使用" width="80">
          <span slot-scope="scope">{{ scope.row.used | booleanFilter }}</span>
        </el-table-column>
        <el-table-column prop="useSceneStr" label="下载用途"></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"
          :page-sizes="[20, 50, 100]"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>

    <!-- SignDownloadData -->
    <sign-download-data
      ref="SignDownloadData"
      :ids="multipleSelection"
      :useScenes="useScenes"
      @modified="getList"
    ></sign-download-data>
  </div>
</template>

<script>
import { USE_TYPE, BOOLEAN_TYPE } from "../../../constants/enumerate";
import { downloadList, useSceneList, downloadBusinessData } from "../api";
import SignDownloadData from "../components/SignDownloadData.vue";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "student-manage",
  components: { SignDownloadData },
  data() {
    return {
      filter: {
        semesterId: "",
        examTypeId: "",
        operateTime: "",
        used: null,
        useScene: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      dataList: [],
      useScenes: [],
      curRow: {},
      BOOLEAN_TYPE,
      USE_TYPE,
      multipleSelection: [],
      downloading: false,
    };
  },
  mounted() {
    this.getList();
    this.getUseSences();
  },
  methods: {
    async getUseSences() {
      const res = await useSceneList();
      this.useScenes = res || [];
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await downloadList(datas);
      this.dataList = data.records;
      this.total = data.total;
      this.queriedFilter = { ...this.filter };
    },
    toPage(page) {
      this.multipleSelection = [];
      this.current = page;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => item.id);
    },
    toSign() {
      if (!this.multipleSelection.length) {
        this.$message.error("请先选择数据！");
        return;
      }
      this.$refs.SignDownloadData.open();
    },
    async toDownload(row) {
      if (this.downloading) return;
      this.downloading = true;

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

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