2026-06-26 20b96473f2520590a0dca6b775b81e3ea06a77a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<script lang="ts" setup>
import type { InfraStudentApi } from '#/api/infra/demo';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createStudent, getStudent, updateStudent } from '#/api/infra/demo';
import { $t } from '#/locales';
  import StudentContactForm from './student-contact-form.vue'
  import StudentTeacherForm from './student-teacher-form.vue'
 
import { useFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<InfraStudentApi.Student>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['学生'])
    : $t('ui.actionTitle.create', ['学生']);
});
 
  /** 子表的表单 */
  const subTabsName = ref('studentContact')
      const studentContactFormRef = ref<InstanceType<typeof StudentContactForm>>()
      const studentTeacherFormRef = ref<InstanceType<typeof StudentTeacherForm>>()
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
        // 校验子表单
                         const studentTeacherValid = await studentTeacherFormRef.value?.validate();
            if (!studentTeacherValid) {
              subTabsName.value = 'studentTeacher';
              return;
            }
    modalApi.lock();
    // 提交表单
    const data = (await formApi.getValues()) as InfraStudentApi.Student;
        // 拼接子表的数据
            data.studentContacts = studentContactFormRef.value?.getData();
            data.studentTeacher = await studentTeacherFormRef.value?.getValues();
    try {
      await (formData.value?.id ? updateStudent(data) : createStudent(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    const data = modalApi.getData<InfraStudentApi.Student>();
    if (!data || !data.id) {
      return;
    }
    modalApi.lock();
    try {
      formData.value = await getStudent(data.id);
      // 设置到 values
      await formApi.setValues(formData.value);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="getTitle">
    <Form class="mx-4" />
      <!-- 子表的表单 -->
      <Tabs v-model:active-key="subTabsName">
          <Tabs.TabPane key="studentContact" tab="学生联系人" force-render>
            <StudentContactForm ref="studentContactFormRef" :student-id="formData?.id" />
          </Tabs.TabPane>
          <Tabs.TabPane key="studentTeacher" tab="学生班主任" force-render>
            <StudentTeacherForm ref="studentTeacherFormRef" :student-id="formData?.id" />
          </Tabs.TabPane>
      </Tabs>
  </Modal>
</template>