<template>
  <div class="task-set h-ull w-full">
    <table-header>
      <div slot="operation">
        <el-button size="small" type="primary" v-hasPermi="['system:task:add']" @click="handleAdd"
          >新增任务</el-button
        >
        <el-button
          size="small"
          type="danger"
          v-hasPermi="['system:task:remove']"
          @click="handleDelAll"
          >批量删除</el-button
        >
      </div>
      <div slot="search" class="flex" v-hasPermi="['system:task:query']">
        <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>
    </table-header>
    <!-- 表格 -->
    <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>
    <y-pagination
      :total="total"
      :page.sync="page"
      :pageSize.sync="size"
      @change="getTaskList"
    ></y-pagination>
    <!-- 新增参数 -->
    <AddTask
      ref="AddTask"
      :show.sync="addVisible"
      :title="title"
      :dict="dict"
      @success="getTaskList"
    ></AddTask>
  </div>
</template>

<script>
import TableHeader from '@/components/TableHeader/TableHeader.vue';
import YTable from '@/components/YTable/YTable.vue';
import YPagination from '@/components/YPagination/YPagination.vue';
import AddTask from './components/AddTask.vue';
import { getTaskList, delTask } from '@/api/system';
export default {
  components: {
    TableHeader,
    AddTask,
    YTable,
    YPagination
  },
  data() {
    return {
      column: [
        {
          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: '执行策略',
          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
                  v-hasPermi={['system:task:edit']}
                  class="primary cursor-pointer"
                  onClick={() => this.handleEdit(row)}
                >
                  编辑
                </span>
                <span
                  v-hasPermi={['system:task:remove']}
                  class="delete cursor-pointer"
                  onClick={() => this.handleDel(row.id)}
                >
                  删除
                </span>
              </div>
            );
          }
        }
      ],
      searchVal: '',
      tableData: [],
      page: 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.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.$clearSelection('MyTable');
      this.getTaskList();
    },
    // 重置
    handleReset() {
      this.searchVal = '';
      this.page = 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>
