import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { useMutation, useQuery } from '@tanstack/react-query'; import { X, Save, Eye, Code, FileText, Monitor, Smartphone, Plus, AlertTriangle, ChevronDown } from 'lucide-react'; import api from '../api/client'; import { EmailTemplate, EmailTemplateCategory, EmailTemplateVariableGroup } from '../types'; interface EmailTemplateFormProps { template?: EmailTemplate | null; onClose: () => void; onSuccess: () => void; } const EmailTemplateForm: React.FC = ({ template, onClose, onSuccess, }) => { const { t } = useTranslation(); const isEditing = !!template; // Form state const [name, setName] = useState(template?.name || ''); const [description, setDescription] = useState(template?.description || ''); const [subject, setSubject] = useState(template?.subject || ''); const [htmlContent, setHtmlContent] = useState(template?.htmlContent || ''); const [textContent, setTextContent] = useState(template?.textContent || ''); const [category, setCategory] = useState(template?.category || 'OTHER'); // UI state const [activeTab, setActiveTab] = useState<'html' | 'text'>('html'); const [editorMode, setEditorMode] = useState<'visual' | 'code'>('code'); const [previewDevice, setPreviewDevice] = useState<'desktop' | 'mobile'>('desktop'); const [showPreview, setShowPreview] = useState(false); const [showVariables, setShowVariables] = useState(false); // Fetch available variables const { data: variablesData } = useQuery<{ variables: EmailTemplateVariableGroup[] }>({ queryKey: ['email-template-variables'], queryFn: async () => { const { data } = await api.get('/api/email-templates/variables/'); return data; }, }); // Preview mutation const previewMutation = useMutation({ mutationFn: async () => { const { data } = await api.post('/api/email-templates/preview/', { subject, html_content: htmlContent, text_content: textContent, }); return data; }, }); // Create/Update mutation const saveMutation = useMutation({ mutationFn: async () => { const payload = { name, description, subject, html_content: htmlContent, text_content: textContent, category, scope: 'BUSINESS', // Business users only create business templates }; if (isEditing && template) { const { data } = await api.patch(`/api/email-templates/${template.id}/`, payload); return data; } else { const { data } = await api.post('/api/email-templates/', payload); return data; } }, onSuccess: () => { onSuccess(); }, }); const handlePreview = () => { previewMutation.mutate(); setShowPreview(true); }; const insertVariable = (code: string) => { if (activeTab === 'html') { setHtmlContent(prev => prev + code); } else if (activeTab === 'text') { setTextContent(prev => prev + code); } }; const categories: { value: EmailTemplateCategory; label: string }[] = [ { value: 'APPOINTMENT', label: t('emailTemplates.categoryAppointment', 'Appointment') }, { value: 'REMINDER', label: t('emailTemplates.categoryReminder', 'Reminder') }, { value: 'CONFIRMATION', label: t('emailTemplates.categoryConfirmation', 'Confirmation') }, { value: 'MARKETING', label: t('emailTemplates.categoryMarketing', 'Marketing') }, { value: 'NOTIFICATION', label: t('emailTemplates.categoryNotification', 'Notification') }, { value: 'REPORT', label: t('emailTemplates.categoryReport', 'Report') }, { value: 'OTHER', label: t('emailTemplates.categoryOther', 'Other') }, ]; const isValid = name.trim() && subject.trim() && (htmlContent.trim() || textContent.trim()); return (
{/* Modal Header */}

{isEditing ? t('emailTemplates.edit', 'Edit Template') : t('emailTemplates.create', 'Create Template')}

{/* Modal Body */}
{/* Left Column - Form */}
{/* Name */}
setName(e.target.value)} placeholder={t('emailTemplates.namePlaceholder', 'e.g., Appointment Confirmation')} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-brand-500 focus:border-brand-500" />
{/* Category */}
{/* Description */}
setDescription(e.target.value)} placeholder={t('emailTemplates.descriptionPlaceholder', 'Brief description of when this template is used')} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-brand-500 focus:border-brand-500" />
{/* Subject */}
setSubject(e.target.value)} placeholder={t('emailTemplates.subjectPlaceholder', 'e.g., Your appointment is confirmed!')} className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white focus:ring-2 focus:ring-brand-500 focus:border-brand-500" />
{/* Variables Dropdown */}
{showVariables && variablesData?.variables && (
{variablesData.variables.map((group) => (

{group.category}

{group.items.map((variable) => ( ))}
))}
)}
{/* Content Tabs */}
{/* Editor Mode Toggle (for HTML only) */} {activeTab === 'html' && (
)}
{/* Content Editor */} {activeTab === 'html' && (