import { useState, useRef, useEffect } from "react"; import { useTranslation } from "react-i18next"; import { BookTemplate, Save, Trash2 } from "lucide-react"; import type { BudgetTemplate } from "../../shared/types"; import { confirmAction } from "../../services/confirmService"; interface TemplateActionsProps { templates: BudgetTemplate[]; onApply: (templateId: number, month: number) => void; onApplyAllMonths: (templateId: number) => void; onSave: (name: string, description?: string) => void; onDelete: (templateId: number) => void; disabled?: boolean; } const MONTH_KEYS = [ "months.jan", "months.feb", "months.mar", "months.apr", "months.may", "months.jun", "months.jul", "months.aug", "months.sep", "months.oct", "months.nov", "months.dec", ] as const; export default function TemplateActions({ templates, onApply, onApplyAllMonths, onSave, onDelete, disabled, }: TemplateActionsProps) { const { t } = useTranslation(); const [showApply, setShowApply] = useState(false); const [showSave, setShowSave] = useState(false); const [templateName, setTemplateName] = useState(""); const [selectedTemplate, setSelectedTemplate] = useState(null); const applyRef = useRef(null); const saveRef = useRef(null); useEffect(() => { if (!showApply && !showSave) return; const handler = (e: MouseEvent) => { if (showApply && applyRef.current && !applyRef.current.contains(e.target as Node)) { setShowApply(false); setSelectedTemplate(null); } if (showSave && saveRef.current && !saveRef.current.contains(e.target as Node)) { setShowSave(false); } }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); }, [showApply, showSave]); const handleSave = () => { if (!templateName.trim()) return; onSave(templateName.trim()); setTemplateName(""); setShowSave(false); }; const handleDelete = async (e: React.MouseEvent, templateId: number) => { e.stopPropagation(); if (await confirmAction(t("budget.deleteTemplateConfirm"))) { onDelete(templateId); } }; const handleSelectTemplate = (templateId: number) => { setSelectedTemplate(templateId); }; const handleApplyToMonth = (month: number) => { if (selectedTemplate === null) return; onApply(selectedTemplate, month); setShowApply(false); setSelectedTemplate(null); }; const handleApplyAll = () => { if (selectedTemplate === null) return; onApplyAllMonths(selectedTemplate); setShowApply(false); setSelectedTemplate(null); }; return (
{/* Apply template */}
{showApply && (
{selectedTemplate === null ? ( // Step 1: Pick a template templates.length === 0 ? (

{t("budget.noTemplates")}

) : ( templates.map((tmpl) => (
handleSelectTemplate(tmpl.id)} > {tmpl.name}
)) ) ) : ( // Step 2: Pick which month(s) to apply to

{t("budget.applyToMonth")}

{t("budget.allMonths")}
{MONTH_KEYS.map((key, idx) => (
handleApplyToMonth(idx + 1)} > {t(key)}
))}
)}
)}
{/* Save as template */}
{showSave && (
setTemplateName(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") handleSave(); }} placeholder={t("budget.templateName")} className="w-full bg-[var(--background)] border border-[var(--border)] rounded-lg px-3 py-1.5 text-sm focus:outline-none focus:ring-1 focus:ring-[var(--primary)] mb-3" autoFocus />
)}
); }