import { useEffect, useState, useRef } from 'react' import { createEvent, uploadImage } from '../api/api' import './EventForm.css' const he = { createNewEvent: 'צור אירוע חדש', eventNameRequired: 'שם האירוע נדרש', failedCreate: 'נכשל בהוספת אירוע', eventName: 'שם האירוע', eventDate: 'תאריך', location: 'מיקום', invitationImage: 'תמונת הזמנה (רקע)', invitationImageHint: 'העלה תמונה או הדבק קישור לתמונה שתשמש כרקע להזמנה', uploadImage: '📁 העלה תמונה', uploading: 'מעלה...', orPasteUrl: 'או הדבק כתובת URL:', create: 'צור', cancel: 'ביטול', guestFormFields: 'שדות שיוצגו לאורח בדף ה-RSVP', } const GUEST_FIELD_OPTIONS = [ { key: 'mealPref', label: 'העדפת ארוחה' }, { key: 'companions', label: 'כמה תהיו? (מספר מגיעים)' }, ] function EventForm({ onEventCreated, onCancel }) { const [formData, setFormData] = useState({ name: '', date: '', location: '', invitation_image_url: '' }) // Which fields the guest sees on the RSVP page — default: both shown const [guestFields, setGuestFields] = useState(new Set(['mealPref', 'companions'])) const toggleGuestField = (key) => { setGuestFields(prev => { const next = new Set(prev) next.has(key) ? next.delete(key) : next.add(key) return next }) } const [loading, setLoading] = useState(false) const [uploading, setUploading] = useState(false) const [error, setError] = useState('') const fileInputRef = useRef(null) useEffect(() => { const previousOverflow = document.body.style.overflow document.body.style.overflow = 'hidden' return () => { document.body.style.overflow = previousOverflow } }, []) const handleChange = (e) => { const { name, value } = e.target setFormData(prev => ({ ...prev, [name]: value })) } const handleFileChange = async (e) => { const file = e.target.files[0] if (!file) return setUploading(true) setError('') try { const result = await uploadImage(file) setFormData(prev => ({ ...prev, invitation_image_url: result.url })) } catch (err) { setError(err.response?.data?.detail || 'נכשל בהעלאת התמונה') } finally { setUploading(false) } } const handleSubmit = async (e) => { e.preventDefault() if (!formData.name.trim()) { setError(he.eventNameRequired) return } setLoading(true) setError('') try { const payload = { ...formData, guest_form_fields: JSON.stringify([...guestFields]) } const newEvent = await createEvent(payload) setFormData({ name: '', date: '', location: '', invitation_image_url: '' }) setGuestFields(new Set(['mealPref', 'companions'])) onEventCreated(newEvent) } catch (err) { setError(err.response?.data?.detail || he.failedCreate) } finally { setLoading(false) } } return (