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