<template>
  <div class="task-set h-ull w-full">
    <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="mr-[10px]"
          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
        ref="MyTable"
        :loading="loading"
        :data="tableData"
        :column="column"
        border
        tooltip-effect="dark"
        :max-height="600"
        :row-key="(row) => row.id"
        @selection-change="handleSelectionChange"
      ></y-table>
    </div>
    <Pagination
      :total="total"
      :current.sync="current"
      :size.sync="size"
      @get="getTaskList"
    ></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 "./components/AddTask.vue";
import { getTaskList, delTask } from "@/api/system";
export default {
  components: {
    TableHeader,
    AddTask,
  },
  data() {
    return {
      column: [
        {
          label: "全选",
          type: "selection",
          width: "55",
          align: "center",
          reserveSelection: true,
        },
        {
          label: "序号",
          type: "index",
          width: "55",
          align: "center",
          index: (index) => {
            return (this.current - 1) * this.size + index + 1;
          },
        },

        {
          label: "任务名称",
          prop: "name",
          align: "center",
        },
        {
          label: "执行主机",
          prop: "excuteHost",
          align: "center",
        },
        {
          label: "执行关键字",
          prop: "taskKey",
          align: "center",
        },
        {
          label: "执行策略",
          prop: "excuteStrategy",
          align: "center",
          formatter: (row) => {
            return (
              <el-tag size="small" type="info">
                {this.dict.excuteStrategy[row.excuteStrategy]}
              </el-tag>
            );
          },
        },
        {
          label: "最后执行主机",
          prop: "lastExcuteHost",
          align: "center",
        },

        {
          label: "最后执行时间",
          prop: "lastExcuteTime",
          align: "center",
        },
        {
          label: "任务状态",
          prop: "status",
          align: "center",
          formatter: (row) => {
            return (
              <el-tag size="small" type="info">
                {this.dict.status[row.status]}
              </el-tag>
            );
          },
        },
        {
          label: "操作",
          align: "center",
          width: "120",
          formatter: (row) => {
            return (
              <div class="flex justify-center gap-4">
                <span
                  class="primary cursor-pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  class="delete cursor-pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          },
        },
      ],
      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.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.$clearSelection("MyTable");
      this.getTaskList();
    },
    // 重置
    handleReset() {
      this.searchVal = "";
      this.current = 1;
      this.$clearSelection("MyTable");
      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.$clearSelection("MyTable");
          }
        })
        .catch(() => {
          console.log("取消成功！");
        });
    },
  },
};
</script>

<style lang="less" scoped></style>
