summaryrefslogtreecommitdiff
path: root/lib/gtc-contract/gtc-clauses/table/preview-document-dialog.tsx
blob: 3639c0f3f7470f8b4ace473674a6526f92c84a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
"use client"

import * as React from "react"
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Separator } from "@/components/ui/separator"

import { 
  Eye, 
  Download, 
  Loader2, 
  FileText, 
  RefreshCw,
  Settings,
  AlertCircle
} from "lucide-react"
import { toast } from "sonner"

import { type GtcClauseTreeView } from "@/db/schema/gtc"
import { ClausePreviewViewer } from "./clause-preview-viewer"

interface PreviewDocumentDialogProps
  extends React.ComponentPropsWithRef<typeof Dialog> {
  clauses: GtcClauseTreeView[]
  document: any
  onExport?: () => void
}

export function PreviewDocumentDialog({ 
  clauses, 
  document,
  onExport,
  ...props 
}: PreviewDocumentDialogProps) {
  const [isGenerating, setIsGenerating] = React.useState(false)
  const [documentGenerated, setDocumentGenerated] = React.useState(false)
  const [viewerInstance, setViewerInstance] = React.useState<any>(null)
  const [hasError, setHasError] = React.useState(false)

  // 조항 통계 계산
  const stats = React.useMemo(() => {
    const activeClausesCount = clauses.filter(c => c.isActive !== false).length
    const topLevelCount = clauses.filter(c => !c.parentId && c.isActive !== false).length
    const hasContentCount = clauses.filter(c => c.content && c.isActive !== false).length
    
    return {
      total: activeClausesCount,
      topLevel: topLevelCount,
      withContent: hasContentCount,
      withoutContent: activeClausesCount - hasContentCount
    }
  }, [clauses])

  const handleGeneratePreview = async () => {
    setIsGenerating(true)
    setHasError(false)
    setDocumentGenerated(false)
    
    try {
      // 실제로는 ClausePreviewViewer에서 문서 생성을 처리하므로
      // 여기서는 상태만 관리
      console.log("🚀 문서 미리보기 생성 시작")
      
      // ClausePreviewViewer가 완전히 로드될 때까지 기다림
      await new Promise(resolve => setTimeout(resolve, 2000))
      
      if (!hasError) {
        setDocumentGenerated(true)
        toast.success("문서 미리보기가 생성되었습니다.")
      }
    } catch (error) {
      console.error("문서 생성 중 오류:", error)
      setHasError(true)
      toast.error("문서 생성 중 오류가 발생했습니다.")
    } finally {
      setIsGenerating(false)
    }
  }

  const handleExportDocument = () => {
    if (viewerInstance) {
      try {
        // PDFTron의 다운로드 기능 실행
        viewerInstance.UI.downloadPdf({
          filename: `${document?.title || 'GTC계약서'}_미리보기.pdf`
        })
        toast.success("PDF 다운로드가 시작됩니다.")
      } catch (error) {
        console.error("다운로드 오류:", error)
        toast.error("다운로드 중 오류가 발생했습니다.")
      }
    } else {
      toast.error("뷰어가 준비되지 않았습니다.")
    }
  }

  const handleRegenerateDocument = () => {
    console.log("🔄 문서 재생성 시작")
    setDocumentGenerated(false)
    setHasError(false)
    handleGeneratePreview()
  }

  const handleViewerSuccess = React.useCallback(() => {
    setDocumentGenerated(true)
    setIsGenerating(false)
    setHasError(false)
  }, [])

  const handleViewerError = React.useCallback(() => {
    setHasError(true)
    setIsGenerating(false)
    setDocumentGenerated(false)
  }, [])

  // 다이얼로그가 열릴 때 자동으로 미리보기 생성
  React.useEffect(() => {
    if (props.open && !documentGenerated && !isGenerating && !hasError) {
      const timer = setTimeout(() => {
        handleGeneratePreview()
      }, 300) // 다이얼로그 애니메이션 후 시작
      
      return () => clearTimeout(timer)
    }
  }, [props.open, documentGenerated, isGenerating, hasError])

  // 다이얼로그가 닫힐 때 상태 초기화
  React.useEffect(() => {
    if (!props.open) {
      setDocumentGenerated(false)
      setIsGenerating(false)
      setHasError(false)
      setViewerInstance(null)
    }
  }, [props.open])

  return (
    <Dialog {...props}>
      <DialogContent className="max-w-7xl h-[90vh] flex flex-col">
        <DialogHeader className="flex-shrink-0">
          <DialogTitle className="flex items-center gap-2">
            <Eye className="h-5 w-5" />
            문서 미리보기
          </DialogTitle>
          <DialogDescription>
            현재 조항들을 기반으로 생성된 문서를 미리보기합니다.
          </DialogDescription>
        </DialogHeader>

        {/* 문서 정보 및 통계 */}
        <div className="flex-shrink-0 p-4 bg-muted/30 rounded-lg">
          <div className="flex items-center justify-between mb-3">
            <div className="flex items-center gap-2">
              <FileText className="h-4 w-4" />
              <span className="font-medium">{document?.title || 'GTC 계약서'}</span>
              <Badge variant="outline">{stats.total}개 조항</Badge>
              {hasError && (
                <Badge variant="destructive" className="gap-1">
                  <AlertCircle className="h-3 w-3" />
                  오류 발생
                </Badge>
              )}
            </div>
            <div className="flex items-center gap-2">
              {documentGenerated && !hasError && (
                <>
                  <Button
                    variant="outline"
                    size="sm"
                    onClick={handleRegenerateDocument}
                    disabled={isGenerating}
                  >
                    <RefreshCw className={`mr-2 h-3 w-3 ${isGenerating ? 'animate-spin' : ''}`} />
                    재생성
                  </Button>
                  <Button
                    variant="outline"
                    size="sm"
                    onClick={handleExportDocument}
                    disabled={!viewerInstance}
                  >
                    <Download className="mr-2 h-3 w-3" />
                    PDF 다운로드
                  </Button>
                </>
              )}
              {hasError && (
                <Button
                  variant="default"
                  size="sm"
                  onClick={handleRegenerateDocument}
                  disabled={isGenerating}
                >
                  <RefreshCw className={`mr-2 h-3 w-3 ${isGenerating ? 'animate-spin' : ''}`} />
                  다시 시도
                </Button>
              )}
            </div>
          </div>

          <div className="grid grid-cols-4 gap-4 text-sm">
            <div className="text-center p-2 bg-background rounded">
              <div className="font-medium text-lg">{stats.total}</div>
              <div className="text-muted-foreground">총 조항</div>
            </div>
            <div className="text-center p-2 bg-background rounded">
              <div className="font-medium text-lg">{stats.topLevel}</div>
              <div className="text-muted-foreground">최상위 조항</div>
            </div>
            <div className="text-center p-2 bg-background rounded">
              <div className="font-medium text-lg text-green-600">{stats.withContent}</div>
              <div className="text-muted-foreground">내용 있음</div>
            </div>
            <div className="text-center p-2 bg-background rounded">
              <div className="font-medium text-lg text-amber-600">{stats.withoutContent}</div>
              <div className="text-muted-foreground">제목만</div>
            </div>
          </div>
        </div>

        <Separator />

        {/* PDFTron 뷰어 영역 */}
        <div className="flex-1 min-h-0 relative">
          {isGenerating ? (
            <div className="absolute inset-0 flex flex-col items-center justify-center bg-background">
              <Loader2 className="h-8 w-8 animate-spin text-primary mb-4" />
              <p className="text-lg font-medium mb-2">문서 생성 중...</p>
              <p className="text-sm text-muted-foreground">
                {stats.total}개의 조항을 배치하고 있습니다.
              </p>
              <p className="text-xs text-gray-400 mt-2">
                초기화에 시간이 걸릴 수 있습니다...
              </p>
            </div>
          ) : hasError ? (
            <div className="absolute inset-0 flex flex-col items-center justify-center bg-muted/10">
              <AlertCircle className="h-12 w-12 text-destructive mb-4" />
              <p className="text-lg font-medium mb-2 text-destructive">문서 생성 실패</p>
              <p className="text-sm text-muted-foreground mb-4 text-center max-w-md">
                문서 생성 중 오류가 발생했습니다. 네트워크 연결이나 파일 권한을 확인해주세요.
              </p>
              <Button onClick={handleRegenerateDocument} disabled={isGenerating}>
                <RefreshCw className="mr-2 h-4 w-4" />
                다시 시도
              </Button>
            </div>
          ) : documentGenerated ? (
            <ClausePreviewViewer
              clauses={clauses}
              document={document}
              instance={viewerInstance}
              setInstance={setViewerInstance}
              onSuccess={handleViewerSuccess}
              onError={handleViewerError}
            />
          ) : (
            <div className="absolute inset-0 flex flex-col items-center justify-center bg-muted/10">
              <FileText className="h-12 w-12 text-muted-foreground mb-4" />
              <p className="text-lg font-medium mb-2">문서 미리보기 준비 중</p>
              <Button onClick={handleGeneratePreview} disabled={isGenerating}>
                <Eye className="mr-2 h-4 w-4" />
                미리보기 생성
              </Button>
            </div>
          )}
        </div>
      </DialogContent>
    </Dialog>
  )
}