'use client' import { useState, useEffect } from 'react' import { flashcardsService, type CreateFlashcardRequest, type CardSet } from '@/lib/services/flashcards' interface FlashcardFormProps { cardSets: CardSet[] initialData?: Partial isEdit?: boolean onSuccess: () => void onCancel: () => void } export function FlashcardForm({ cardSets, initialData, isEdit = false, onSuccess, onCancel }: FlashcardFormProps) { const [formData, setFormData] = useState({ cardSetId: initialData?.cardSetId || (cardSets[0]?.id || ''), english: initialData?.english || '', chinese: initialData?.chinese || '', pronunciation: initialData?.pronunciation || '', partOfSpeech: initialData?.partOfSpeech || '名詞', example: initialData?.example || '', }) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const partOfSpeechOptions = [ '名詞', '動詞', '形容詞', '副詞', '介詞', '連詞', '感嘆詞', '代詞', '冠詞' ] const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError(null) try { let result if (isEdit && initialData?.id) { result = await flashcardsService.updateFlashcard(initialData.id, formData) } else { result = await flashcardsService.createFlashcard(formData) } if (result.success) { onSuccess() } else { setError(result.error || '操作失敗') } } catch (err) { setError('操作失敗,請重試') } finally { setLoading(false) } } const handleChange = (field: keyof CreateFlashcardRequest, value: string) => { setFormData(prev => ({ ...prev, [field]: value })) } return (

{isEdit ? '編輯詞卡' : '新增詞卡'}

{error && (

{error}

)}
{/* 詞卡集合選擇 */}
{/* 英文單字 */}
handleChange('english', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="例如:negotiate" required />
{/* 中文翻譯 */}
handleChange('chinese', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="例如:談判,協商" required />
{/* 詞性和發音 */}
handleChange('pronunciation', e.target.value)} className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary focus:border-transparent" placeholder="例如:/nɪˈɡoʊʃieɪt/" />
{/* 例句 */}