<template>
  <a-drawer
    :confirmLoading="confirmLoading"
    :visible="visible"
    :width="800"
    @close="onClose"
    class="yo-drawer-form"
    title="编辑应用"
  >
    <FormBody ref="form-body" />
    <div class="ant-drawer-footer">
      <a-button @click="onClose">取消</a-button>
      <a-button :loading="confirmLoading" @click="onOk" type="primary">确定</a-button>
    </div>
  </a-drawer>
</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
          .sysMenuEdit(data)
          .then(({ success }) => {
            if (success) {
              this.$message.success('编辑成功');
              this.onClose();
              this.$emit('ok');
            }
          })
          .finally(() => {
            this.confirmLoading = false;
          });
      });
    },

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