const { useState, useEffect } = React; function SurveyModal({ survey, onClose, onSave }) { const empty = { name: '', description: '', mode: 'survey_only', welcome_message: 'Merhaba, size birkaç kısa soru sormak istiyorum.', completion_message: 'Teşekkür ederiz, iyi günler!', abandon_message: 'Görüşmek üzere, iyi günler.', escalation_threshold: '', escalation_dest: '9999', questions: [] }; const [form, setForm] = useState(survey ? { ...survey, escalation_threshold: survey.escalation_threshold ?? '', questions: [] } : empty); const [questions, setQuestions] = useState([]); const [loading, setLoading] = useState(!!survey); useEffect(() => { if (survey) { authFetch(`/api/surveys/${survey.id}`).then(r => r.json()).then(d => { setForm({ ...d, escalation_threshold: d.escalation_threshold ?? '' }); setQuestions(d.questions.map(q => ({ ...q, branching_rules: JSON.stringify(q.branching_rules || []), options: (q.options || []).join(', ') }))); setLoading(false); }); } }, []); const addQuestion = () => setQuestions(qs => [...qs, { order: qs.length + 1, text: '', question_type: 'open', options: '', scale_min: 1, scale_max: 10, retry_text: 'Üzgünüm, anlayamadım. Lütfen tekrar söyleyin.', max_retries: 2, branching_rules: '[]' }]); const updateQ = (i, field, val) => setQuestions(qs => qs.map((q, idx) => idx === i ? { ...q, [field]: val } : q)); const removeQ = (i) => setQuestions(qs => qs.filter((_, idx) => idx !== i).map((q, idx) => ({ ...q, order: idx + 1 }))); const handleSave = async () => { const payload = { ...form, escalation_threshold: form.escalation_threshold !== '' ? parseFloat(form.escalation_threshold) : null, questions: questions.map((q, i) => ({ ...q, order: i + 1, options: q.options ? q.options.split(',').map(s => s.trim()).filter(Boolean) : [], branching_rules: (() => { try { return JSON.parse(q.branching_rules || '[]'); } catch { return []; } })() })) }; const url = survey ? `/api/surveys/${survey.id}` : '/api/surveys'; const method = survey ? 'PUT' : 'POST'; const r = await authFetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (r.ok) onSave(); else { const e = await r.json(); alert(JSON.stringify(e)); } }; const qTypeLabel = { open: 'Açık Uçlu', yesno: 'Evet/Hayır', scale: 'Puanlama', choice: 'Çoktan Seçmeli' }; return (
e.stopPropagation()} style={{ width: '850px', maxWidth: '95vw', padding: 0, margin: 'auto' }}>
{survey ? 'Anketi Düzenle' : 'Yeni Anket Oluştur'}
{loading ?
Yükleniyor...
:
{/* Temel Bilgiler */}
setForm(f => ({ ...f, name: e.target.value }))} placeholder="Müşteri Memnuniyet Anketi" />
setForm(f => ({ ...f, description: e.target.value }))} placeholder="Kısa açıklama" />
setForm(f => ({ ...f, escalation_threshold: e.target.value }))} placeholder="örn: 6 (boş = kapalı)" />
setForm(f => ({ ...f, escalation_dest: e.target.value }))} placeholder="9999" />