"use client"

import { useRouter } from "next/navigation"
import { useFamily } from "@/context/family-context"
import { SiteHeader } from "@/components/site-header"
import { MemberForm } from "@/components/members/member-form"
import { Button } from "@/components/ui/button"
import { ArrowLeft } from "lucide-react"
import type { FamilyMember } from "@/types/family"

export default function NewMemberPage() {
  const { addMember, treeData } = useFamily()
  const router = useRouter()

  const existingMembers = Object.values(treeData.members)

  const handleSave = (newMember: FamilyMember) => {
    addMember({
      ...newMember,
      createdAt: new Date().toISOString(),
      updatedAt: new Date().toISOString(),
    })
    router.push(`/members/${newMember.id}`)
  }

  return (
    <div className="min-h-screen flex flex-col bg-background">
      <SiteHeader />
      <div className="container py-8">
        <div className="mb-6 flex items-center gap-2">
          <Button variant="ghost" onClick={() => router.back()}>
            <ArrowLeft className="mr-2 h-4 w-4" /> 返回
          </Button>
          <h1 className="text-2xl font-bold font-serif">新增家族成员</h1>
        </div>
        <MemberForm existingMembers={existingMembers} onSubmit={handleSave} onCancel={() => router.back()} />
      </div>
    </div>
  )
}
