<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
  >
    <div>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="名称">
          <el-input v-model="formData.triggerTitle" placeholder="输入Trigger名称" />
        </el-form-item>

        <el-form-item label="Tigger Code">
          <el-input v-model="formData.triggerCode" placeholder="Code" />
        </el-form-item>

        <el-form-item label="Group Code">
          <el-input v-model="formData.triggerGroupCode" placeholder="Group Code" />
        </el-form-item>

        <el-form-item label="Cron">
          <el-input v-model="formData.triggerCron" placeholder="Cron" />
        </el-form-item>

        <el-form-item label="说明">
          <el-input v-model="formData.triggerDesc" placeholder="输入说明" />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import { getJobTriggerById, createJobTrigger, updateJobTrigger } from '@/api/system/jobTriggerApi'

export default {
  props: {
    title: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      dialogVisible: false,
      actionType: '',
      formData: {
        triggerId: '',
        triggerTitle: '',
        triggerCode: '',
        triggerGroupCode: '',
        triggerCron: '',
        triggerDesc: ''
      },
      rules: {
        triggerTitle: [
          { required: true, message: '请填写Job名称', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    formSuccess() {
      this.$emit('formSuccess')
    },

    // Show Add Model
    showAddModel() {
      this.actionType = ACTION_ADD
      this.resetFormData()
      this.dialogVisible = true
    },

    // Show Edit Model
    showEditModel(triggerId) {
      this.actionType = ACTION_UPDATE
      this.resetFormData()
      this.dialogVisible = true

      getJobTriggerById(triggerId).then((resp) => {
        const { data } = resp
        this.formData = data
      }).catch((error) => {
        console.log(error)
      })
    },

    // Reset Form Data
    resetFormData() {
      this.formData = {
        triggerId: '',
        triggerTitle: '',
        triggerCode: '',
        triggerGroupCode: '',
        triggerCron: '',
        triggerDesc: ''
      }
    },

    // Submit Form
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleActionCommand()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // Handle ACTION Command
    handleActionCommand() {
      const actionType = this.actionType

      switch (actionType) {
        // Action: ADD
        case ACTION_ADD:
          createJobTrigger(this.formData).then((resp) => {
            const { msg } = resp
            this.dialogVisible = false
            this.formSuccess()
            this.$message.success(msg)
          }).catch((error) => {
            console.log(error)
          })
          break

        // Action: UPDATE
        case ACTION_UPDATE:
          updateJobTrigger(this.formData).then((resp) => {
            const { msg } = resp
            this.dialogVisible = false
            this.formSuccess()
            this.$message.success(msg)
          }).catch((error) => {
            console.log(error)
          })
          break
      }
    }
  }
}
</script>

<style lang="scss">

</style>
<style lang="scss" scoped>

</style>
