<template>
  <a-modal
    :confirmLoading="confirmLoading"
    :visible="visible"
    @close="onCancel"
    class="yo-modal-form"
    title="编辑XX"
  >
    <FormBody ref="form-body" />
  </a-modal>
</template>
<script>
import FormBody from './form';

export default {
  components: {
    FormBody,
  },
  data() {
    return {
      visible: false,
      confirmLoading: false,
    };
  },

  computed: {
    formBody() {
      return this.$refs['form-body'];
    },
  },

  methods: {
    /**
     * 必要的方法
     * 从外部调用打开本窗口,并填充外部传入的数据
     */
    onOpen(record) {
      this.visible = true;
      this.$nextTick(async () => {
        await this.formBody.onInit();
        this.formBody.onFillData(record);
      });
    },

    /**
     * 必要的方法
     * 点击保存时的操作
     */
    onOk() {
      this.formBody.onGetData().then((data) => {
        this.confirmLoading = true;
        this.$api
          /** !!此处必须修改调用的接口方法 */
          .testEditApi(data)
          .then(({ success }) => {
            if (success) {
              this.$message.success('编辑成功');
              this.onCancel();
              this.$emit('ok');
            }
          })
          .finally(() => {
            this.confirmLoading = false;
          });
      });
    },

    /**
     * 必要的方法
     * 关闭窗口时的操作
     */
    onCancel() {
      this.formBody.onResetFields();
      this.visible = false;
    },
  },
};
</script>