<template>
    <manager-ui
        title="任务调度"
        :show-pagination="true"
        :show-download-btn="false"
        :query-model="queryParams.taskMainMap"
        :query-items="queryItems"
        :static-data="statics"
        :table-data="tableData"
        :column-items="columnItems"
        :pagination="page"
        @search="search"
        @searchClick="page.current=1; search()"
        @resetForm="page.current=1; search()"
        @create="createdObj"
        @selection-change="handleSelectionChange"
    >
        <template slot="tableTools">
            <el-button type="text" icon="el-icon-delete-solid" @click="batchdel">删除</el-button>
        </template>

        <template #isPause="{scope}">
            {{ scope.row.isPause | toCase(statics.isPause, 'value', 'label') }}
        </template>
        
        <template #operate="{scope}">
            <el-context-menu
                :menus="menus"
                :scope="scope"
                :default="defaultV"
                :visibles="[
                    scope.row.isPause != '1',
                    scope.row.isPause != '0'
                ]"
                @oper="clickRow"
            />
        </template>
    </manager-ui>
</template>
<script>

import { getTaskList, delTaskList, execTaskStatus, changeTaskStatus } from './api/taskScheduing'

export default {
  name: 'taskScheduing',
  data() {
    return {
        tableData: [],
        multipleSelection: [],
        statics: {
            isPause: [
                {
                    label: "运行中",
                    value: "0",
                },
                {
                    label: "已暂停",
                    value: "1",
                }
            ]
        },
        queryItems: [
            {
                type: 'input',
                span: 12,
                key: 'jobName',
                label: '任务名称'
            },
            {
                type: 'datetime',
                span: 12,
                key: 'createTime',
                label: '创建时间',
                dataType: 'datetimerange',
                format: 'yyyy-MM-dd HH:mm:ss',
                valueFormat: 'yyyy-MM-dd HH:mm:ss'
            }
        ],
        columnItems: [
            {
                _key: 'selection',
                type: 'selection',
                width: '50'
            },
            {
                _key: 'jobId',
                prop: 'jobId',
                label: '任务ID',
                showOverflowTooltip: true,
            },
            {
                _key: 'jobName',
                prop: 'jobName',
                label: '任务名称',
                showOverflowTooltip: true,
            },
            {
                _key: 'serviceName',
                prop: 'serviceName',
                label: '服务名称',
                showOverflowTooltip: true,
            },
            {
                _key: 'beanName',
                prop: 'beanName',
                label: 'Bean名称',
                showOverflowTooltip: true,
            },
            {
                _key: 'methodName',
                prop: 'methodName',
                label: '执行方法',
                showOverflowTooltip: true,
            },
            {
                _key: 'params',
                prop: 'params',
                label: '参数',
                showOverflowTooltip: true,
            },
            {
                _key: 'cronExpression',
                prop: 'cronExpression',
                label: 'cron表达式',
                width: 110,
                showOverflowTooltip: true,
            },
            {
                _key: 'isPause',
                prop: 'isPause',
                label: '状态',
                showOverflowTooltip: true,
                render: true
            },
            {
                _key: 'description',
                prop: 'description',
                label: '描述',
                showOverflowTooltip: true,
            },
            {
                _key: 'createTime',
                prop: 'createTime',
                label: '创建日期',
                showOverflowTooltip: true,
                width: 150
            },
            {
                _key: 'operate',
                prop: 'operate',
                label: '操作',
                render: true,
                width: 120
            }
        ],
        defaultV: 3,
        menus: [
            {
                text: '查看',
                command: 'detail',
                icon: 'el-icon-s-order'
            },
            {
                text: '修改',
                command: 'modify',
                icon: 'el-icon-edit'
            },
            {
                text: '删除',
                command: 'del',
                icon: 'el-icon-delete',
                color: 'red'
            },
            {
                text: '执行',
                command: 'exec',
                icon: 'el-icon-delete',                
            },
            {
                text: '暂停',
                command: 'pause',
                visible: true,
                icon: 'el-icon-delete',                
            },
            {
                text: '恢复',
                command: 'resume',
                visible: true,
                icon: 'el-icon-delete',                
            },
        ],
        queryParams: {
            taskMainMap: {
                jobName: null,
                createTime: null,
                startTime: null,
                endTime: null,
            },
            page:{
                current: 1,
                size: 10
            }
        },
        page: {
            current: 1,
            size: 10,
            total: 0
        },
        //删除数据
        deleteData: {
            taskMainMap: {
            ids: [],
            },
        }
    }
  },
  created() {
    this.search()
  },
  methods: {
    createdObj() {
        this.$router.push('/asp/taskScheduingAdd/add')
    },
    resetForm() {
        console.log(this.$refs.form)
        this.$refs.form.resetFields()
        this.search()
    },
    handleSelectionChange(val) {
        this.multipleSelection = val
    },
    search() { // 获取查询数据
        if (this.queryParams.taskMainMap.createTime != null) {
            [this.queryParams.taskMainMap.startTime, this.queryParams.taskMainMap.endTime] = this.queryParams.taskMainMap.createTime
        }
        let { current, size } = JSON.parse(JSON.stringify(this.page))
        this.queryParams.page = { current, size }
        getTaskList(this.queryParams).then((resp) => {
            if (resp.data.returnCode == "0") {
                this.tableData = resp.data.data.list;
                const { current, size, total } = resp.data.data.page
                this.page.current = current
                this.page.size = size
                this.page.total = total
            }
        }, (error) => {
           this.$message.error(error.message);
        })
    },
    clickRow: function(command, scope, index, rows) {
      index = scope.row.jobId;
      this.oper = command;
      if (command === 'del') {
        this.deleteData.taskMainMap.ids = [{id: index}];
        this.confirmDel()
      } else if (command === "modify" || command === "detail") {
        this.$router.push(`/asp/taskScheduingAdd/${command}/${encodeURIComponent(JSON.stringify(scope.row))}`)
      } else if (command === "exec") {
        execTaskStatus(scope.row.serviceName, index).then(
          (resp) => {
            if (resp.data.returnCode == "0") {
              this.$message.success("执行成功");
              this.search()
            }
          },
          error => {
            this.$message.error(error.message);
          }
        );
      } else if (command === "pause" || command === "resume") {
        changeTaskStatus(scope.row.serviceName, index).then(
          (resp) => {
            if (resp.data.returnCode == "0") {
              this.$message.success(`${command === 'pause' ? '暂停' : '恢复'}成功`);
              this.search();
            }
          },
          error => {
            this.$message.error(error.message);
          }
        );
      }
    },
    batchdel() {
      let list = [],
        idObj = null;
      for (let temp in this.multipleSelection) {
        idObj = {
          id: null,
        };
        idObj.id = this.multipleSelection[temp].jobId;
        list.push(idObj);
      }
      if (list.length >= 1) {
        this.deleteData.taskMainMap.ids = list;
        this.confirmDel();
      } else {
        this.$message.warning("未选择行");
      }
    },
    //删除前确认
    confirmDel() {
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.deletefilter();
        }).catch(() => {
            this.$message({
            type: 'info',
            message: '已取消删除'
            })
        })
    },
    deletefilter() {
        delTaskList(this.deleteData).then((resp) => {
            if (resp.data.returnCode == "0") {
                this.search();
                this.$message.success('删除成功!')
            }
        }, (error) => {
            this.$message.error(error.message)
        })
    },
  }
}
</script>
