<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')">
          <secp-select
            v-model="filter"
            defaultSelectExam
            @exam-default="toPage(1)"
          ></secp-select>
          <el-form-item label="开课机构：">
            <org-select
              v-model="filter.orgId"
              placeholder="开课机构"
            ></org-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="filter.userParam"
              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-download"
          type="primary"
          :disabled="!filterHasQuery"
          @click="toBatchDownload"
        >
          批量下载
        </el-button>
        <el-button
          v-if="checkPrivilege('button', 'download')"
          type="primary"
          icon="el-icon-s-order"
          @click="toDataTask"
          >批量下载结果查询</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"
          fixed="left"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="50"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="courseName" label="课程（代码）" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.courseName }}（{{ scope.row.courseCode }}）
          </template>
        </el-table-column>
        <el-table-column
          prop="paperNumber"
          label="试卷编号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="orgName"
          label="开课机构"
          width="150"
        ></el-table-column>
        <el-table-column prop="userName" label="命题老师" width="180"
          ><span slot-scope="scope"
            >{{ scope.row.userName }}（{{ scope.row.loginName }}）</span
          ></el-table-column
        >
        <el-table-column prop="paperType" label="全部卷型" width="100">
        </el-table-column>
        <el-table-column label="未曝光卷型" width="90">
          <template slot-scope="scope">
            <span v-if="scope.row.unexposedPaperType"
              >卷{{ scope.row.unexposedPaperType }}</span
            >
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column label="已曝光卷型" width="90"
          ><template slot-scope="scope">
            {{ scope.row.exposedPaperType | defaultFieldFilter }}
          </template></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, sizes, prev, pager, next, jumper"
          :pager-count="5"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
          @size-change="pageSizeChange"
        >
        </el-pagination>
      </div>
    </div>

    <!-- DownloadSetDialog -->
    <download-set-dialog :download-set="downloadSet" ref="DownloadSetDialog">
    </download-set-dialog>
    <!-- data-task-dialog -->
    <data-task-dialog
      v-if="checkPrivilege('button', 'download')"
      ref="DataTaskDialog"
      task-type="DOWNLOAD_PAPER_FILE_BATCH"
    ></data-task-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: "",
        courseId: "",
        paperNumber: "",
        orgId: "",
        userParam: "",
      },
      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]
      );
    },
  },
  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,
        };
      }
      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("下载成功！");
    },
    toDataTask() {
      this.$refs.DataTaskDialog.open();
    },
  },
};
</script>
