'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { createOrUpdateCollectionAction } from '@/app/actions' import { Link } from '@/i18n/routing' import { ArrowLeft, Save } from 'lucide-react' interface Option { value: string label: string } interface FormProps { collection: any | null allListings: Option[] } export default function CollectionForm({ collection, allListings }: FormProps) { const router = useRouter() const [activeTab, setActiveTab] = useState<'tr' | 'en' | 'ru'>('tr') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState(false) // Track selected listing IDs const [selectedListingIds, setSelectedListingIds] = useState( collection?.listingIds || [] ) const toggleListing = (id: string) => { setSelectedListingIds(prev => prev.includes(id) ? prev.filter(p => p !== id) : [...prev, id] ) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError('') setSuccess(false) const formData = new FormData(e.currentTarget) if (collection) { formData.append('id', collection.id) } // Append array of listing IDs manually since standard FormData doesn't handle arrays easily selectedListingIds.forEach(id => { formData.append('listingIds', id) }) try { const res = await createOrUpdateCollectionAction(formData) if (res.error) { setError(res.error) } else { setSuccess(true) setTimeout(() => { router.push('/admin/collections') router.refresh() }, 1500) } } catch (err) { setError('İşlem sırasında bir hata oluştu.') } finally { setLoading(false) } } return (
{error && (
{error}
)} {success && (
Koleksiyon başarıyla kaydedildi! Yönlendiriliyorsunuz...
)}
{(['tr', 'en', 'ru'] as const).map((lang) => { const label = lang === 'tr' ? '🇹🇷 Türkçe' : lang === 'en' ? '🇬🇧 English' : '🇷🇺 Русский' const isTabActive = activeTab === lang return ( ) })}
{activeTab === 'tr' && (