<template>
  <div class="monitoring">
    <header>
      <div class="left">
        <div class="title">
          <h1>后台任务日志</h1>
        </div>
        <div class="search">
          <el-input
            placeholder="任务名称"
            v-model="searchForm.task_name"
            @keyup.enter.native="submitsearchForm"
          >
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
        </div>
      </div>
      <div class="right">
        <el-button type="primary" @click="monPush" :loading="pushLoading"
          >输出</el-button
        >
        <el-button
          icon="el-icon-refresh"
          circle
          @click="initMainTableData(searchForm)"
          :loading="refreshLoading"
        ></el-button>
      </div>
    </header>
    <main>
      <BaseTable
        ref="mainTable"
        :tableData="tableData"
        :tableColumn="tableColumn"
        :funData="funData"
        :funWidth="funWidth"
        :showIndex="true"
        :tabLoading="mainTabLoading"
        @onFunc="tableButtonHandle"
        :tableHeight="'70vh'"
        :border="false"
      ></BaseTable>
    </main>
    <footer>
      <basePage
        :pageModel="pageModel"
        @update:pageModel="currentChangeHandle"
      ></basePage>
    </footer>
  </div>
</template>

<script>
import BaseTable from "./compoments/baseTable.vue";
import basePage from "./compoments/basePage.vue";
import { getApiModuleApi, getExcelApi } from "@/api/apiChunks/index.js";
export default {
  data() {
    return {
      searchForm: {
        task_name: "",
      },
      refreshLoading: false,
      mainTabLoading: false,
      pushLoading: false,
      funData: [
        {
          type: "view",
          text: "查看",
          color: "#5a9cf8",
        },
      ],
      tableColumn: [
        {
          id: "task_name",
          title: "任务名称",
        },
        {
          id: "task_classes",
          title: "类型",
        },
        {
          id: "start_time",
          title: "开始时间",
          tooltip: true,
          width: 200,
        },
        {
          id: "end_time",
          title: "完成时间",
          tooltip: true,
          width: 200,
        },
        {
          id: "diff_time",
          title: "耗时",
          tooltip: true,
        },
        {
          id: "result",
          title: "结果",
        },
        {
          id: "server_name",
          title: "服务器名",
          tooltip: true,
        },
        {
          id: "server_living",
          title: "服务器实例",
          tooltip: true,
        },
        {
          id: "explain",
          title: "说明",
          tooltip: true,
        },
      ],
      tableData: [],
      pageModel: {
        pageIndex: 1,
        total: 10,
        limit: 10,
      },
    };
  },
  methods: {
    async monPush() {
      this.pushLoading = true;
      const res = await getExcelApi(
        {
          tl: "integrationTaskLogService",
          as: "integrationTaskLog",
          dj: "queryTaskLogExcel",
        },
        {
          task_name: this.searchForm.task_name,
        }
      );
      this.pushLoading = false;
      console.log(res);
      if (res.status == 500) {
        this.$message({
          type: "error",
          message: "下载失败！",
        });
      } else {
        let url = window.URL.createObjectURL(
          new Blob([res], { type: "application/vnd.ms-excel" })
        );
        let fileName = "后台任务日志";
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", fileName + ".xlsx");
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
      }
    },
    async initMainTableData(obj = {}) {
      this.refreshLoading = true;
      const res = await getApiModuleApi(
        {
          tl: "integrationTaskLogService",
          as: "integrationTaskLog",
          dj: "queryListJson",
        },
        {
          pageNum: this.pageModel.pageIndex,
          pageSize: this.pageModel.limit,
          ...obj,
        }
      );
      this.refreshLoading = false;
      if (res.status == 200) {
        this.tableData = res.attribute;
      }
    },
    submitsearchForm() {
      this.initMainTableData(this.searchForm);
    },
    tableButtonHandle(val, item) {
      if (item.type === "view") {
        this.$router.push({ name: "taskLogsView", query: { id: val.id } });
      } else if (item.type === "dele") {
        this.$confirm("确认删除？")
          .then(() => {})
          .catch(() => {});
      } else if (item.type === "edit") {
        this.$router.push({});
      }
    },
    currentChangeHandle(pageModel) {
      this.pageModel = pageModel;
      this.$nextTick(() => {
        this.initMainTableData(this.searchForm);
      });
    },
  },
  computed: {
    // 操作框的宽度
    funWidth() {
      return this.funData.length * 70;
    },
  },
  components: {
    BaseTable,
    basePage,
  },
  created() {
    this.initMainTableData();
  },
};
</script>

<style scoped lang='scss'>
.monitoring {
  position: relative;
  width: 100%;
  header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    background-color: #fff;
    border-radius: 20px;

    .left {
      align-items: center;
      display: flex;
      .search {
        margin-left: 30px;
      }
    }
  }
  main {
    padding: 20px;
    height: 750px;
    border-radius: 20px;
    background-color: #fff;
  }
  footer {
    margin-top: 20px;
  }
}
</style>