<script setup lang="ts">
import { useVbenForm, useVbenModal } from '@vben/common-ui';
import { FormSchema } from './schema';
import { useMessage } from 'naive-ui';
import { useRouter } from 'vue-router';
const message = useMessage();
const router = useRouter();
const [Modal, modalApi] = useVbenModal({
  fullscreenButton: false,
  title: '创建知识库',
  onOpenChange(isOpen) {
    if (!isOpen) {
      formApi.resetForm();
    }
  },
  async onConfirm() {
    try {
      const valid = await formApi.validate();
      if (!valid.valid) {
        message.error('请完善表单信息');
        return;
      }
      handleSubmit(formApi.getValues());
    } catch (error) {
      message.error('表单验证失败');
    }
    modalApi.close();
  },
});
const handleSubmit = async (values: any) => {
  console.log('提交表单', values);
  try {
    // 这里可以调用接口创建知识库
    // await api.createDataset(values);
    message.success('知识库创建成功');
    router.push({
      name: 'Documents',
      params: { id: '13213212121' },
    });
  } catch (error) {
    message.error('知识库创建失败');
  }
};
const [Form, formApi] = useVbenForm({
  handleSubmit: handleSubmit,
  ...FormSchema,
});
</script>
<template>
  <Modal> <Form /> </Modal>
</template>
<style lang="less" scoped></style>
