<template>
  <BasicModal v-bind="$attrs" @register="registerModal" title="新建文件夹" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { BasicForm, useForm } from '@/components/Form';
  import { addFolderSchema } from '../modal.data';

  import { orgPosGroup } from '@/api/demo/system';
  import { useMessage } from '@/hooks/web/useMessage';
  const { createMessage } = useMessage();

  const emit = defineEmits(['success']);

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 100,
    baseColProps: { span: 24 },
    schemas: addFolderSchema,
    showActionButtonGroup: false,
  });

  // 上级文件夹的id
  let folderId = ref();
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    folderId.value = data.record?.id;
    setFieldsValue({
      ...data.record,
    });
  });

  // 提交
  async function handleSubmit() {
    try {
      const values = await validate();
      let query = {
        folderId: folderId.value,
        newName: values.newName,
      };
      // 调用接口
      const data = await orgPosGroup(query);
      if (data) {
        setModalProps({ confirmLoading: true });
        closeModal();
        emit('success');
        return createMessage.success('成功');
      } else {
        return createMessage.error('失败');
      }
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
