<template>
  <div class="task-set">
    <TableHeader>
      <div slot="left">
        <el-button size="small" type="primary" @click="handleAdd"
          >新增任务</el-button
        >
        <el-button size="small" type="danger" @click="handleDelAll"
          >批量移除</el-button
        >
      </div>
      <div slot="right" class="flex">
        <el-input
          size="small"
          v-model="searchVal"
          style="width: 200px"
          class="ml10 mr10"
          placeholder="请输入任务名称搜索"
          @keyup.native.enter="handleSearch"
        ></el-input>
        <el-button size="small" type="primary" @click="handleSearch"
          >搜 索</el-button
        >
        <el-button size="small" @click="handleReset">重 置</el-button>
      </div>
    </TableHeader>
    <!-- 表格 -->
    <div class="table-content">
      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        tooltip-effect="dark"
        style="width: 100%"
        max-height="676px"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          reserve-selection
          type="selection"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column
          type="index"
          label="序号"
          width="55"
          align="center"
          :index="(index) => (current - 1) * size + index + 1"
        >
        </el-table-column>
        <el-table-column label="任务名称" align="center" prop="name">
        </el-table-column>
        <el-table-column label="执行主机" align="center" prop="excuteHost">
        </el-table-column>
        <el-table-column label="执行关键字" align="center" prop="taskKey">
        </el-table-column>
        <el-table-column label="执行策略" align="center" prop="excuteStrategy">
          <template slot-scope="scope">
            <el-tag type="info">{{
              filterItems(scope.row.excuteStrategy, dict.excuteStrategy)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="最后执行主机"
          align="center"
          prop="lastExcuteHost"
        >
        </el-table-column>
        <el-table-column
          label="最后执行时间"
          align="center"
          prop="lastExcuteTime"
        >
        </el-table-column>
        <el-table-column label="任务状态" align="center" prop="status">
          <template slot-scope="scope">
            <!-- <el-tag v-if="scope.row.status == 1" type="success">执行中</el-tag> -->
            <el-tag size="small" type="info">{{
              filterItems(scope.row.status, dict.status)
            }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <div class="flex jca">
              <span class="primary pointer" @click="handleEdit(scope.row)"
                >编辑</span
              >
              <span class="delete pointer" @click="handleDel(scope.row.id)"
                >删除</span
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Pagination
      :total="total"
      :current="current"
      :size="size"
      @currentChange="changePagination"
      @sizeChange="changeSize"
    ></Pagination>
    <!-- 新增参数 -->
    <AddTask
      ref="AddTask"
      :addVisible.sync="addVisible"
      :title="title"
      :dict="dict"
      @addSuccess="getTaskList"
    ></AddTask>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import Pagination from "@/components/Pagination.vue";
import AddTask from "./modal/AddTask.vue";
import { getTaskList, delTask } from "@/api/system";
export default {
  components: {
    TableHeader,
    AddTask,
    Pagination,
  },
  data() {
    return {
      searchVal: "",
      tableData: [],
      current: 1,
      size: 10,
      total: 10,
      loading: false,
      selectKeys: [],
      addVisible: false,
      title: "新增参数",
      dict: {}, // 字典
    };
  },
  created() {
    this.getTaskList();
  },
  computed: {},
  methods: {
    // 系统事项列表
    async getTaskList() {
      this.loading = true;
      let res = await getTaskList({
        page: this.current,
        size: this.size,
        name: `%${this.searchVal}%`,
      });
      if (res.data.code == 1) {
        let { data, total, dict } = res.data.data;
        this.dict = dict;
        if (!data.length && this.current > 1) {
          this.current -= 1;
          this.getTaskList();
        }
        this.tableData = data;
        this.total = total;
        this.$refs.multipleTable.bodyWrapper.scrollTop = 0;
      }
      this.loading = false;
    },

    // 批量移除
    handleDelAll() {
      if (!this.selectKeys.length) {
        this.$message.warning("请先勾选数据");
        return;
      }
      let ids = this.selectKeys.map((v) => v.id).join(",");
      this.handleDel(ids);
    },
    // 搜索
    handleSearch() {
      this.current = 1;
      this.$refs.multipleTable.clearSelection();
      this.getTaskList();
    },
    // 重置
    handleReset() {
      this.searchVal = "";
      this.current = 1;
      this.$refs.multipleTable.clearSelection();
      this.getTaskList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectKeys = select;
    },
    // 翻页
    changePagination(cur) {
      this.current = cur;
      this.getTaskList();
    },
    // 改变没有显示数量
    changeSize(size) {
      this.size = size;
      this.getTaskList();
    },
    // 新增
    handleAdd() {
      this.title = "新增任务";
      this.$refs.AddTask.onAdd();
      this.addVisible = true;
    },
    // 编辑
    handleEdit(row) {
      this.title = "编辑任务";
      this.$refs.AddTask.onEdit(row);
      this.addVisible = true;
    },
    // 移除
    handleDel(id) {
      this.$confirm("此操作将删除所选数据，是否继续？", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
      })
        .then(async () => {
          let res = await delTask({ id });
          let { code, msg } = res.data;
          if (code === 1) {
            this.$message.success(msg);
            this.getTaskList();
            this.$refs.multipleTable.clearSelection();
            this.selectKeys = [];
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
    // 过滤表格数据
    filterItems(key, dict = {}) {
      let val = "";
      Object.keys(dict).forEach((keys) => {
        if (key == keys) {
          val = dict[keys];
        }
      });
      return val;
    },
  },
};
</script>

<style lang="less" scoped>
.task-set {
  width: 100%;
  min-height: 100%;
  .short {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .full-name {
    color: rgb(172, 170, 170);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  :deep(.el-card) {
    height: auto;
  }
  // .table-content {
  //   height: 550px;
  // }
}
</style>