"use client"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Printer, FileDown, FileText } from "lucide-react"; import { useReportDesigner } from "@/contexts/ReportDesignerContext"; import { useState } from "react"; import { useToast } from "@/hooks/use-toast"; import { getFullImageUrl } from "@/lib/api/client"; interface ReportPreviewModalProps { isOpen: boolean; onClose: () => void; } export function ReportPreviewModal({ isOpen, onClose }: ReportPreviewModalProps) { const { layoutConfig, getQueryResult, reportDetail } = useReportDesigner(); const [isExporting, setIsExporting] = useState(false); const { toast } = useToast(); // 컴포넌트의 실제 표시 값 가져오기 const getComponentValue = (component: any): string => { if (component.queryId && component.fieldName) { const queryResult = getQueryResult(component.queryId); if (queryResult && queryResult.rows.length > 0) { const value = queryResult.rows[0][component.fieldName]; if (value !== null && value !== undefined) { return String(value); } } return `{${component.fieldName}}`; } return component.defaultValue || "텍스트"; }; const handlePrint = () => { // HTML 생성하여 인쇄 const printHtml = generatePrintHTML(); const printWindow = window.open("", "_blank"); if (!printWindow) return; printWindow.document.write(printHtml); printWindow.document.close(); printWindow.print(); }; // 페이지별 컴포넌트 HTML 생성 const generatePageHTML = ( pageComponents: any[], pageWidth: number, pageHeight: number, backgroundColor: string, ): string => { const componentsHTML = pageComponents .map((component) => { const queryResult = component.queryId ? getQueryResult(component.queryId) : null; let content = ""; // Text/Label 컴포넌트 if (component.type === "text" || component.type === "label") { const displayValue = getComponentValue(component); content = `
${displayValue}
`; } // Image 컴포넌트 else if (component.type === "image" && component.imageUrl) { const imageUrl = component.imageUrl.startsWith("data:") ? component.imageUrl : getFullImageUrl(component.imageUrl); content = ``; } // Divider 컴포넌트 else if (component.type === "divider") { const width = component.orientation === "horizontal" ? "100%" : `${component.lineWidth || 1}px`; const height = component.orientation === "vertical" ? "100%" : `${component.lineWidth || 1}px`; content = `
`; } // Signature 컴포넌트 else if (component.type === "signature") { const labelPosition = component.labelPosition || "left"; const showLabel = component.showLabel !== false; const labelText = component.labelText || "서명:"; const imageUrl = component.imageUrl ? component.imageUrl.startsWith("data:") ? component.imageUrl : getFullImageUrl(component.imageUrl) : ""; if (labelPosition === "left" || labelPosition === "right") { content = `
${showLabel ? `
${labelText}
` : ""}
${imageUrl ? `` : ""} ${component.showUnderline ? '
' : ""}
`; } else { content = `
${showLabel && labelPosition === "top" ? `
${labelText}
` : ""}
${imageUrl ? `` : ""} ${component.showUnderline ? '
' : ""}
${showLabel && labelPosition === "bottom" ? `
${labelText}
` : ""}
`; } } // Stamp 컴포넌트 else if (component.type === "stamp") { const showLabel = component.showLabel !== false; const labelText = component.labelText || "(인)"; const personName = component.personName || ""; const imageUrl = component.imageUrl ? component.imageUrl.startsWith("data:") ? component.imageUrl : getFullImageUrl(component.imageUrl) : ""; content = `
${personName ? `
${personName}
` : ""}
${imageUrl ? `` : ""} ${showLabel ? `
${labelText}
` : ""}
`; } // Table 컴포넌트 else if (component.type === "table" && queryResult && queryResult.rows.length > 0) { const columns = component.tableColumns && component.tableColumns.length > 0 ? component.tableColumns : queryResult.fields.map((field) => ({ field, header: field, align: "left" as const, width: undefined, })); const tableRows = queryResult.rows .map( (row) => ` ${columns.map((col: { field: string; align?: string }) => `${String(row[col.field] ?? "")}`).join("")} `, ) .join(""); content = ` ${columns.map((col: { header: string; align?: string; width?: number }) => ``).join("")} ${tableRows}
${col.header}
`; } return `
${content}
`; }) .join(""); return `
${componentsHTML}
`; }; // 모든 페이지 HTML 생성 (인쇄/PDF용) const generatePrintHTML = (): string => { const pagesHTML = layoutConfig.pages .sort((a, b) => a.page_order - b.page_order) .map((page) => generatePageHTML( Array.isArray(page.components) ? page.components : [], page.width, page.height, page.background_color, ), ) .join('
'); return ` 리포트 인쇄 ${pagesHTML} `; }; // PDF 다운로드 (브라우저 인쇄 기능 이용) const handleDownloadPDF = () => { const printHtml = generatePrintHTML(); const printWindow = window.open("", "_blank"); if (!printWindow) return; printWindow.document.write(printHtml); printWindow.document.close(); toast({ title: "안내", description: "인쇄 대화상자에서 'PDF로 저장'을 선택하세요.", }); }; // 이미지 URL을 Base64로 변환 const imageUrlToBase64 = async (url: string): Promise => { try { // 이미 Base64인 경우 그대로 반환 if (url.startsWith("data:")) { return url; } // 서버 이미지 URL을 fetch하여 Base64로 변환 const fullUrl = getFullImageUrl(url); const response = await fetch(fullUrl); const blob = await response.blob(); return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = () => resolve(reader.result as string); reader.onerror = reject; reader.readAsDataURL(blob); }); } catch (error) { console.error("이미지 변환 실패:", error); return ""; } }; // WORD 다운로드 (백엔드 API 사용 - 컴포넌트 데이터 전송) const handleDownloadWord = async () => { setIsExporting(true); try { toast({ title: "처리 중", description: "WORD 파일을 생성하고 있습니다...", }); // 이미지를 Base64로 변환하여 컴포넌트 데이터에 포함 const pagesWithBase64 = await Promise.all( layoutConfig.pages.map(async (page) => { const componentsWithBase64 = await Promise.all( (Array.isArray(page.components) ? page.components : []).map(async (component) => { // 이미지가 있는 컴포넌트는 Base64로 변환 if (component.imageUrl) { try { const base64 = await imageUrlToBase64(component.imageUrl); return { ...component, imageBase64: base64 }; } catch { return component; } } return component; }), ); return { ...page, components: componentsWithBase64 }; }), ); // 쿼리 결과 수집 const queryResults: Record[] }> = {}; for (const page of layoutConfig.pages) { const pageComponents = Array.isArray(page.components) ? page.components : []; for (const component of pageComponents) { if (component.queryId) { const result = getQueryResult(component.queryId); if (result) { queryResults[component.queryId] = result; } } } } const fileName = reportDetail?.report?.report_name_kor || "리포트"; // 백엔드 API 호출 (컴포넌트 데이터 전송) const { apiClient } = await import("@/lib/api/client"); const response = await apiClient.post( "/admin/reports/export-word", { layoutConfig: { ...layoutConfig, pages: pagesWithBase64 }, queryResults, fileName, }, { responseType: "blob" }, ); // Blob 다운로드 const blob = new Blob([response.data], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document", }); const timestamp = new Date().toISOString().slice(0, 10); const url = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `${fileName}_${timestamp}.docx`; link.click(); window.URL.revokeObjectURL(url); toast({ title: "성공", description: "WORD 파일이 다운로드되었습니다.", }); } catch (error) { console.error("WORD 변환 오류:", error); const errorMessage = error instanceof Error ? error.message : "WORD 생성에 실패했습니다."; toast({ title: "오류", description: errorMessage, variant: "destructive", }); } finally { setIsExporting(false); } }; return ( 미리보기 현재 레이아웃의 미리보기입니다. 인쇄하거나 파일로 다운로드할 수 있습니다. {/* 미리보기 영역 - 모든 페이지 표시 */}
{layoutConfig.pages .sort((a, b) => a.page_order - b.page_order) .map((page) => (
{/* 페이지 컨텐츠 */}
{(Array.isArray(page.components) ? page.components : []).map((component) => { const displayValue = getComponentValue(component); const queryResult = component.queryId ? getQueryResult(component.queryId) : null; return (
{component.type === "text" && (
{displayValue}
)} {component.type === "label" && (
{displayValue}
)} {component.type === "table" && queryResult && queryResult.rows.length > 0 ? ( (() => { // tableColumns가 없으면 자동 생성 const columns = component.tableColumns && component.tableColumns.length > 0 ? component.tableColumns : queryResult.fields.map((field) => ({ field, header: field, align: "left" as const, width: undefined, })); return ( {columns.map((col) => ( ))} {queryResult.rows.map((row, idx) => ( {columns.map((col) => ( ))} ))}
{col.header}
{String(row[col.field] ?? "")}
); })() ) : component.type === "table" ? (
쿼리를 실행해주세요
) : null} {component.type === "image" && component.imageUrl && ( 이미지 )} {component.type === "divider" && (
)} {component.type === "signature" && (
{component.showLabel !== false && (
{component.labelText || "서명:"}
)}
{component.imageUrl && ( 서명 )} {component.showUnderline !== false && (
)}
)} {component.type === "stamp" && (
{component.personName && (
{component.personName}
)}
{component.imageUrl && ( 도장 )} {component.showLabel !== false && (
{component.labelText || "(인)"}
)}
)}
); })}
))}
); }