import breadcrumbComponent from '@/components/breadcrumb.vue'
import tableComponent from '@/components/table.vue'
import list from '@/mixins/list'
import routerUserMenu from '@/mixins/router-user-menu'

export default {
  name: 'DatabaseList',
  components: {breadcrumbComponent, tableComponent},
  mixins: [list, routerUserMenu],
  data () {
    return {
      searchForm: {
        taskName: '',
        pageSize: 10,
        page: 1
      },
      // 列表数据
      tableData: {
        data: [],
        total: 0
      },
      // 列表设置
      tableOption: {
        // 是否显示序号
        index: true,
        // 是否显示勾选框
        select: false,
        // 默认排序
        defaultSort: {prop: 'updateTime', order: 'descending'},
        // 列
        tableLabel: [
          {prop: 'taskName', title: '任务名称', width: '100'},
          {prop: 'taskStatusDesc', title: '任务状态', width: '100'},
          {prop: 'taskCron', title: '表达式', width: '100'},
          {prop: 'updateTime', title: '修改时间', width: '100', sort: true},
          {prop: 'userName', title: '修改人', width: '100'},
        ],
        // 操作
        tableOperate: {
          width: '250',
          buttons: [
            {label: '查看', methods: 'view', color: 'safe'},
            {label: '立即执行一次', methods: 'execute', color: 'link',show: {'taskStatus':'1'}},
            {label: '启用', methods: 'start',show: {'taskStatus':'2'}},
            {label: '停用',methods: 'stop',show: {'taskStatus':'1'},color:'warn'},
            {label: '修改', methods: 'edit', color: 'safe',show: {'taskStatus':'2'}},
            {label: '删除', methods: 'del', color: 'warn',show: {'taskStatus':'2'}}
          ]
        },
      },
    }
  },
  created () {
  },
  mounted () {
    // this.getList()
  },
  methods: {

    /* 立即执行一次 */
    execute (obj) {
      let params = {id: obj.id,jobId: obj.id, operateType: 3}
      this.$api.task.operate(params)
        .then(res => {
          if (res.code == 1){
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    /* 启用 */
    start (obj) {
      let params = {id: obj.id,jobId: obj.id, operateType: 1}
      this.$api.task.operate(params)
        .then(res => {
          if (res.code == 1){
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    /* 停用 */
    stop (obj) {
      let params = {id: obj.id,jobId: obj.id, operateType: 2}
      this.$api.task.operate(params)
        .then(res => {
          if (res.code == 1){
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    /* 编辑功能 */
    edit (obj) {
      this.go('TaskEdit', {id: obj.id})
    },
    /* 查看功能 */
    view (obj) {
      this.go('TaskView',{id:obj.id,readonly:1})
    },
    /* 删除功能 */

    // 执行删除
    delAction(obj){
      let params={id: obj.id}
      this.$api.task.toDel(params)
        .then(res => {
          if (res.code == 1) {
            this.$message.success(res.msg)
            this.getList()
          }
        })
    },
    // 删除
    del(obj){
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delAction(obj)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },


    /* 获取数据库列表信息 */
    getList () {
      let params = this.searchForm
      this.$api.task.list(params)
        .then(res => {
          this.tableData.data = res.data
          this.tableData.total = res.totalNumber
        })
    },
  }
}
