"use client" import type React from "react" import { useState } from "react" import type { FamilyMember } from "@/types/family" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { CalendarIcon, User, Scroll, Users, Images, BookOpen } from "lucide-react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { ImageUpload } from "@/components/ui/image-upload" import { PhotoGallery } from "./photo-gallery" import { StoryManager } from "./story-manager" interface MemberFormProps { initialData?: Partial existingMembers?: FamilyMember[] onSubmit: (data: FamilyMember) => void onCancel: () => void } export function MemberForm({ initialData, existingMembers = [], onSubmit, onCancel }: MemberFormProps) { // Initialize state with default values or initialData const [formData, setFormData] = useState>({ id: initialData?.id || crypto.randomUUID(), surname: initialData?.surname || "李", givenName: initialData?.givenName || "", fullName: initialData?.fullName || "", gender: initialData?.gender || "male", generation: initialData?.generation || 1, spouseIds: initialData?.spouseIds || [], childrenIds: initialData?.childrenIds || [], fatherId: initialData?.fatherId, motherId: initialData?.motherId, ...initialData, }) const [errors, setErrors] = useState([]) const handleChange = (field: keyof FamilyMember, value: any) => { setFormData((prev) => { const newData = { ...prev, [field]: value } // Auto-update full name if surname or given name changes if (field === "surname" || field === "givenName") { newData.fullName = (newData.surname || "") + (newData.givenName || "") } return newData }) // Clear errors when user makes changes if (errors.length > 0) setErrors([]) } const handleSpouseChange = (spouseId: string) => { // If selecting "none" (empty string), clear spouses if (!spouseId) { setFormData((prev) => ({ ...prev, spouseIds: [] })) return } // Otherwise set as single spouse (logic can be expanded for multiple later) setFormData((prev) => ({ ...prev, spouseIds: [spouseId] })) } const validate = (): boolean => { const newErrors: string[] = [] // 1. Date Validation if (formData.birthDate && formData.deathDate) { if (new Date(formData.birthDate) > new Date(formData.deathDate)) { newErrors.push("出生日期不能晚于逝世日期") } } // 2. Parent Age Validation const birthYear = formData.birthDate ? new Date(formData.birthDate).getFullYear() : null if (birthYear) { if (formData.fatherId) { const father = existingMembers.find((m) => m.id === formData.fatherId) if (father?.birthDate) { const fatherBirthYear = new Date(father.birthDate).getFullYear() if (birthYear - fatherBirthYear < 15) { newErrors.push(`与父亲的年龄差过小 (${birthYear - fatherBirthYear}岁),请确认`) } if (birthYear < fatherBirthYear) { newErrors.push("出生日期不能早于父亲") } } } if (formData.motherId) { const mother = existingMembers.find((m) => m.id === formData.motherId) if (mother?.birthDate) { const motherBirthYear = new Date(mother.birthDate).getFullYear() if (birthYear - motherBirthYear < 15) { newErrors.push(`与母亲的年龄差过小 (${birthYear - motherBirthYear}岁),请确认`) } if (birthYear < motherBirthYear) { newErrors.push("出生日期不能早于母亲") } } } } // 3. Generation Validation if (formData.fatherId) { const father = existingMembers.find((m) => m.id === formData.fatherId) if (father && formData.generation !== father.generation + 1) { newErrors.push(`世系错误:父亲是第 ${father.generation} 世,子女应为第 ${father.generation + 1} 世`) } } // 4. Circular Reference Detection (循环引用检测) if (formData.fatherId || formData.motherId) { const visited = new Set() const checkCircular = (memberId: string): boolean => { if (memberId === formData.id) return true if (visited.has(memberId)) return false visited.add(memberId) const member = existingMembers.find((m) => m.id === memberId) if (!member) return false if (member.fatherId && checkCircular(member.fatherId)) return true if (member.motherId && checkCircular(member.motherId)) return true return false } if (formData.fatherId && checkCircular(formData.fatherId)) { newErrors.push("检测到循环引用:不能将自己的后代设置为父亲") } if (formData.motherId && checkCircular(formData.motherId)) { newErrors.push("检测到循环引用:不能将自己的后代设置为母亲") } } // 5. Self-Reference Detection (自我引用检测) if (formData.fatherId === formData.id) { newErrors.push("不能将自己设置为自己的父亲") } if (formData.motherId === formData.id) { newErrors.push("不能将自己设置为自己的母亲") } if (formData.spouseIds?.includes(formData.id || "")) { newErrors.push("不能将自己设置为自己的配偶") } setErrors(newErrors) return newErrors.length === 0 } const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (validate()) { onSubmit(formData as FamilyMember) } } const potentialRelatives = existingMembers.filter((m) => m.id !== formData.id) return (
{errors.length > 0 && (

请检查以下问题:

    {errors.map((err, index) => (
  • {err}
  • ))}
)}
{/* Basic Identity */} 基本信息 (Identity)
handleChange("avatarImageId", imageId)} />
handleChange("surname", e.target.value)} />
handleChange("givenName", e.target.value)} />
handleChange("generation", Number.parseInt(e.target.value))} />
{/* Traditional Names */} 传统称谓 (Traditional Names)
handleChange("courtesyName", e.target.value)} placeholder="e.g. 伯虎" />
handleChange("artName", e.target.value)} placeholder="e.g. 六如居士" />
handleChange("generationName", e.target.value)} />
handleChange("posthumousName", e.target.value)} />
{/* Dates & Places */} 生平与地点 (Life & Places)
handleChange("birthDate", e.target.value)} />
handleChange("deathDate", e.target.value)} />
handleChange("ancestralHome", e.target.value)} placeholder="e.g. 福建省泉州市" />
handleChange("burialPlace", e.target.value)} />
{/* Contact Information */} 联系方式 (Contact Information) 在世成员的联系方式
handleChange("phone", e.target.value)} placeholder="e.g. 13800138000" />
handleChange("telephone", e.target.value)} placeholder="e.g. 0592-1234567" />
handleChange("email", e.target.value)} placeholder="e.g. example@email.com" />
handleChange("address", e.target.value)} placeholder="e.g. 福建省厦门市思明区XX路XX号" />
{/* Biography */} 生平事迹 (Biography)