<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">补推</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 dayjs from "dayjs";
import BaseTable from "./compoments/baseTable.vue";
import basePage from "./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: [
        {
          id: "task_name",
          title: "任务名称",
        },
        {
          id: "task_classes",
          title: "类型",
        },
        {
          id: "start_time",
          title: "开始时间",
          tooltip: true,
          width: 150,
        },
        {
          id: "end_time",
          title: "完成时间",
          tooltip: true,
          width: 150,
        },
        {
          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: {
    // 时间处理
    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;
        console.log(res);
      }
    },
    // 点击提交
    submitsearchForm() {
      this.initMainTableData(this.searchForm);
    },
    // 表单操作
    tableButtonHandle(val, item) {
      if (item.type === "view") {
        this.$router.push({ name: "taskLivingView", 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;
      }
    }
  }
  main {
    padding: 20px 20px 5px;
    height: 75vh;
    border-radius: 20px;
    background-color: #fff;
  }
  footer {
    margin-top: 20px;
  }
}
</style>