diff --git a/app/components/app/person/family-parents-form.vue b/app/components/app/person/family-parents-form.vue index 04bebecc..8c41a3c8 100644 --- a/app/components/app/person/family-parents-form.vue +++ b/app/components/app/person/family-parents-form.vue @@ -4,6 +4,7 @@ import { toTypedSchema } from '@vee-validate/zod' import { FieldArray } from 'vee-validate' // component +import * as DE from '~/components/pub/my-ui/doc-entry' import { Form } from '~/components/pub/ui/form' import { SelectEducation } from '~/components/app/patient/fields' import { InputBase } from '~/components/pub/my-ui/form' @@ -17,33 +18,45 @@ const props = defineProps<{ const formSchema = toTypedSchema(props.schema) const formRef = ref() +const isFamilyFormDisabled = ref(true) -// Watcher untuk mengatur families ketika shareFamilyData berubah -watch( - () => formRef.value?.values?.shareFamilyData, - (newValue) => { - if (newValue === '1' && formRef.value) { - // Ketika memilih "Ya", pastikan ada data families untuk mother dan father - const currentFamilies = formRef.value.values?.families || [] - if (currentFamilies.length === 0) { - formRef.value.setFieldValue('families', [ - { relation: 'mother', name: undefined, education: undefined, occupation: undefined }, - { relation: 'father', name: undefined, education: undefined, occupation: undefined }, - ]) - } - } else if (newValue === '0' && formRef.value) { - // Ketika memilih "Tidak", kosongkan families - formRef.value.setFieldValue('families', []) - } - }, - { immediate: false }, -) +const isEditing = computed(() => !!props.initialValues?.id) defineExpose({ validate: () => formRef.value?.validate(), resetForm: () => formRef.value?.resetForm(), values: computed(() => formRef.value?.values), }) + +watch( + () => formRef.value?.values?.shareFamilyData, + (newValue) => { + if (!formRef.value) return + + const currentFamilies = formRef.value.values?.families || [] + + if (newValue === '1') { + isFamilyFormDisabled.value = false + + // CREATE MODE — Auto default + if (!isEditing.value && currentFamilies.length === 0) { + formRef.value.setFieldValue('families', [ + { relation: 'mother', name: '', education: '', occupation: '' }, + { relation: 'father', name: '', education: '', occupation: '' }, + ]) + } + + return + } + + isFamilyFormDisabled.value = true + + if (!isEditing.value) { + formRef.value.setFieldValue('families', []) + } + }, + { immediate: true }, +)