export default {
  name: 'Mixins_AddDialogForm',
  emits: ['submit'],
  props: {
    show: Boolean,
    editData: Object
    // editMode: {
    //   type: Boolean,
    //   default: false
    // }
  },
  data() {
    return {
      formData: {},
      orgFormData: {},
      rules: {},
      editMode: false
    }
  },
  watch: {
    show: {
      handler(newValue, oldValue) {
        if (this.editData !== null && this.editData !== undefined) {
          this.formData = this.editData
          this.editMode = true
        } else {
          this.formData = this.orgFormData
          this.editMode = false
        }
      },
      immediate: true
    }

    // editData: {
    //   handler(newValue, oldValue) {
    //     if (newValue) {
    //       this.editMode = true
    //       this.formData = newValue
    //     } else {
    //       this.editMode = false
    //     }
    //   },
    //   immediate: true
    // }
  },
  created() {},
  mounted() {},

  methods: {
    onSubmit(e, ref, callback, errorMessage) {
      console.log('[Submit]')

      if (ref === null || ref === undefined) return
      console.log('[Ref]', ref)
      this.$refs[ref].validate((valid) => {
        if (valid) {
          if (callback) callback(valid)
        } else {
          this.$message.error(errorMessage || '填写错误')
          return false
        }
      })
    },

    hideDialog(e, callback) {
      console.log('[HideDialog]')
      this.$emit('update:show', false)
      if (callback) callback()
    },

    resetFormData() {
      if (this.editMode && this.editData !== null && this.editData !== undefined) {
        this.formData = this.editData
      } else {
        this.formData = this.orgFormData
      }
    }

    // setEditData(editData) {
    //   this.editData = editData
    //   this.checkEditData()
    // },

    // checkEditData() {
    //   if (this.editMode) {
    //     this.formData = this.editData
    //     this.editMode = true
    //     return
    //   }

    //   this.editMode = false
    // }
  }
}
