<template>
  <div class="m-container">
    <h2>定时任务 <div class="squrt"></div>
    </h2>
    <div class="m-core">
      <div class="tableWrap">
        <el-table style="width: 100%"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}"
                  :data='tableData'>
          <el-table-column prop="id"
                           align='center'
                           label="任务编号">
          </el-table-column>
          <el-table-column prop="description"
                           align='center'
                           label="英文名">
          </el-table-column>
          <el-table-column prop="name"
                           align='center'
                           label="任务名">
          </el-table-column>
          <el-table-column align='center'
                           prop="timeExp"
                           label="表达式">
          </el-table-column>
          <el-table-column prop="status"
                           align='center'
                           label="任务状态">
            <template slot-scope="scope">
              <div>
                {{ getStatus(scope.row.status) }}
              </div>
            </template>
          </el-table-column>
          
          <el-table-column align='center'
                           label="操作">
            <template slot-scope="scope">
              <el-button type="text" v-if="scope.row.status == 1 || scope.row.status == 3" @click="onPause(scope.row)">暂停</el-button>
              <el-button type="text" v-if="scope.row.status == 1 || scope.row.status == 3"  @click="onExecute(scope.row)">执行</el-button>
              <el-button type="text" v-if="scope.row.status == 0" @click="onResume(scope.row)">恢复</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>
<script>
import { taskList, pause, execute, resume } from '@/api/task'
export default {
  data () {
    return {
      tableData: []
    }
  },
  mounted () {
    this.__init()
  },
  methods: {
    __init () {
      taskList().then(res => {
        if(res.code == 200) {
          let result = res.data
          this.tableData = result.rows
        }
      })
    },
    onPause(row) {
      this.$confirm('您确定暂停吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pause({ taskId: row.id }).then(res => {
          if(res.code == 200) {
            this.$message.success('暂停成功')
            this.__init()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
    onExecute(row) {
      this.$confirm('您确定执行吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        execute({ taskId: row.id }).then(res => {
          if(res.code == 200) {
            this.$message.success('执行成功')
            this.__init()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
    onResume(row) {
      this.$confirm('您确定恢复吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        resume({ taskId: row.id }).then(res => {
          if(res.code == 200) {
            this.$message.success('恢复成功')
            this.__init()
          } else {
            this.$message.error(res.msg)
          }
        })
      }).catch(() => { })
    },
    getStatus(index) {
      let template = {
        // -1: "执行失败",
        0: "暂停",
        1: "准备就绪",
        2: "执行中",
        3: "执行成功"
      }
      if(index == -1) {
        return '执行失败'
      }
      return template[index]
    }
  }
}
</script>
<style lang="scss" scope>

</style>