<template>
  <div class="export-download">
    <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb>
    <div class="component-content screenSet" id="screenSet">
      <p>导出文件列表</p>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          prop="title"
          label="名称"
          align="center"
          min-width="150"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="createdTime" label="时间" align="center" min-width="120">
          <template slot-scope="scope">{{ scope.row.createdTime }}</template>
        </el-table-column>
        <el-table-column prop="status" label="状态" align="center" min-width="150">
          <template slot-scope="scope">{{ scope.row.status | exportStatus }}</template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" min-width="70">
          <template slot-scope="scope">
            <el-button
              type="primary"
              :disabled="scope.row.buttonStatus"
              size="small"
              @click="update(scope.row)"
            >{{ scope.row.buttonText }}</el-button>
          </template>
        </el-table-column>
        <div slot="empty">
          <div class="table-empty">
            <img src="../../assets/image/no-content1.png" />
            <p>没有相关数据，请重新选择查询范围</p>
          </div>
        </div>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="param.pageNo"
          :page-sizes="[20, 50 ,100]"
          :page-size="param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalRows"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import BreadCrumb from "../../components/breadcrumb.vue";
import * as commonUtil from "../../utils-o/utils";
import { openLoading, closeLoading } from "../../utils-o/utils";
import axios from "axios";
let vm = null;
export default {
  components: {
    BreadCrumb
  },
  data() {
    return {
      curmbFirst: "学情报告",
      curmbSecond: "导出下载",
      projectId: "",
      tableData: [],
      param: {
        pageSize: 20,
        pageNo: 1
      },
      totalRows: 0,
      totalTime: 3,
      queryData: {}
      // buttonText: "刷新",
      // buttonStatus: false,
    };
  },
  created() {
    vm = this;
    this.projectId = vm.getUrlSearch(window.location.href, "projectId");
    this.search();
  },
  mounted: function() {
    commonUtil.resizeHeight();
  },
  methods: {
    setButton() {
      for (let i = 0; i < vm.tableData.length; i++) {
        vm.tableData[i].index = i;
        if (vm.tableData[i].status == 1) {
          vm.tableData[i].buttonText = "下载";
          vm.tableData[i].buttonStatus = false;
        } else if(vm.tableData[i].status == 0) {
          vm.tableData[i].buttonText = "刷新";
          vm.tableData[i].buttonStatus = false;
        } else if(vm.tableData[i].status == 2) {
          vm.tableData[i].buttonText = "刷新";
          vm.tableData[i].buttonStatus = true;
        }
      }
    },
    search() {
      // this.setButton();
      let req = {
        projectId: vm.projectId
      };
      if(req.projectId == null) {
        req.projectId = '';
      }
      vm.reportGET("report/downLoad/getDownloadList", req).then(res => {
        if (res.code == "000000") {
          vm.tableData = res.data.data;
          vm.totalRows = res.data.totalRows;
          vm.setButton();
        }
      });
    },
    downLoad(row) {
      // vm.queryData = this.$route.query;
      // let req = vm.queryData;
      // openLoading(vm);
      // vm.reportGET("report/downLoad/downLoad", req).then(res => {
      //   closeLoading(vm);
      //   // console
      //   if (res.code == "000000") {
      //   }
      // });

      setTimeout(() => {
        window.open(row.fileUrl);
      }, 500);
    },
    getDownLoadStatus(row) {
      let req = {
        id: row.id
      };
      vm.reportGET("report/downLoad/getDownLoadStatus", req).then(res => {
        if (res.code == "000000") {
          vm.tableData[row.index].status = res.data.data;
          // console.log('res.data.data',res.data.data);
          if (res.data.data == 1) {
            vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
            vm.tableData[row.index].buttonText = "下载";
            vm.tableData[row.index].fileUrl = res.data.url;
          } else if(res.data.data == 0) {
            console.log("res", res, vm.tableData[row.index]);
            vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
            vm.tableData[row.index].buttonText = "刷新";
          } else if(res.data.data == 2) {
            vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
            vm.tableData[row.index].buttonText = "刷新";
            vm.tableData[row.index].buttonStatus = true;
          }
        } else {
          vm.$message(res.message);
          vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
          vm.tableData[row.index].buttonText = "刷新";
        }
      });
    },
    update(row) {
      if (vm.tableData[row.index].status == 1) {
        vm.downLoad(row);
      } else {
        this.$nextTick(function() {
          console.log(vm.tableData[row.index]);
          let nowTime = vm.totalTime;
          vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
          vm.tableData[row.index].buttonText = nowTime + "s后可以刷新";
          vm.tableData[row.index].buttonStatus = true;

          let clock = window.setInterval(() => {
            nowTime--;
            vm.tableData[row.index].title = vm.tableData[row.index].title + " ";
            vm.tableData[row.index].buttonText = nowTime + "s后可以刷新";
            if (nowTime <= 0) {
              window.clearInterval(clock);
              vm.tableData[row.index].title =
                vm.tableData[row.index].title + " ";
              // vm.tableData[row.index].buttonText = "刷新";
              vm.tableData[row.index].buttonStatus = false; //这里重新开启
              vm.getDownLoadStatus(row);
            }
          }, 1000);
        });
      }
    },
    // 更改数据显示数量
    handleSizeChange(val) {
      vm.param.pageSize = val;
      vm.search();
    },
    // 换页
    handleCurrentChange(val) {
      vm.param.pageNo = value;
      vm.search();
    }
  }
};
</script>
<style lang="scss">
.export-download {
  .component-content {
    background: #fff;
    padding: 10px;
  }
}
</style>