<template>
  <div class="download-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <template v-if="checkPrivilege('condition', 'condition')">
          <el-form-item label="学期：">
            <semester-select v-model="filter.semesterId"></semester-select>
          </el-form-item>
          <el-form-item label="考试：">
            <exam-select
              v-model="filter.examId"
              :semester-id="filter.semesterId"
            ></exam-select>
          </el-form-item>
          <el-form-item label="机构：">
            <org-select v-model="filter.orgId" placeholder="机构"></org-select>
          </el-form-item>
          <el-form-item label="课程名称：">
            <el-input
              v-model="filter.courseName"
              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>
      <div class="part-box-action">
        <el-button
          v-if="checkPrivilege('button', 'download')"
          icon="el-icon-circle-plus-outline"
          type="info"
          :disabled="!filterHasQuery"
          @click="toBatchDownload"
        >
          批量下载
        </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="courseName" label="课程（代码）">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column prop="paperNumber" label="试卷编号"></el-table-column>
        <el-table-column prop="paperType" label="全部卷型" width="100">
        </el-table-column>
        <el-table-column
          prop="unexposedPaperType"
          label="未曝光卷型"
          width="100"
        ></el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          width="120px"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              v-if="checkPrivilege('link', 'download')"
              class="btn-primary"
              type="text"
              @click="toDownload(scope.row)"
              >下载</el-button
            >
          </template>
        </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>

    <!-- DownloadSetDialog -->
    <download-set-dialog :download-set="downloadSet" ref="DownloadSetDialog">
    </download-set-dialog>
  </div>
</template>

<script>
import DownloadSetDialog from "../components/DownloadSetDialog";
import { downloadByApi } from "@/plugins/download";
import { dataDownloadList, dataDownloadDetail } from "../api";

export default {
  name: "download-manage",
  components: {
    DownloadSetDialog
  },
  data() {
    return {
      filter: {
        semesterId: "",
        examId: "",
        orgId: "",
        courseName: ""
      },
      queriedFilter: {},
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      downloading: false,
      multipleSelection: [],
      dataList: [],
      curRow: {},
      downloadSet: {}
    };
  },
  computed: {
    filterHasQuery() {
      return !Object.keys(this.filter).some(
        k => this.filter[k] !== this.queriedFilter[k]
      );
    }
  },
  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 dataDownloadList(datas);
      this.dataList = data.records;
      this.total = data.total;
      this.queriedFilter = { ...this.filter };
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.id);
    },
    toBatchDownload() {
      if (this.multipleSelection.length) {
        this.downloadSet = {
          idSet: this.multipleSelection
        };
      } else {
        this.downloadSet = {
          ...this.filter,
          idSet: this.multipleSelection
        };
      }
      this.$refs.DownloadSetDialog.open();
    },
    async toDownload(row) {
      if (this.downloading) return;
      this.downloading = true;

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

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