<template>
  <div>
    <div class="nav-title">调度日志</div>
    <div class="search">
      <el-form
        class="search-form"
        :model="form"
        ref="form"
        :inline="true"
        size="mini"
      >
        <el-form-item label="执行器：">
          <el-select v-model="form.jobGroup" placeholder="请选择">
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="form.logStatus" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="dateRange"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="toSearch">查询</el-button>
          <!-- <el-button type="primary" @click="handleEdit()">新增</el-button> -->
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ background: '#EAF1FE', color: '#666666' }"
      >
        <el-table-column prop="jobId" label="任务ID" align="center" />
        <el-table-column prop="triggerTime" label="调度时间" align="center" />
        <el-table-column prop="triggerCode" label="调度结果" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.triggerCode === 200 ? 'success' : 'danger'"
              >{{ scope.row.triggerCode === 200 ? "成功" : "失败" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="triggerMsg" label="调度备注" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="handleView(scope.row.triggerMsg)"
              >查看</el-button
            >
            <!-- <div v-html="scope.row.triggerMsg"></div> -->
          </template>
        </el-table-column>
        <el-table-column prop="handleTime" label="执行时间" align="center" />
        <el-table-column prop="handleCode" label="执行结果" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.handleCode === 0 ? 'success' : 'danger'">{{
              scope.row.handleCode === 0 ? "成功" : "失败"
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="handleMsg" label="执行备注" align="center" />
      </el-table>
      <Pagination
        :limit="params.size"
        :page="params.current"
        :total="total"
        class="pagination"
        @pagination="handlePageChange"
      />
    </div>
    <el-dialog
      title="告警详情"
      :visible.sync="centerDialogVisible"
      width="600px"
      center
    >
      <div v-html="dialogInfo"></div>
    </el-dialog>
  </div>
</template>

<script>
import { jobgroupList, joblogList } from "@/api/job.js";
export default {
  data() {
    return {
      options: [
        { label: "全部", value: -1 },
        { label: "成功", value: 1 },
        { label: "失败", value: 2 },
        { label: "进行中", value: 3 },
      ],
      tableData: [],
      dialogInfo: "",
      centerDialogVisible: false,
      form: {
        jobGroup: 0,
        jobId: 0,
        logStatus: -1,
      },
      groupList: [],
      params: {
        current: 1,
        size: 10,
      },
      total: 0,
      dateRange: [],
    };
  },
  mounted() {
    this.handleJobGroupList();
    this.handleJoblogList();
  },
  methods: {
    toSearch() {
      this.params.current = 1;
      this.handleJoblogList();
    },
    async handleJobGroupList() {
      let data = {
        start: 0,
        length: 999,
      };
      const queryParams = new URLSearchParams(data).toString();
      const res = await jobgroupList(queryParams);
      res.data.unshift({
        id: 0,
        title: "全部",
      });
      this.groupList = res.data;
      // this.form.jobGroup = res.data[0].id;
    },
    handleView(e) {
      this.dialogInfo = e;
      this.centerDialogVisible = true;
    },
    handlePageChange(pageData) {
      this.params.size = pageData.size;
      this.params.current = pageData.page;
      this.handleJoblogList();
    },
    async handleJoblogList() {
      // filterTime
      //       this.form.startUploadTime = this.dateRange[0];
      // this.form.endUploadTime = this.dateRange[1];
      let data = {
        ...this.form,
        start: (this.params.current - 1) * this.params.size,
        length: this.params.size,
      };
      if (this.dateRange.length > 0) {
        data.filterTime = this.dateRange[0] + " - " + this.dateRange[1];
      }
      const queryParams = new URLSearchParams(data).toString();
      const res = await joblogList(queryParams);
      console.log(res);
      this.tableData = res.data;
      this.total = res.recordsTotal;
    },
  },
};
</script>

<style lang="scss" scoped>
.nav-title {
  color: #02a7f0;
  font-size: 20px;
  position: relative;
  padding-left: 16px;
  font-weight: bold;
  line-height: 28px;
}
.nav-title::before {
  content: "";
  position: absolute;
  left: 0px;
  top: 1px;
  width: 5px;
  height: 100%;
  background-color: #02a7f0;
  border-radius: 4px;
}
.search {
  margin: 16px 0;
  .search-form {
    padding: 10px;
    background-color: #eaf1fe;
    margin-bottom: 20px;
    border-radius: 8px;
    .el-form-item--mini.el-form-item,
    .el-form-item--small.el-form-item {
      margin-bottom: 0;
    }
  }
}
</style>