"use client"; import { useEffect, useState } from "react"; import { Button } from "@/components/ui/button"; import { Trash2, Loader2, RefreshCw } from "lucide-react"; import { useReportDesigner } from "@/contexts/ReportDesignerContext"; import { reportApi } from "@/lib/api/reportApi"; import { useToast } from "@/hooks/use-toast"; import { Badge } from "@/components/ui/badge"; const SYSTEM_TEMPLATES = [ { id: "order", name: "λ°œμ£Όμ„œ", icon: "πŸ“‹" }, { id: "invoice", name: "μ²­κ΅¬μ„œ", icon: "πŸ’°" }, { id: "basic", name: "κΈ°λ³Έ", icon: "πŸ“„" }, ]; interface CustomTemplate { template_id: string; template_name_kor: string; template_name_eng: string | null; is_system: string; } export function TemplatePalette() { const { applyTemplate } = useReportDesigner(); const [customTemplates, setCustomTemplates] = useState([]); const [isLoading, setIsLoading] = useState(false); const [deletingId, setDeletingId] = useState(null); const { toast } = useToast(); const fetchCustomTemplates = async () => { setIsLoading(true); try { const response = await reportApi.getTemplates(); if (response.success && response.data) { setCustomTemplates(response.data.custom || []); } } catch (error) { console.error("ν…œν”Œλ¦Ώ 쑰회 μ‹€νŒ¨:", error); } finally { setIsLoading(false); } }; useEffect(() => { fetchCustomTemplates(); }, []); const handleApplyTemplate = async (templateId: string) => { await applyTemplate(templateId); }; const handleDeleteTemplate = async (templateId: string, templateName: string) => { if (!confirm(`"${templateName}" ν…œν”Œλ¦Ώμ„ μ‚­μ œν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ?`)) { return; } setDeletingId(templateId); try { const response = await reportApi.deleteTemplate(templateId); if (response.success) { toast({ title: "성곡", description: "ν…œν”Œλ¦Ώμ΄ μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.", }); fetchCustomTemplates(); } } catch (error: any) { toast({ title: "였λ₯˜", description: error.response?.data?.message || "ν…œν”Œλ¦Ώ μ‚­μ œμ— μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.", variant: "destructive", }); } finally { setDeletingId(null); } }; return (
{/* μ‹œμŠ€ν…œ ν…œν”Œλ¦Ώ */}

μ‹œμŠ€ν…œ ν…œν”Œλ¦Ώ

{SYSTEM_TEMPLATES.map((template) => ( ))}
{/* μ‚¬μš©μž μ •μ˜ ν…œν”Œλ¦Ώ */}

μ‚¬μš©μž μ •μ˜ ν…œν”Œλ¦Ώ

{isLoading ? (
) : customTemplates.length === 0 ? (

μ €μž₯된 ν…œν”Œλ¦Ώμ΄ μ—†μŠ΅λ‹ˆλ‹€

) : ( customTemplates.map((template) => (
)) )}
); }