import React, { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { api } from '@/lib/api'; import { SettingsLayout } from '../components/SettingsLayout'; import { SettingsCard } from '../components/SettingsCard'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { __ } from '@/lib/i18n'; import { ArrowLeft, RefreshCw, Upload, Plus, Trash2, Facebook, Twitter, Instagram, Linkedin, Youtube, Globe, MessageCircle, Music, Send, AtSign } from 'lucide-react'; import { toast } from 'sonner'; import { openWPMediaLogo } from '@/lib/wp-media'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; interface SocialLink { platform: string; url: string; } interface EmailSettings { primary_color: string; secondary_color: string; hero_gradient_start: string; hero_gradient_end: string; hero_text_color: string; button_text_color: string; body_bg_color: string; social_icon_color: string; logo_url: string; header_text: string; footer_text: string; social_links: SocialLink[]; } export default function EmailCustomization() { const navigate = useNavigate(); const queryClient = useQueryClient(); // Fetch email settings const { data: settings, isLoading } = useQuery({ queryKey: ['email-settings'], queryFn: () => api.get('/notifications/email-settings'), placeholderData: { primary_color: '#7f54b3', secondary_color: '#7f54b3', hero_gradient_start: '#667eea', hero_gradient_end: '#764ba2', hero_text_color: '#ffffff', button_text_color: '#ffffff', body_bg_color: '#f8f8f8', social_icon_color: 'white', logo_url: '', header_text: '', footer_text: '', social_links: [], }, }); const [formData, setFormData] = useState(settings || { primary_color: '#7f54b3', secondary_color: '#7f54b3', hero_gradient_start: '#667eea', hero_gradient_end: '#764ba2', hero_text_color: '#ffffff', button_text_color: '#ffffff', body_bg_color: '#f8f8f8', social_icon_color: 'white', logo_url: '', header_text: '', footer_text: '', social_links: [], }); // Update form when settings load React.useEffect(() => { if (settings) { setFormData(settings); } }, [settings]); const saveMutation = useMutation({ mutationFn: async () => { return api.post('/notifications/email-settings', formData); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['email-settings'] }); toast.success(__('Email settings saved successfully')); }, onError: (error: any) => { toast.error(error.message || __('Failed to save email settings')); }, }); const resetMutation = useMutation({ mutationFn: async () => { return api.del('/notifications/email-settings'); }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['email-settings'] }); toast.success(__('Email settings reset to defaults')); }, onError: (error: any) => { toast.error(error.message || __('Failed to reset email settings')); }, }); const handleSave = async () => { return new Promise((resolve, reject) => { saveMutation.mutate(undefined, { onSuccess: () => resolve(), onError: () => reject(), }); }); }; const handleReset = () => { if (!confirm(__('Are you sure you want to reset all email customization to defaults?'))) return; resetMutation.mutate(); }; const handleChange = (field: keyof EmailSettings, value: string) => { setFormData(prev => ({ ...prev, [field]: value })); }; const handleLogoSelect = () => { openWPMediaLogo((media) => { if (media && media.url) { handleChange('logo_url', media.url); } }); }; const addSocialLink = () => { setFormData(prev => ({ ...prev, social_links: [...prev.social_links, { platform: 'facebook', url: '' }], })); }; const removeSocialLink = (index: number) => { setFormData(prev => ({ ...prev, social_links: prev.social_links.filter((_, i) => i !== index), })); }; const updateSocialLink = (index: number, field: 'platform' | 'url', value: string) => { setFormData(prev => ({ ...prev, social_links: prev.social_links.map((link, i) => i === index ? { ...link, [field]: value } : link ), })); }; const getSocialIcon = (platform: string) => { const icons: Record = { facebook: Facebook, x: AtSign, instagram: Instagram, linkedin: Linkedin, youtube: Youtube, discord: MessageCircle, spotify: Music, telegram: Send, whatsapp: MessageCircle, threads: AtSign, website: Globe, }; const Icon = icons[platform] || Globe; return ; }; if (isLoading) { return (
); } return ( } >
{/* Brand Colors */}
handleChange('primary_color', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('primary_color', e.target.value)} placeholder="#7f54b3" className="flex-1" />

{__('Used for primary buttons and main accents')}

handleChange('secondary_color', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('secondary_color', e.target.value)} placeholder="#7f54b3" className="flex-1" />

{__('Used for outline buttons and borders')}

{/* Hero Card Gradient */}
handleChange('hero_gradient_start', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('hero_gradient_start', e.target.value)} placeholder="#667eea" className="flex-1" />
handleChange('hero_gradient_end', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('hero_gradient_end', e.target.value)} placeholder="#764ba2" className="flex-1" />
handleChange('hero_text_color', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('hero_text_color', e.target.value)} placeholder="#ffffff" className="flex-1" />

{__('Text and heading color for hero cards (usually white)')}

{/* Preview */}

{__('Preview')}

{__('This is how your hero cards will look')}

{/* Button Styling */}
handleChange('button_text_color', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('button_text_color', e.target.value)} placeholder="#ffffff" className="flex-1" />

{__('Text color for buttons (usually white for dark buttons)')}

{/* Button Preview */}
{/* Email Background & Social Icons */}
handleChange('body_bg_color', e.target.value)} className="w-20 h-10 p-1 cursor-pointer" /> handleChange('body_bg_color', e.target.value)} placeholder="#f8f8f8" className="flex-1" />

{__('Background color for the email body')}

{__('Choose white icons for dark backgrounds, black for light backgrounds')}

{/* Logo & Branding */}
handleChange('logo_url', e.target.value)} placeholder="https://example.com/logo.png" className="flex-1" />

{__('Full URL to your logo image (recommended: 200x60px)')}

{formData.logo_url && (
Logo preview
)}
handleChange('header_text', e.target.value)} placeholder={__('Your Store Name')} />

{__('Text shown in email header (leave empty to use store name)')}

handleChange('footer_text', e.target.value)} placeholder={__('© {current_year} Your Store. All rights reserved.')} />

{__('Text shown in email footer. Use {current_year} for dynamic year.')}

{/* Social Links */}
{formData.social_links.length === 0 ? (

{__('No social links added. Click "Add Social Link" to get started.')}

) : (
{formData.social_links.map((link, index) => (
updateSocialLink(index, 'url', e.target.value)} placeholder="https://..." className="h-9" />
))}
)}

{__('Social links will appear as icons in the email footer')}

{/* Info Box */}

{__('Note:')} {__('These settings will apply to all email templates. Individual templates can still override specific content, but colors and branding will be consistent across all emails.')}

); }