<template>
  <div class="print-plan-push-manage">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" 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.syncDate"
            type="date"
            value-format="timestamp"
            placeholder="同步日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label-width="0px">
          <el-button
            type="primary"
            :disabled="!filter.semesterId || !filter.examTypeId"
            @click="search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button
          icon="el-icon-circle-plus-outline"
          type="primary"
          :loading="downloading"
          @click="toSync"
        >
          人工同步数据
        </el-button>
      </div>
    </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="syncDate" label="同步日期" width="170">
          <span slot-scope="scope">{{
            scope.row.syncDate | timestampFilter
          }}</span>
        </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 class-name="action-column" label="操作" width="160">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.downloadStatus"
              class="btn-primary"
              type="text"
              @click="toDownload(scope.row)"
              >下载</el-button
            >
            <el-button
              v-if="!scope.row.useFile"
              class="btn-primary"
              type="text"
              @click="toUse(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"
          :page-sizes="[20, 50, 100]"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  businessDataList,
  downloadBusinessData,
  manualSync,
  manualUse,
} from "../api";
import { downloadByApi } from "@/plugins/download";

export default {
  name: "business-data-manage",
  data() {
    return {
      filter: {
        semesterId: "",
        examTypeId: "",
        syncDate: "",
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      downloading: false,
      dataList: [],
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size,
      };
      const data = await businessDataList(datas);
      this.dataList = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    search() {
      this.toPage(1);
    },
    async toSync() {
      if (this.downloading) return;
      this.downloading = true;
      await manualSync().catch(() => {});
      this.downloading = false;
    },
    async toDownload(row) {
      if (this.downloading) return;
      this.downloading = true;

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

      if (!res) return;
      this.$message.success("下载成功！");

      // this.$message.success(
      //   "已开始下载，下载需要时间，请稍后到【下载列表】模块中查看数据"
      // );
    },
    async toUse(row) {
      await manualUse(row.id);
      this.$message.success("操作成功！");
      this.getList();
    },
  },
};
</script>
