<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">
      <y-table
        :max-height="650"
        :data="tableData"
        :column="columns"
        border
        :loading="loading"
        @selection-change="handleSelectionChange"
        ref="multipleTable"
      >
        <template slot="excuteStrategy" slot-scope="scope">
          <el-tag size="small" type="info">{{
            dict.excuteStrategy[scope.row.excuteStrategy]
          }}</el-tag>
        </template>

        <template slot="status" slot-scope="scope">
          <el-tag size="small" type="info">{{
            dict.status[scope.row.status]
          }}</el-tag>
        </template>

        <template slot="action" 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>
      </y-table>
    </div>
    <y-pagination
      :total="total"
      :page="page"
      :pageSize="size"
      @change="getTaskList"
    ></y-pagination>
    <!-- 新增参数 -->
    <AddTask
      ref="AddTask"
      :addVisible.sync="addVisible"
      :title="title"
      :dict="dict"
      @addSuccess="getTaskList"
    ></AddTask>
  </div>
</template>

<script>
import TableHeader from "@/components/TableHeader.vue";
import AddTask from "./modal/AddTask.vue";
import { getTaskList, delTask } from "@/api/system";
export default {
  components: {
    TableHeader,
    AddTask,
  },
  data() {
    return {
      searchVal: "",
      tableData: [],
      page: 1,
      size: 10,
      total: 10,
      loading: false,
      selectKeys: [],
      addVisible: false,
      title: "新增参数",
      dict: {}, // 字典
      columns: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.page - 1) * this.size + index + 1;
          },
        },
        {
          label: "任务名称",
          prop: "name",
          align: "center",
        },
        {
          label: "执行主机",
          prop: "excuteHost",
          align: "center",
        },
        {
          label: "执行关键字",
          prop: "taskKey",
          align: "center",
        },
        {
          label: "执行策略",
          slot: true,
          prop: "excuteStrategy",
          align: "center",
        },
        {
          label: "最后执行主机",
          prop: "lastExcuteHost",
          align: "center",
        },
        {
          label: "最后执行时间",

          prop: "lastExcuteTime",
          align: "center",
        },

        {
          label: "任务状态",
          slot: true,
          prop: "status",
          align: "center",
        },

        {
          label: "操作",
          slot: true,
          prop: "action",
          align: "center",
          width: "100",
        },
      ],
    };
  },
  created() {
    this.getTaskList();
  },
  computed: {},
  methods: {
    // 系统事项列表
    async getTaskList() {
      this.loading = true;
      let res = await getTaskList({
        page: this.page,
        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.page > 1) {
          this.page -= 1;
          this.getTaskList();
        }
        this.tableData = data;
        this.total = total;
      }
      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.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getTaskList();
    },
    // 重置
    handleReset() {
      this.searchVal = "";
      this.page = 1;
      this.$refs.multipleTable.clearSelection();
      this.getTaskList();
    },
    // 选中
    handleSelectionChange(select) {
      this.selectKeys = select;
    },

    // 新增
    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();
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
  },
};
</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>
