"use client" import type React from "react" import { useState, useEffect } from "react" import type { Dance } from "@/lib/api/types" import { useToast } from "@/hooks/use-toast" import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" 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 { Loader2, Trash2 } from "lucide-react" import { FileUpload } from "@/components/ui/file-upload" interface AddDanceDialogProps { open: boolean onOpenChange: (open: boolean) => void onDanceAdded: (dance: Dance) => void editDance?: Dance } export function AddDanceDialog({ open, onOpenChange, onDanceAdded, editDance }: AddDanceDialogProps) { const { toast } = useToast() const [isSubmitting, setIsSubmitting] = useState(false) // 表单状态 const [formData, setFormData] = useState>({ name: "", choreographer: "", duration: "", difficulty: "中等", description: "", category: "流行", tags: [], motionFile: "", videoUrl: "", coverUrl: "", }) // 预览ID const [previewId, setPreviewId] = useState( "DNC" + Math.floor(Math.random() * 1000) .toString() .padStart(3, "0"), ) // 当编辑模式且有舞蹈数据时,初始化表单 useEffect(() => { if (editDance) { setFormData({ name: editDance.name || "", choreographer: editDance.choreographer || "", duration: editDance.duration || "", difficulty: editDance.difficulty || "中等", description: editDance.description || "", category: editDance.category || "流行", tags: editDance.tags || [], motionFile: editDance.motionFile || "", videoUrl: editDance.videoUrl || "", coverUrl: editDance.coverUrl || "", }) } else { // 重置表单 setFormData({ name: "", choreographer: "", duration: "", difficulty: "中等", description: "", category: "流行", tags: [], motionFile: "", videoUrl: "", coverUrl: "", }) // 生成新的预览ID setPreviewId( "DNC" + Math.floor(Math.random() * 1000) .toString() .padStart(3, "0"), ) } }, [editDance, open]) const handleChange = (e: React.ChangeEvent) => { const { name, value } = e.target setFormData((prev) => ({ ...prev, [name]: value })) } const handleSelectChange = (name: string, value: string) => { setFormData((prev) => ({ ...prev, [name]: value })) } const handleTagsChange = (e: React.ChangeEvent) => { const tagsString = e.target.value const tagsArray = tagsString .split(",") .map((tag) => tag.trim()) .filter((tag) => tag !== "") setFormData((prev) => ({ ...prev, tags: tagsArray })) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() // 表单验证 if (!formData.name) { toast({ title: "表单不完整", description: "请填写舞蹈名称", variant: "destructive", }) return } setIsSubmitting(true) try { // 模拟API请求延迟 await new Promise((resolve) => setTimeout(resolve, 1000)) const newDance: Dance = { id: editDance?.id || previewId, name: formData.name || "", choreographer: formData.choreographer || "", duration: formData.duration || "", difficulty: formData.difficulty || "中等", description: formData.description || "", category: formData.category || "流行", tags: formData.tags || [], motionFile: formData.motionFile || "", videoUrl: formData.videoUrl || "", coverUrl: formData.coverUrl || editDance?.coverUrl || "/placeholder.svg?height=300&width=400", createdAt: editDance?.createdAt || new Date().toISOString(), updatedAt: new Date().toISOString(), } onDanceAdded(newDance) onOpenChange(false) } catch (error) { toast({ title: "操作失败", description: "添加或更新舞蹈时出现错误,请重试。", variant: "destructive", }) } finally { setIsSubmitting(false) } } return ( {editDance ? "编辑舞蹈" : "添加新舞蹈"} {editDance ? "修改舞蹈信息,完成后点击保存。" : "填写舞蹈信息,完成后点击添加。"}