<template>
  <div class="monitoring">
    <header>
      <div class="left"></div>
      <div class="right">
        <el-button type="primary" icon="el-icon-plus" @click="inserterAdd"
          >添加插件</el-button
        >
        <el-button
          icon="el-icon-refresh"
          :loading="refreshLoading"
          @click="initMainTableData(searchForm)"
          circle
        ></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"
      >
        <template v-slot:diff_time="{ row }">
          <div>{{ getTimeHandler(row.diff_time) }}</div>
        </template>
      </BaseTable>
    </main>
    <footer>
      <basePage
        :pageModel="pageModel"
        @update:pageModel="currentChangeHandle"
      ></basePage>
    </footer>
  </div>
</template>

<script>
import configData from "./configData.js";
import dayjs from "dayjs";
import BaseTable from "@/views/intergrationTask/compoments/baseTable.vue";
import basePage from "@/views/intergrationTask/compoments/basePage.vue";
import { getApiModuleApi } from "@/api/apiChunks/index.js";
export default {
  data() {
    return {
      refreshLoading: false,
      searchForm: {
        task_name: "",
      },
      mainTabLoading: false,
      funData: [
        {
          type: "view",
          text: "查看",
          color: "#5a9cf8",
        },
      ],
      tableColumn: configData.tableColumn,
      tableData: [],
      pageModel: {
        pageIndex: 1,
        total: 10,
        limit: 10,
      },
    };
  },
  methods: {
    // 添加应用
    inserterAdd() {
      this.$router.push({ path: "/inserterAdmin/inserterAdd" });
    },
    // 时间处理
    getTimeHandler(time) {
      var days = parseInt(time / (1000 * 60 * 60 * 24));
      var hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
      var minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60));
      var seconds = (time % (1000 * 60)) / 1000;
      return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
    },
    // 初始化表单
    async initMainTableData(obj = {}) {
      this.refreshLoading = true;
      const res = await getApiModuleApi(
        {
          tl: "integrationTaskLivingService",
          as: "integrationTaskLiving",
          dj: "queryPagedJson",
        },
        {
          pageNum: this.pageModel.pageIndex,
          pageSize: this.pageModel.limit,
          ...obj,
        }
      );
      this.refreshLoading = false;
      if (res.status == 200) {
        this.tableData = res.attribute.list;
        this.pageModel.total = res.attribute.total;
      }
    },
    // 点击提交
    submitsearchForm() {
      this.initMainTableData(this.searchForm);
    },
    // 表单操作
    tableButtonHandle(val, item) {
      if (item.type === "view") {
        this.$router.push({
          path: "/applicationList/applicationAdd",
          query: { id: val.id },
        });
      } else if (item.type === "dele") {
        this.$confirm("确认删除？")
          .then(() => {})
          .catch(() => {});
      }
    },
    // 页码
    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;
        display: flex;
        > .chunk {
          margin-left: 10px;
        }
      }
    }
  }
  main {
    padding: 20px 20px 5px;
    height: 75vh;
    border-radius: 20px;
    background-color: #fff;
  }
  footer {
    margin-top: 20px;
  }
}
</style>
