// Schnelltexte tab — quick copy-to-clipboard text templates for WhatsApp/Kleinanzeigen. // Three panes: template list (with add/edit/delete) · rental picker · live preview with Kopieren. function DocsSchnelltexte({ rentals, equipment, company, docs, setDocs, initialRentalId }) { const t = useTheme(); const toast = useToast(); const [templates, setTemplates] = useLocal('rf-d-qtext-tpls-v1', QUICK_TEXT_TEMPLATES); const [selTplId, setSelTplId] = useState(templates[0] && templates[0].id); const [selRentalId, setSelRentalId] = useState(initialRentalId || (rentals[0] && rentals[0].id)); const [editingTpl, setEditingTpl] = useState(false); const [tplSnapshot, setTplSnapshot] = useState(null); // Snapshot taken when editor opens, used for „Verwerfen“ const [confirmDel, setConfirmDel] = useState(null); useEffect(() => { if (initialRentalId) setSelRentalId(initialRentalId); }, [initialRentalId]); const tpl = templates.find((x) => x.id === selTplId) || templates[0]; const r = rentals.find((x) => x.id === selRentalId); const eq = r && equipment.find((e) => e.id === r.equipmentId); const filled = useMemo(() => tpl && r ? fillQuickText(tpl.body, r, eq, company) : '', [tpl && tpl.id, tpl && tpl.body, r && r.id, eq && eq.id, company]); const [editedBody, setEditedBody] = useState(filled); useEffect(() => { setEditedBody(filled); }, [filled]); const copy = () => { if (!editedBody) return; try { navigator.clipboard.writeText(editedBody); toast('In die Zwischenablage kopiert'); } catch (err) { toast('Kopieren fehlgeschlagen'); } }; const saveToArchive = () => { if (!r || !tpl) return; const doc = { id: 'd-' + Date.now(), kind: tpl.kind || 'custom', format: 'text', title: tpl.name, rentalId: r.id, templateId: tpl.id, body: editedBody, createdISO: todayISO(), }; setDocs((prev) => [doc, ...prev]); toast('Im Archiv gespeichert'); }; const patchTpl = (patch) => setTemplates((prev) => prev.map((x) => x.id === tpl.id ? { ...x, ...patch } : x)); // Open editor: snapshot current template so we can restore on „Verwerfen“ const openEditor = () => { if (tpl) { setTplSnapshot({ ...tpl }); setEditingTpl(true); } }; const closeEditor = () => { setTplSnapshot(null); setEditingTpl(false); }; const discardTplEdits = () => { if (!tplSnapshot) return; setTemplates((prev) => prev.map((x) => x.id === tplSnapshot.id ? { ...tplSnapshot } : x)); toast('Änderungen verworfen'); }; const tplIsDirty = !!(tplSnapshot && tpl && ( tpl.name !== tplSnapshot.name || tpl.body !== tplSnapshot.body || tpl.icon !== tplSnapshot.icon || tpl.color !== tplSnapshot.color || tpl.desc !== tplSnapshot.desc )); // Built-in templates can be reset to their original definition const builtIn = tpl && QUICK_TEXT_TEMPLATES.find((x) => x.id === tpl.id); const tplDiffersFromBuiltIn = !!(builtIn && ( builtIn.name !== tpl.name || builtIn.body !== tpl.body || builtIn.icon !== tpl.icon || builtIn.color !== tpl.color || builtIn.desc !== tpl.desc )); const resetToBuiltIn = () => { if (!builtIn) return; setTemplates((prev) => prev.map((x) => x.id === builtIn.id ? { ...builtIn } : x)); toast('Auf Standard zurückgesetzt'); }; const addTpl = () => { const id = 'qt-' + Date.now(); const fresh = { id, name: 'Neue Vorlage', icon: '📝', color: '#5856D6', kind: 'custom', desc: 'Eigene Schnellnachricht', body: 'Hallo {{name}},\n\n…\n\nViele Grüße\n{{firma}}' }; setTemplates((prev) => [...prev, fresh]); setSelTplId(id); setEditingTpl(true); }; const delTpl = () => { setTemplates((prev) => prev.filter((x) => x.id !== tpl.id)); setSelTplId((templates.find((x) => x.id !== tpl.id) || templates[0] || { id: null }).id); setConfirmDel(null); setEditingTpl(false); toast('Vorlage gelöscht'); }; return (