<template>
  <a-modal
    :confirmLoading="confirmLoading"
    :visible="visible"
    :width="1024"
    @cancel="onCancel"
    @ok="onOk"
    title="新增应用"
  >
    <FormBody mode="add" 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, id) {
      this.visible = true;
      this.$nextTick(async () => {
        await this.formBody.onInit();

        // 获取外部选中的部门id
        this.formBody.onFillData({
          sysEmpParam: {
            orgId: id,
          },
        });
      });
    },

    /**
     * 必要的方法
     * 点击保存时的操作
     */
    onOk() {
      this.formBody.onValidate((valid) => {
        if (valid) {
          this.confirmLoading = true;
          this.$api
            .sysUserAdd(this.formBody.form)
            .then(({ success }) => {
              if (success) {
                this.$message.success('新增成功');
                this.onCancel();
                this.$emit('ok');
              }
            })
            .finally(() => {
              this.confirmLoading = false;
            });
        }
      });
    },

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