summaryrefslogtreecommitdiff
path: root/lib/vendor-document-list/plant/upload/components/view-submission-dialog.tsx
blob: 9a55a7fa2de8ef913f9c60c0776561b1cecbe269 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// lib/vendor-document-list/plant/upload/components/view-submission-dialog.tsx
"use client"

import * as React from "react"
import { useState, useEffect, useRef } from "react"
import { WebViewerInstance } from "@pdftron/webviewer"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { 
  Download,
  Eye,
  FileText,
  Calendar,
  User,
  CheckCircle2,
  XCircle,
  Clock,
  RefreshCw,
  Loader2
} from "lucide-react"
import { StageSubmissionView } from "@/db/schema"
import { formatDateTime, formatDate } from "@/lib/utils"
import { toast } from "sonner"
import { downloadFile, formatFileSize } from "@/lib/file-download"

interface ViewSubmissionDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  submission: StageSubmissionView
}

interface SubmissionDetail {
  id: number
  revisionNumber: number
  submissionStatus: string
  reviewStatus?: string
  reviewComments?: string
  submittedBy: string
  submittedAt: Date
  files: Array<{
    id: number
    originalFileName: string
    fileSize: number
    uploadedAt: Date
    syncStatus: string
    storageUrl: string
  }>
}

// PDFTron 문서 뷰어 컴포넌트
const DocumentViewer: React.FC<{
  open: boolean
  onClose: () => void
  files: Array<{
    id: number
    originalFileName: string
    storageUrl: string
  }>
}> = ({ open, onClose, files }) => {
  const [instance, setInstance] = useState<null | WebViewerInstance>(null)
  const [viewerLoading, setViewerLoading] = useState<boolean>(true)
  const [fileSetLoading, setFileSetLoading] = useState<boolean>(true)
  const viewer = useRef<HTMLDivElement>(null)
  const initialized = useRef(false)
  const isCancelled = useRef(false)

  const cleanupHtmlStyle = () => {
    const htmlElement = document.documentElement
    const originalStyle = htmlElement.getAttribute("style") || ""
    const colorSchemeStyle = originalStyle
      .split(";")
      .map((s) => s.trim())
      .find((s) => s.startsWith("color-scheme:"))

    if (colorSchemeStyle) {
      htmlElement.setAttribute("style", colorSchemeStyle + ";")
    } else {
      htmlElement.removeAttribute("style")
    }
  }

  useEffect(() => {
    if (open && !initialized.current) {
      initialized.current = true
      isCancelled.current = false

      requestAnimationFrame(() => {
        if (viewer.current) {
          import("@pdftron/webviewer").then(({ default: WebViewer }) => {
            if (isCancelled.current) {
              console.log("WebViewer 초기화 취소됨")
              return
            }

            WebViewer(
              {
                path: "/pdftronWeb",
                licenseKey:
                  "demo:1739264618684:616161d7030000000091db1c97c6f386d41d3506ab5b507381ef2ee2bd",
                fullAPI: true,
                css: "/globals.css",
              },
              viewer.current as HTMLDivElement
            ).then(async (instance: WebViewerInstance) => {
              setInstance(instance)
              instance.UI.enableFeatures([instance.UI.Feature.MultiTab])
              instance.UI.disableElements([
                "addTabButton",
                "multiTabsEmptyPage",
              ])
              setViewerLoading(false)
            })
          })
        }
      })
    }

    return () => {
      if (instance) {
        instance.UI.dispose()
      }
      setTimeout(() => cleanupHtmlStyle(), 500)
    }
  }, [open])

  useEffect(() => {
    const loadDocuments = async () => {
      if (instance && files.length > 0) {
        const { UI } = instance
        const tabIds = []

        for (const file of files) {
          const fileExtension = file.originalFileName.split('.').pop()?.toLowerCase()
          
          const options = {
            filename: file.originalFileName,
            ...(fileExtension === 'xlsx' || fileExtension === 'xls' ? {
              officeOptions: {
                formatOptions: {
                  applyPageBreaksToSheet: true,
                },
              },
            } : {}),
          }

          try {
            const response = await fetch(file.storageUrl)
            const blob = await response.blob()
            const tab = await UI.TabManager.addTab(blob, options)
            tabIds.push(tab)
          } catch (error) {
            console.error(`Failed to load ${file.originalFileName}:`, error)
            toast.error(`Failed to load ${file.originalFileName}`)
          }
        }

        if (tabIds.length > 0) {
          await UI.TabManager.setActiveTab(tabIds[0])
        }

        setFileSetLoading(false)
      }
    }
    
    loadDocuments()
  }, [instance, files])

  const handleClose = async () => {
    if (!fileSetLoading) {
      if (instance) {
        try {
          await instance.UI.dispose()
          setInstance(null)
        } catch (e) {
          console.warn("dispose error", e)
        }
      }
      
      setTimeout(() => cleanupHtmlStyle(), 1000)
      onClose()
    }
  }

  return (
    <Dialog open={open} onOpenChange={(val) => !val && handleClose()}>
      <DialogContent className="w-[90vw] h-[90vh]" style={{ maxWidth: "none" }}>
        <DialogHeader className="h-[38px]">
          <DialogTitle>Preview</DialogTitle>
          {/* <DialogDescription>첨부파일 미리보기</DialogDescription> */}
        </DialogHeader>
        <div
          ref={viewer}
          style={{ height: "calc(90vh - 20px - 38px - 1rem - 48px)" }}
        >
          {viewerLoading && (
            <div className="flex flex-col items-center justify-center py-12">
              <Loader2 className="h-8 w-8 text-blue-500 animate-spin mb-4" />
              <p className="text-sm text-muted-foreground">
                문서 뷰어 로딩 중...
              </p>
            </div>
          )}
        </div>
      </DialogContent>
    </Dialog>
  )
}

export function ViewSubmissionDialog({ 
  open, 
  onOpenChange, 
  submission 
}: ViewSubmissionDialogProps) {
  const [loading, setLoading] = useState(false)
  const [submissionDetail, setSubmissionDetail] = useState<SubmissionDetail | null>(null)
  const [downloadingFiles, setDownloadingFiles] = useState<Set<number>>(new Set())
  const [viewerOpen, setViewerOpen] = useState(false)
  const [selectedFiles, setSelectedFiles] = useState<Array<{
    id: number
    originalFileName: string
    storageUrl: string
  }>>([])
  
  useEffect(() => {
    if (open && submission.latestSubmissionId) {
      fetchSubmissionDetail()
    }
  }, [open, submission.latestSubmissionId])
  
  const fetchSubmissionDetail = async () => {
    if (!submission.latestSubmissionId) return
    
    setLoading(true)
    try {
      const response = await fetch(`/api/stage-submissions/${submission.latestSubmissionId}`)
      if (response.ok) {
        const data = await response.json()
        setSubmissionDetail(data)
      }
    } catch (error) {
      console.error("Failed to fetch submission details:", error)
      toast.error("Failed to load submission details")
    } finally {
      setLoading(false)
    }
  }
  
  const handleDownload = async (file: any) => {
    setDownloadingFiles(prev => new Set(prev).add(file.id))
    
    try {
      const result = await downloadFile(
        file.storageUrl, 
        file.originalFileName,
        {
          action: 'download',
          showToast: true,
          showSuccessToast: true,
          onError: (error) => {
            console.error("Download failed:", error)
            toast.error(`Failed to download ${file.originalFileName}`)
          },
          onSuccess: (fileName, fileSize) => {
            console.log(`Successfully downloaded ${fileName}`)
          }
        }
      )
      
      if (!result.success) {
        console.error("Download failed:", result.error)
      }
    } finally {
      setDownloadingFiles(prev => {
        const newSet = new Set(prev)
        newSet.delete(file.id)
        return newSet
      })
    }
  }
  
  // PDFTron으로 미리보기 처리
  const handlePreview = (file: any) => {
    setSelectedFiles([{
      id: file.id,
      originalFileName: file.originalFileName,
      storageUrl: file.storageUrl
    }])
    setViewerOpen(true)
  }
  
  // 모든 파일 미리보기
  const handlePreviewAll = () => {
    if (submissionDetail) {
      const files = submissionDetail.files.map(file => ({
        id: file.id,
        originalFileName: file.originalFileName,
        storageUrl: file.storageUrl
      }))
      setSelectedFiles(files)
      setViewerOpen(true)
    }
  }
  
  const getStatusBadge = (status?: string) => {
    if (!status) return null
    
    const variant = status === "APPROVED" ? "success" :
                   status === "REJECTED" ? "destructive" :
                   status === "SUBMITTED" ? "default" : "secondary"
    
    return <Badge variant={variant}>{status}</Badge>
  }
  
  return (
    <>
      <Dialog open={open} onOpenChange={onOpenChange}>
        <DialogContent className="max-w-4xl max-h-[80vh]">
          <DialogHeader>
            <DialogTitle>View Submission</DialogTitle>
            <DialogDescription>
              Submission details and attached files
            </DialogDescription>
          </DialogHeader>
          
          {loading ? (
            <div className="flex items-center justify-center py-8">
              <RefreshCw className="h-6 w-6 animate-spin text-muted-foreground" />
            </div>
          ) : submissionDetail ? (
            <Tabs defaultValue="details" className="w-full">
              <TabsList className="grid w-full grid-cols-2">
                <TabsTrigger value="details">Details</TabsTrigger>
                <TabsTrigger value="files">
                  Files ({submissionDetail.files.length})
                </TabsTrigger>
              </TabsList>
              
              <TabsContent value="details" className="space-y-4">
                <div className="grid gap-4">
                  <div className="grid grid-cols-2 gap-4">
                    <div className="space-y-1">
                      <p className="text-sm font-medium text-muted-foreground">
                        Revision
                      </p>
                      <p className="text-lg font-medium">
                        Rev. {submissionDetail.revisionNumber}
                      </p>
                    </div>
                    <div className="space-y-1">
                      <p className="text-sm font-medium text-muted-foreground">
                        Status
                      </p>
                      <div className="flex items-center gap-2">
                        {getStatusBadge(submissionDetail.submissionStatus)}
                        {submissionDetail.reviewStatus && 
                          getStatusBadge(submissionDetail.reviewStatus)}
                      </div>
                    </div>
                  </div>
                  
                  <div className="grid grid-cols-2 gap-4">
                    <div className="space-y-1">
                      <p className="text-sm font-medium text-muted-foreground">
                        Submitted By
                      </p>
                      <div className="flex items-center gap-2">
                        <User className="h-4 w-4 text-muted-foreground" />
                        <span>{submissionDetail.submittedBy}</span>
                      </div>
                    </div>
                    <div className="space-y-1">
                      <p className="text-sm font-medium text-muted-foreground">
                        Submitted At
                      </p>
                      <div className="flex items-center gap-2">
                        <Calendar className="h-4 w-4 text-muted-foreground" />
                        <span>{formatDateTime(submissionDetail.submittedAt)}</span>
                      </div>
                    </div>
                  </div>
                  
                  {submissionDetail.reviewComments && (
                    <div className="space-y-1">
                      <p className="text-sm font-medium text-muted-foreground">
                        Review Comments
                      </p>
                      <div className="p-3 bg-muted rounded-lg">
                        <p className="text-sm">{submissionDetail.reviewComments}</p>
                      </div>
                    </div>
                  )}
                </div>
              </TabsContent>
              
              <TabsContent value="files">
                <div className="flex justify-end mb-4">
                  <Button
                    variant="outline"
                    size="sm"
                    onClick={handlePreviewAll}
                    disabled={submissionDetail.files.length === 0}
                  >
                    <Eye className="h-4 w-4 mr-2" />
                    모든 파일 미리보기
                  </Button>
                </div>
                <ScrollArea className="h-[400px]">
                  <Table>
                    <TableHeader>
                      <TableRow>
                        <TableHead>File Name</TableHead>
                        <TableHead>Size</TableHead>
                        <TableHead>Upload Date</TableHead>
                        <TableHead>Sync Status</TableHead>
                        <TableHead className="text-right">Actions</TableHead>
                      </TableRow>
                    </TableHeader>
                    <TableBody>
                      {submissionDetail.files.map((file) => {
                        const isDownloading = downloadingFiles.has(file.id)
                        
                        return (
                          <TableRow key={file.id}>
                            <TableCell className="font-medium">
                              <div className="flex items-center gap-2">
                                <FileText className="h-4 w-4 text-muted-foreground" />
                                {file.originalFileName}
                              </div>
                            </TableCell>
                            <TableCell>{formatFileSize(file.fileSize)}</TableCell>
                            <TableCell>{formatDate(file.uploadedAt)}</TableCell>
                            <TableCell>
                              <Badge 
                                variant={
                                  file.syncStatus === "synced" ? "success" :
                                  file.syncStatus === "failed" ? "destructive" :
                                  "secondary"
                                }
                                className="text-xs"
                              >
                                {file.syncStatus}
                              </Badge>
                            </TableCell>
                            <TableCell className="text-right">
                              <div className="flex justify-end gap-2">
                                <Button
                                  variant="ghost"
                                  size="icon"
                                  onClick={() => handleDownload(file)}
                                  disabled={isDownloading}
                                  title="Download"
                                >
                                  {isDownloading ? (
                                    <RefreshCw className="h-4 w-4 animate-spin" />
                                  ) : (
                                    <Download className="h-4 w-4" />
                                  )}
                                </Button>
                                <Button
                                  variant="ghost"
                                  size="icon"
                                  onClick={() => handlePreview(file)}
                                  disabled={isDownloading}
                                  title="Preview"
                                >
                                  {isDownloading ? (
                                    <RefreshCw className="h-4 w-4 animate-spin" />
                                  ) : (
                                    <Eye className="h-4 w-4" />
                                  )}
                                </Button>
                              </div>
                            </TableCell>
                          </TableRow>
                        )
                      })}
                    </TableBody>
                  </Table>
                </ScrollArea>
              </TabsContent>
            </Tabs>
          ) : (
            <div className="text-center py-8 text-muted-foreground">
              No submission found
            </div>
          )}
        </DialogContent>
      </Dialog>
      
      {/* PDFTron 문서 뷰어 다이얼로그 */}
      {viewerOpen && (
        <DocumentViewer
          open={viewerOpen}
          onClose={() => {
            setViewerOpen(false)
            setSelectedFiles([])
          }}
          files={selectedFiles}
        />
      )}
    </>
  )
}