import { DynamicTextElement } from '../../types/elements';
import { NumericPropertyInput } from '../ui/NumericPropertyInput';
import { ColorPropertyInput } from '../ui/ColorPropertyInput';
import { getEditorFeatureFlags, getPdfBuilderData } from '../../utils/editorFeatures';
type DynamicTextTemplateOption = {
id: string;
label: string;
template: string;
};
// Composant Toggle personnalisé
const Toggle = ({ checked, onChange, label, description }: {
checked: boolean;
onChange: (checked: boolean) => void;
label: string;
description: string;
}) => (
onChange(!checked)}
style={{
position: 'relative',
width: '44px',
height: '24px',
backgroundColor: checked ? '#007bff' : '#ccc',
borderRadius: '12px',
cursor: 'pointer',
transition: 'background-color 0.2s ease',
border: 'none'
}}
>
{description}
);
interface DynamicTextPropertiesProps {
element: DynamicTextElement;
onChange: (elementId: string, property: string, value: unknown) => void;
activeTab: { [key: string]: 'fonctionnalites' | 'personnalisation' | 'positionnement' };
setActiveTab: (tabs: { [key: string]: 'fonctionnalites' | 'personnalisation' | 'positionnement' }) => void;
}
export function DynamicTextProperties({ element, onChange, activeTab, setActiveTab }: DynamicTextPropertiesProps) {
const dynamicCurrentTab = activeTab[element.id] || 'fonctionnalites';
const setDynamicCurrentTab = (tab: 'fonctionnalites' | 'personnalisation' | 'positionnement') => {
setActiveTab({ ...activeTab, [element.id]: tab });
};
const { canUseEditorThemes } = getEditorFeatureFlags();
const freeTextExamples: DynamicTextTemplateOption[] = [
{
id: 'document_title',
label: 'Titre de document',
template: 'CONTRAT DE PRESTATION DE SERVICES'
},
{
id: 'document_subtitle',
label: 'Sous-titre de document',
template: 'Entre les parties ci-dessous désignées'
},
{
id: 'date_today',
label: 'Date du jour',
template: 'Fait à [Ville], le [date]'
}
];
const customTemplate: DynamicTextTemplateOption = {
id: 'custom',
label: 'Personnalisé',
template: ''
};
const pdfBuilderData = getPdfBuilderData();
const premiumTextExamples = ((pdfBuilderData.dynamicTextTemplatesPremium ?? []) as DynamicTextTemplateOption[]);
const allTextExamples = [customTemplate, ...freeTextExamples, ...premiumTextExamples];
// Détecter le template actuel
const currentText = element.text || '';
const currentTemplate = (() => {
if (element.textTemplate && allTextExamples.some((example) => example.id === element.textTemplate)) {
return element.textTemplate;
}
return allTextExamples.find((example) => example.template === currentText)?.id || 'custom';
})();
// Fonction pour changer de template
const handleTemplateChange = (templateId: string) => {
const selectedExample = allTextExamples.find((ex) => ex.id === templateId);
if (selectedExample) {
onChange(element.id, 'text', selectedExample.template);
onChange(element.id, 'textTemplate', selectedExample.id);
}
};
const dynamicTextThemes = [
{
id: 'clean',
name: 'Propre',
preview: (
),
styles: {
backgroundColor: '#ffffff',
borderColor: '#f3f4f6',
textColor: '#374151',
headerTextColor: '#111827'
}
},
{
id: 'subtle',
name: 'Discret',
preview: (
),
styles: {
backgroundColor: '#f9fafb',
borderColor: '#e5e7eb',
textColor: '#6b7280',
headerTextColor: '#374151'
}
},
{
id: 'highlighted',
name: 'Surligné',
preview: (
),
styles: {
backgroundColor: '#eff6ff',
borderColor: '#dbeafe',
textColor: '#1e40af',
headerTextColor: '#1e3a8a'
}
}
];
const availableVariables = [
{ key: '{{customer_name}}', label: 'Nom du client' },
{ key: '{{customer_first_name}}', label: 'Prénom du client' },
{ key: '{{customer_last_name}}', label: 'Nom du client' },
{ key: '{{customer_email}}', label: 'Email du client' },
{ key: '{{customer_phone}}', label: 'Téléphone du client' },
{ key: '{{order_number}}', label: 'Numéro de commande' },
{ key: '{{order_date}}', label: 'Date de commande' },
{ key: '{{order_total}}', label: 'Total de la commande' },
{ key: '{{company_name}}', label: 'Nom de l\'entreprise' },
{ key: '{{company_email}}', label: 'Email de l\'entreprise' },
{ key: '{{company_phone}}', label: 'Téléphone de l\'entreprise' },
{ key: '{{company_address}}', label: 'Adresse de l\'entreprise' },
{ key: '{{current_date}}', label: 'Date actuelle' },
{ key: '{{current_time}}', label: 'Heure actuelle' }
];
return (
<>
{/* Système d'onglets pour Dynamic Text */}
{/* Onglet Fonctionnalités */}
{dynamicCurrentTab === 'fonctionnalites' && (
<>
{availableVariables.map((variable) => (
{
const currentText = element.text || '';
const newText = currentText + variable.key;
onChange(element.id, 'text', newText);
}}
style={{
cursor: 'pointer',
padding: '4px 8px',
marginBottom: '4px',
backgroundColor: '#ffffff',
border: '1px solid #e0e0e0',
borderRadius: '3px',
fontSize: '11px',
fontFamily: 'monospace'
}}
title={`Cliquez pour insérer ${variable.key}`}
>
{variable.key} - {variable.label}
))}
onChange(element.id, 'autoWrap', e.target.checked)}
style={{ marginRight: '8px' }}
/>
Adapte le texte à la largeur
{/* Section Affichage du fond */}
Affichage du fond
onChange(element.id, 'showBackground', checked)}
label="Afficher le fond"
description="Affiche un fond coloré derrière le texte dynamique"
/>
>
)}
{/* Onglet Personnalisation */}
{dynamicCurrentTab === 'personnalisation' && (
<>
{canUseEditorThemes && (
{dynamicTextThemes.map((theme) => (
onChange(element.id, 'theme', theme.id)}
style={{
cursor: 'pointer',
border: element.theme === theme.id ? '2px solid #007bff' : '2px solid transparent',
borderRadius: '6px',
padding: '6px',
backgroundColor: '#ffffff',
transition: 'all 0.2s ease'
}}
title={theme.name}
>
{theme.name}
{theme.preview}
))}
)}
onChange(element.id, 'fontSize', value)}
description="Taille de la police en pixels"
/>
{/* Section Couleur de fond - visible seulement si showBackground est activé */}
{element.showBackground !== false && (
onChange(element.id, 'backgroundColor', value)}
/>
)}
>
)}
{/* Onglet Positionnement */}
{dynamicCurrentTab === 'positionnement' && (
<>
onChange(element.id, 'x', value)}
/>
onChange(element.id, 'y', value)}
/>
onChange(element.id, 'width', value)}
/>
onChange(element.id, 'height', value)}
/>
onChange(element.id, 'padding', value)}
/>
>
)}
>
);
}