<template>
  <BasicModal v-bind="$attrs" @register="registerTreeModal" destroyOnClose :title="title" :width="600" @ok="handleSubmit">
    <BasicForm @register="registerTreeForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref, nextTick } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../TreeNode.data';
  import { saveOrUpdate } from '../DocFilesDir.api';
  //保存、修改接口
  //   import { saveOrUpdateNode } from '../DocFiles.api';
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  defineProps({
    rootTreeData: { type: Array, default: () => [] },
  });
  // 当前是否是更新模式
  const isUpdate = ref<boolean>(false);
  // 当前是否为添加子级
  let isChild = ref<boolean>(false);
  const model = ref<object>({});
  const title = computed(() => (isUpdate.value ? '修改' : '新增'));
  //表单配置
  const [registerTreeForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    //labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
  });
  //表单赋值
  const [registerTreeModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: true, showOkBtn: true });
    isUpdate.value = !!data?.isUpdate;
    //if (unref(isUpdate)) {
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
    //}

    // let record = unref(data?.record);
    // if (typeof record !== 'object') {
    //   record = {};
    // }
    // // 赋默认值
    // record = Object.assign(record);
    // model.value = record;
    // await setFieldsValue({ ...record });
  });

  //表单提交事件
  async function handleSubmit() {
    try {
      let values = await validate();
      setModalProps({ confirmLoading: true });
      //提交表单
      const node = await saveOrUpdate(values, isUpdate.value);
      await nextTick();
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success', node, isUpdate.value);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
