<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '编辑'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="handleConfirm()" label-width="100px">
      <el-form-item label="bean名称" prop="beanName">
        <el-input v-model="dataForm.beanName" placeholder="spring bean名称, 如: testTask"></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input v-model="dataForm.params" placeholder="参数"></el-input>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input v-model="dataForm.cronExpression" placeholder="如: 0 0 12 * * ?"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { apiSysScheduleInfo, apiSysScheduleConfirm } from '@/api/api_sys'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          beanName: '',
          params: '',
          cronExpression: '',
          remark: '',
          status: 0
        },
        dataRule: {
          beanName: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
          cronExpression: [{ required: true, message: 'cron表达式不能为空', trigger: 'blur' }]
        },
        apiType: '' // api类型
      }
    },
    methods: {
      init (id) {
        this.visible = true
        if (id) {
          this.dataForm.id = id || 0
          this.handleEdit()
          this.apiType = '/update'
        } else {
          this.apiType = '/save'
        }
      },
      // 编辑
      handleEdit () {
        apiSysScheduleInfo({}, `/${this.dataForm.id}`).then(res => {
          if (res && res.code === 0) {
            this.dataForm.beanName = res.schedule.beanName
            this.dataForm.params = res.schedule.params
            this.dataForm.cronExpression = res.schedule.cronExpression
            this.dataForm.remark = res.schedule.remark
            this.dataForm.status = res.schedule.status
          }
        })
      },
      // 表单提交
      handleConfirm () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let params = {
              'jobId': this.dataForm.id || undefined,
              'beanName': this.dataForm.beanName,
              'params': this.dataForm.params,
              'cronExpression': this.dataForm.cronExpression,
              'remark': this.dataForm.remark,
              'status': !this.dataForm.id ? undefined : this.dataForm.status
            }
            apiSysScheduleConfirm(params, this.apiType).then(res => {
              if (res && res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
