export default {
  name: 'EditDialog',
  pageInfo: {
    defaultForm: {},
    title: '',
    updateApi: null,
    addApi: null,
  },
  data() {
    return {
      saving: false,
      dialogVisible: false,
      form: { ...this.$options.pageInfo.defaultForm },
      rules: {},
      title: ''
    }
  },
  computed: {
    mixin_isEdit() {
      return !!this.form.id;
    },
  },
  methods: {
    open(data = {}) {
      this.dialogVisible = true;
      this.form = { ...this.$options.pageInfo.defaultForm, ...data };
      this.title = `${this.mixin_isEdit ? '编辑': '新增'}${this.$options.pageInfo.title}`;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
    },
    closeVisible() {
      this.dialogVisible = false;
    },
    combinform() {
      return this.form;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.saving = true;
          if (this.mixin_isEdit) {
            this.$options.pageInfo.updateApi(this.combinform()).then(res => {
              this.saving = false;
              this.$message.success('编辑成功');
              this.$emit('refresh');
              this.closeVisible();
            }).catch(() => {
              this.saving = false;
            });
          } else {
            this.$options.pageInfo.addApi(this.combinform()).then(res => {
              this.saving = false;
              this.$message.success('新增成功');
              this.$emit('refresh');
              this.closeVisible();
            }).catch(() => {
              this.saving = false;
            });
          }
        } else {
          this.submitValidateErr();
        }
      });
    },
    submitValidateErr() {
    }
  }
}