'use client' import { useState } from 'react' import { useRouter } from 'next/navigation' import { createOrUpdateBlogPostAction } from '@/app/actions' import { ArrowLeft, Save, Eye, Edit3 } from 'lucide-react' import { Link } from '@/i18n/routing' interface Option { value: string label: string } interface FormProps { post: any | null listingOptions: Option[] } // Simple local markdown preview renderer helper function previewMarkdown(md: string): string { if (!md) return '' let html = md .replace(/&/g, '&') .replace(//g, '>') html = html.replace(/\*\*(.*?)\*\*/g, '$1') html = html.replace(/\*(.*?)\*/g, '$1') html = html.replace(/^### (.*?)$/gm, '

$1

') html = html.replace(/^## (.*?)$/gm, '

$1

') html = html.replace(/^# (.*?)$/gm, '

$1

') html = html.replace(/^\* (.*?)$/gm, '
  • $1
  • ') html = html.replace(/^- (.*?)$/gm, '
  • $1
  • ') html = html.replace(/\[(.*?)\]\((.*?)\)/g, '$1') const paragraphs = html.split(/\n\n+/) return paragraphs.map(p => { const t = p.trim() if (!t) return '' if (t.startsWith('${t.replace(/\n/g, '
    ')}

    ` }).join('\n') } export default function BlogForm({ post, listingOptions }: FormProps) { const router = useRouter() const [activeTab, setActiveTab] = useState<'tr' | 'en' | 'ru'>('tr') const [editorMode, setEditorMode] = useState<'edit' | 'preview'>('edit') const [loading, setLoading] = useState(false) const [error, setError] = useState('') const [success, setSuccess] = useState(false) // Markdown content states for live preview const [contentTr, setContentTr] = useState(post?.contentTr || '') const [contentEn, setContentEn] = useState(post?.contentEn || '') const [contentRu, setContentRu] = useState(post?.contentRu || '') // Selected Listings const [selectedListings, setSelectedListings] = useState(post?.relatedListingIds || []) const [listingSearch, setListingSearch] = useState('') const handleListingToggle = (id: string) => { setSelectedListings(prev => prev.includes(id) ? prev.filter(i => i !== id) : [...prev, id] ) } const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError('') setSuccess(false) const formData = new FormData(e.currentTarget) if (post) { formData.append('id', post.id) } formData.append('relatedListingIds', selectedListings.join(',')) try { const res = await createOrUpdateBlogPostAction(formData) if (res.error) { setError(res.error) } else { setSuccess(true) setTimeout(() => { router.push('/admin/blog') router.refresh() }, 1500) } } catch (err) { setError('İşlem sırasında bir hata oluştu.') } finally { setLoading(false) } } const filteredListings = listingOptions.filter(opt => opt.label.toLowerCase().includes(listingSearch.toLowerCase()) ) const activeContent = activeTab === 'tr' ? contentTr : activeTab === 'en' ? contentEn : contentRu return (
    {error && (
    {error}
    )} {success && (
    Yazı başarıyla kaydedildi! Yönlendiriliyorsunuz...
    )} {/* Language tabs */}
    {(['tr', 'en', 'ru'] as const).map((lang) => { const label = lang === 'tr' ? '🇹🇷 Türkçe' : lang === 'en' ? '🇬🇧 English' : '🇷🇺 Русский' const isTabActive = activeTab === lang return ( ) })}
    {/* Edit / Preview controls */}
    {/* Localized inputs */}
    {activeTab === 'tr' && (
    {editorMode === 'edit' ? (