<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="定时器名称" prop="jobName">
        <el-input v-model="formData.jobName" placeholder="请输入定时器名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="spring bean 名称" prop="beanName">
        <el-input v-model="formData.beanName" placeholder="请输入 spring bean 名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="方法名" prop="methodName">
        <el-input v-model="formData.methodName" placeholder="请输入方法名" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input v-model="formData.params" placeholder="请输入参数" />
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input v-model="formData.cronExpression" placeholder="请输入cron表达式" />
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="formData.status" size="mini" placeholder="请选择任务状态">
          <el-option label="正常" :value="0" />
          <el-option label="暂停" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType">
        <el-input v-model="formData.taskType" placeholder="请输入任务类型" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" maxlength="520" :rows="3" show-word-limit />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { saveQuartzJob, updateQuartzJob, getIdQuartzJob } from '@/api/timedTasksManagement/timedTasksList'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        jobId: '',
        jobName: '',
        beanName: '',
        methodName: '',
        params: '',
        cronExpression: '',
        status: '',
        taskType: '',
        remark: '',
        createTime: ''
      },
      formRules: {
        jobName: [
          { required: true, message: '请输入定时器名称' }
        ],
        beanName: [
          { required: true, message: '请输入 spring bean 名称' }
        ],
        methodName: [
          { required: true, message: '请输入方法名' }
        ],
        params: [
          { required: false, message: '请输入参数' }
        ],
        cronExpression: [
          { required: false, message: '请输入cron表达式' }
        ],
        status: [
          { required: true, message: '请选择任务状态' }
        ],
        taskType: [
          { required: false, message: '请输入任务类型' }
        ],
        remark: [
          { required: false, message: '请输入备注' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.jobId ? '编辑定时任务' : '添加定时任务'
      this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      if (params.jobId) {
        this.getInfo(params.jobId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getIdQuartzJob({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          jobId: res.data.jobId || '',
          jobName: res.data.jobName || '',
          beanName: res.data.beanName || '',
          methodName: res.data.methodName || '',
          params: res.data.params || '',
          cronExpression: res.data.cronExpression || '',
          status: res.data.status,
          taskType: res.data.taskType || '',
          remark: res.data.remark || '',
          createTime: res.data.createTime || ''
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            this.formData.jobId ? await updateQuartzJob(params) : await saveQuartzJob(params)
            loading.close()
            this.$message({ message: `${this.formData.jobId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

