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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
|
"use client"
import * as React from "react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { z } from "zod"
import { toast } from "sonner"
import { useRouter } from "next/navigation"
import { useSession } from "next-auth/react"
import ExcelJS from 'exceljs'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import {
Dropzone,
DropzoneDescription,
DropzoneInput,
DropzoneTitle,
DropzoneUploadIcon,
DropzoneZone,
} from "@/components/ui/dropzone"
import {
FileList,
FileListAction,
FileListHeader,
FileListIcon,
FileListInfo,
FileListItem,
FileListName,
FileListSize,
} from "@/components/ui/file-list"
import { Badge } from "@/components/ui/badge"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Separator } from "@/components/ui/separator"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import {
Upload,
X,
Loader2,
FileSpreadsheet,
Files,
CheckCircle2,
AlertCircle,
Download
} from "lucide-react"
import prettyBytes from "pretty-bytes"
import type { EnhancedDocument } from "@/types/enhanced-documents"
// 일괄 업로드 스키마
const bulkUploadSchema = z.object({
uploaderName: z.string().optional(),
comment: z.string().optional(),
templateFile: z.instanceof(File).optional(),
attachmentFiles: z.array(z.instanceof(File)).min(1, "최소 1개 파일이 필요합니다"),
})
type BulkUploadSchema = z.infer<typeof bulkUploadSchema>
interface ParsedUploadItem {
documentId: number
docNumber: string
title: string
stage: string
revision: string
fileNames: string[] // ';'로 구분된 파일명들
}
interface FileMatchResult {
matched: { file: File; item: ParsedUploadItem }[]
unmatched: File[]
missingFiles: string[]
}
interface BulkUploadDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
documents: EnhancedDocument[]
projectType: "ship" | "plant"
contractId: number // ✅ contractId 추가
}
export function BulkUploadDialog({
open,
onOpenChange,
documents,
projectType,
contractId, // ✅ contractId 받기
}: BulkUploadDialogProps) {
const [selectedFiles, setSelectedFiles] = React.useState<File[]>([])
const [templateFile, setTemplateFile] = React.useState<File | null>(null)
const [parsedData, setParsedData] = React.useState<ParsedUploadItem[]>([])
const [matchResult, setMatchResult] = React.useState<FileMatchResult | null>(null)
const [isUploading, setIsUploading] = React.useState(false)
const [uploadProgress, setUploadProgress] = React.useState(0)
const [currentStep, setCurrentStep] = React.useState<'template' | 'files' | 'review' | 'upload'>('template')
const router = useRouter()
const { data: session } = useSession()
const form = useForm<BulkUploadSchema>({
resolver: zodResolver(bulkUploadSchema),
defaultValues: {
uploaderName: session?.user?.name || "",
comment: "",
templateFile: undefined,
attachmentFiles: [],
},
})
React.useEffect(() => {
if (session?.user?.name) {
form.setValue('uploaderName', session.user.name)
}
}, [session?.user?.name, form])
// 다이얼로그가 열릴 때마다 업로더명 리프레시
React.useEffect(() => {
if (open && session?.user?.name) {
form.setValue('uploaderName', session.user.name)
}
}, [open, session?.user?.name, form])
// 리비전 정렬 및 최신 리비전 찾기 헬퍼 함수들
const compareRevisions = (a: string, b: string): number => {
// 알파벳 리비전 (A, B, C, ..., Z, AA, AB, ...)
const aIsAlpha = /^[A-Z]+$/.test(a)
const bIsAlpha = /^[A-Z]+$/.test(b)
if (aIsAlpha && bIsAlpha) {
// 길이 먼저 비교 (A < AA)
if (a.length !== b.length) {
return a.length - b.length
}
// 같은 길이면 알파벳 순서
return a.localeCompare(b)
}
// 숫자 리비전 (0, 1, 2, ...)
const aIsNumber = /^\d+$/.test(a)
const bIsNumber = /^\d+$/.test(b)
if (aIsNumber && bIsNumber) {
return parseInt(a) - parseInt(b)
}
// 혼재된 경우 알파벳이 먼저
if (aIsAlpha && bIsNumber) return -1
if (aIsNumber && bIsAlpha) return 1
// 기타 복잡한 형태는 문자열 비교
return a.localeCompare(b)
}
const getLatestRevisionInStage = (document: EnhancedDocument, stageName: string): string => {
const stage = document.allStages?.find(s => s.stageName === stageName)
if (!stage || !stage.revisions || stage.revisions.length === 0) {
return ''
}
// 리비전들을 정렬해서 최신 것 찾기
const sortedRevisions = [...stage.revisions].sort((a, b) =>
compareRevisions(a.revision, b.revision)
)
return sortedRevisions[sortedRevisions.length - 1]?.revision || ''
}
const getNextRevision = (currentRevision: string): string => {
if (!currentRevision) return "A"
// 알파벳 리비전 (A, B, C...)
if (/^[A-Z]+$/.test(currentRevision)) {
// 한 글자인 경우
if (currentRevision.length === 1) {
const charCode = currentRevision.charCodeAt(0)
if (charCode < 90) { // Z가 아닌 경우
return String.fromCharCode(charCode + 1)
}
return "AA" // Z 다음은 AA
}
// 여러 글자인 경우 (AA, AB, ... AZ, BA, ...)
let result = currentRevision
let carry = true
let newResult = ''
for (let i = result.length - 1; i >= 0 && carry; i--) {
let charCode = result.charCodeAt(i)
if (charCode < 90) { // Z가 아닌 경우
newResult = String.fromCharCode(charCode + 1) + newResult
carry = false
} else { // Z인 경우
newResult = 'A' + newResult
}
}
if (carry) {
newResult = 'A' + newResult
} else {
newResult = result.substring(0, result.length - newResult.length) + newResult
}
return newResult
}
// 숫자 리비전 (0, 1, 2...)
if (/^\d+$/.test(currentRevision)) {
return String(parseInt(currentRevision) + 1)
}
// 기타 복잡한 리비전 형태는 그대로 반환
return currentRevision
}
// 템플릿 export 함수
const exportTemplate = async () => {
try {
const workbook = new ExcelJS.Workbook()
const worksheet = workbook.addWorksheet('BulkUploadTemplate')
// 헤더 정의
const headers = [
'documentId',
'docNumber',
'title',
'currentStage',
'latestRevision',
'targetStage',
'targetRevision',
'fileNames'
]
// 헤더 스타일링
const headerRow = worksheet.addRow(headers)
headerRow.eachCell((cell, colNumber) => {
cell.font = { bold: true, color: { argb: 'FFFFFF' } }
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: '366092' }
}
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
}
cell.alignment = { horizontal: 'center', vertical: 'middle' }
})
// 데이터 추가
documents.forEach(doc => {
const currentStageName = doc.currentStageName || ''
const latestRevision = getLatestRevisionInStage(doc, currentStageName)
const row = worksheet.addRow([
doc.documentId,
doc.docNumber,
doc.title,
currentStageName,
latestRevision, // 현재 스테이지의 최신 리비전
currentStageName, // 기본값으로 현재 스테이지 설정
latestRevision, // 기본값으로 현재 최신 리비전 설정 (사용자가 선택)
'', // 사용자가 입력할 파일명들 (';'로 구분)
])
// 데이터 행 스타일링
row.eachCell((cell, colNumber) => {
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
}
// 편집 가능한 칼럼 (targetStage, targetRevision, fileNames) 강조
if (colNumber >= 6) {
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'FFF2CC' } // 연한 노란색
}
}
})
})
// 칼럼 너비 설정
worksheet.columns = [
{ width: 12 }, // documentId
{ width: 18 }, // docNumber
{ width: 35 }, // title
{ width: 20 }, // currentStage
{ width: 15 }, // latestRevision
{ width: 20 }, // targetStage
{ width: 15 }, // targetRevision
{ width: 60 }, // fileNames
]
// 헤더 고정
worksheet.views = [{ state: 'frozen', ySplit: 1 }]
// 주석 추가
const instructionRow = worksheet.insertRow(1, [
'지침:',
'1. latestRevision: 현재 스테이지의 최신 리비전',
'2. targetStage: 업로드할 스테이지명 (수정 가능)',
'3. targetRevision: 같은 리비전에 파일 추가 시 그대로, 새 리비전 생성 시 수정',
'4. fileNames: 파일명들을 세미콜론(;)으로 구분',
'예: file1.pdf;file2.dwg;file3.xlsx',
'',
'← 이 행은 삭제하고 사용해도 됩니다'
])
instructionRow.eachCell((cell) => {
cell.font = { italic: true, color: { argb: '888888' } }
cell.fill = {
type: 'pattern',
pattern: 'solid',
fgColor: { argb: 'F0F0F0' }
}
})
// 파일 다운로드
const buffer = await workbook.xlsx.writeBuffer()
const blob = new Blob([buffer], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = `bulk-upload-template-${new Date().toISOString().split('T')[0]}.xlsx`
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(url)
toast.success("템플릿이 다운로드되었습니다. targetRevision은 기본값(최신 리비전)이 설정되어 있습니다!")
} catch (error) {
console.error('템플릿 생성 오류:', error)
toast.error('템플릿 생성에 실패했습니다.')
}
}
// 템플릿 파일 파싱
const parseTemplateFile = async (file: File) => {
try {
const arrayBuffer = await file.arrayBuffer()
const workbook = new ExcelJS.Workbook()
await workbook.xlsx.load(arrayBuffer)
const worksheet = workbook.getWorksheet(1) // 첫 번째 워크시트
if (!worksheet) {
throw new Error('워크시트를 찾을 수 없습니다.')
}
// 헤더 행 찾기 (지침 행이 있을 수 있으므로)
let headerRowIndex = 1
let headers: string[] = []
// 최대 5행까지 헤더를 찾아본다
for (let i = 1; i <= 5; i++) {
const row = worksheet.getRow(i)
const firstCell = row.getCell(1).value
if (firstCell && String(firstCell).includes('documentId')) {
headerRowIndex = i
headers = []
// 헤더 추출
for (let col = 1; col <= 8; col++) {
const cellValue = row.getCell(col).value
headers.push(String(cellValue || ''))
}
break
}
}
const expectedHeaders = ['documentId', 'docNumber', 'title', 'currentStage', 'latestRevision', 'targetStage', 'targetRevision', 'fileNames']
const missingHeaders = expectedHeaders.filter(h => !headers.includes(h))
if (missingHeaders.length > 0) {
throw new Error(`필수 칼럼이 누락되었습니다: ${missingHeaders.join(', ')}`)
}
// 데이터 파싱
const parsed: ParsedUploadItem[] = []
const rowCount = worksheet.rowCount
console.log(`📊 파싱 시작: 총 ${rowCount}행, 헤더 행: ${headerRowIndex}`)
for (let i = headerRowIndex + 1; i <= rowCount; i++) {
const row = worksheet.getRow(i)
// 빈 행 스킵
if (!row.hasValues) {
console.log(`행 ${i}: 빈 행 스킵`)
continue
}
const documentIdCell = row.getCell(headers.indexOf('documentId') + 1).value
const docNumberCell = row.getCell(headers.indexOf('docNumber') + 1).value
const titleCell = row.getCell(headers.indexOf('title') + 1).value
const stageCell = row.getCell(headers.indexOf('targetStage') + 1).value
const revisionCell = row.getCell(headers.indexOf('targetRevision') + 1).value
const fileNamesCell = row.getCell(headers.indexOf('fileNames') + 1).value
// 값들을 안전하게 변환
const documentId = Number(documentIdCell) || 0
const docNumber = String(docNumberCell || '').trim()
const title = String(titleCell || '').trim()
const stage = String(stageCell || '').trim().replace(/[ \s]/g, ' ').trim() // 전각공백 처리
const revision = String(revisionCell || '').trim()
const fileNamesStr = String(fileNamesCell || '').trim().replace(/[ \s]/g, ' ').trim() // 전각공백 처리
console.log(`행 ${i} 파싱 결과:`, {
documentId, docNumber, title, stage, revision, fileNamesStr,
originalCells: { documentIdCell, docNumberCell, titleCell, stageCell, revisionCell, fileNamesCell }
})
// 필수 데이터 체크 (documentId와 docNumber만 체크, stage와 revision은 빈 값 허용)
if (!documentId || !docNumber) {
console.warn(`행 ${i}: 필수 데이터 누락 (documentId: ${documentId}, docNumber: ${docNumber})`)
continue
}
// fileNames가 비어있는 행은 무시
if (!fileNamesStr || fileNamesStr === '' || fileNamesStr === 'undefined' || fileNamesStr === 'null') {
console.log(`행 ${i}: fileNames가 비어있어 스킵합니다. (${docNumber}) - fileNamesStr: "${fileNamesStr}"`)
continue
}
// stage와 revision이 비어있는 경우 기본값 설정
const finalStage = stage || 'Default Stage'
const finalRevision = revision || 'A'
const fileNames = fileNamesStr.split(';').map(name => name.trim()).filter(Boolean)
if (fileNames.length === 0) {
console.warn(`행 ${i}: 파일명 파싱 실패 (${docNumber}) - 원본: "${fileNamesStr}"`)
continue
}
console.log(`✅ 행 ${i} 파싱 성공:`, {
documentId, docNumber, stage: finalStage, revision: finalRevision, fileNames
})
parsed.push({
documentId,
docNumber,
title,
stage: finalStage,
revision: finalRevision,
fileNames,
})
}
console.log(`📋 파싱 완료: ${parsed.length}개 항목`)
if (parsed.length === 0) {
console.error('파싱된 데이터:', parsed)
throw new Error('파싱할 수 있는 유효한 데이터가 없습니다. fileNames 칼럼에 파일명이 입력되어 있는지 확인해주세요.')
}
setParsedData(parsed)
setCurrentStep('files')
toast.success(`템플릿 파싱 완료: ${parsed.length}개 항목, 총 ${parsed.reduce((sum, item) => sum + item.fileNames.length, 0)}개 파일 필요`)
} catch (error) {
console.error('템플릿 파싱 오류:', error)
toast.error(error instanceof Error ? error.message : '템플릿 파싱에 실패했습니다.')
}
}
// 파일 매칭 로직
const matchFiles = (files: File[], uploadItems: ParsedUploadItem[]): FileMatchResult => {
const matched: { file: File; item: ParsedUploadItem }[] = []
const unmatched: File[] = []
const missingFiles: string[] = []
// 모든 필요한 파일명 수집
const requiredFileNames = new Set<string>()
uploadItems.forEach(item => {
item.fileNames.forEach(fileName => requiredFileNames.add(fileName))
})
// 파일 매칭
files.forEach(file => {
let isMatched = false
for (const item of uploadItems) {
if (item.fileNames.some(fileName => fileName === file.name)) {
matched.push({ file, item })
isMatched = true
break
}
}
if (!isMatched) {
unmatched.push(file)
}
})
// 누락된 파일 찾기
const uploadedFileNames = new Set(files.map(f => f.name))
requiredFileNames.forEach(fileName => {
if (!uploadedFileNames.has(fileName)) {
missingFiles.push(fileName)
}
})
return { matched, unmatched, missingFiles }
}
// 템플릿 드롭 처리
const handleTemplateDropAccepted = (acceptedFiles: File[]) => {
const file = acceptedFiles[0]
if (!file) return
if (!file.name.endsWith('.xlsx') && !file.name.endsWith('.xls')) {
toast.error('Excel 파일(.xlsx, .xls)만 업로드 가능합니다.')
return
}
setTemplateFile(file)
form.setValue('templateFile', file)
parseTemplateFile(file)
}
// 파일 드롭 처리
const handleFilesDropAccepted = (acceptedFiles: File[]) => {
const newFiles = [...selectedFiles, ...acceptedFiles]
setSelectedFiles(newFiles)
form.setValue('attachmentFiles', newFiles, { shouldValidate: true })
// 파일 매칭 수행
if (parsedData.length > 0) {
const result = matchFiles(newFiles, parsedData)
setMatchResult(result)
setCurrentStep('review')
}
}
// 파일 제거
const removeFile = (index: number) => {
const updatedFiles = [...selectedFiles]
updatedFiles.splice(index, 1)
setSelectedFiles(updatedFiles)
form.setValue('attachmentFiles', updatedFiles, { shouldValidate: true })
if (parsedData.length > 0) {
const result = matchFiles(updatedFiles, parsedData)
setMatchResult(result)
}
}
// 일괄 업로드 처리
const onSubmit = async (data: BulkUploadSchema) => {
if (!matchResult || matchResult.matched.length === 0) {
toast.error('매칭된 파일이 없습니다.')
return
}
setIsUploading(true)
setUploadProgress(0)
setCurrentStep('upload')
try {
const formData = new FormData()
// 메타데이터
formData.append('uploaderName', data.uploaderName || '')
formData.append('comment', data.comment || '')
formData.append('projectType', projectType)
if (contractId) {
formData.append('contractId', String(contractId)) // ✅ contractId 추가
}
formData.append('contractId', String(contractId)) // ✅ contractId 추가
// 매칭된 파일들과 메타데이터
const uploadData = matchResult.matched.map(({ file, item }) => ({
documentId: item.documentId,
stage: item.stage,
revision: item.revision,
fileName: file.name,
}))
formData.append('uploadData', JSON.stringify(uploadData))
// 파일들 추가
matchResult.matched.forEach(({ file }, index) => {
formData.append(`file_${index}`, file)
})
// 진행률 시뮬레이션
const progressInterval = setInterval(() => {
setUploadProgress(prev => Math.min(prev + 10, 90))
}, 500)
const response = await fetch('/api/bulk-upload', {
method: 'POST',
body: formData,
})
clearInterval(progressInterval)
if (!response.ok) {
const errorData = await response.json()
throw new Error(errorData.error || '일괄 업로드에 실패했습니다.')
}
const result = await response.json()
setUploadProgress(100)
toast.success(`${result.data?.uploadedCount || 0}개 파일이 성공적으로 업로드되었습니다.`)
setTimeout(() => {
handleDialogClose()
router.refresh()
}, 1000)
} catch (error) {
console.error('일괄 업로드 오류:', error)
toast.error(error instanceof Error ? error.message : "업로드 중 오류가 발생했습니다")
} finally {
setIsUploading(false)
setTimeout(() => setUploadProgress(0), 2000)
}
}
const handleDialogClose = () => {
form.reset({
uploaderName: session?.user?.name || "", // ✅ 항상 최신 session 값으로 리셋
comment: "",
templateFile: undefined,
attachmentFiles: [],
})
setSelectedFiles([])
setTemplateFile(null)
setParsedData([])
setMatchResult(null)
setCurrentStep('template')
setIsUploading(false)
setUploadProgress(0)
onOpenChange(false)
}
const canProceedToUpload = matchResult && matchResult.matched.length > 0 && matchResult.missingFiles.length === 0
return (
<Dialog open={open} onOpenChange={handleDialogClose}>
<DialogContent className="sm:max-w-6xl max-h-[90vh] flex flex-col" style={{maxWidth:900}}>
{/* 고정 헤더 */}
<DialogHeader className="flex-shrink-0">
<DialogTitle className="flex items-center gap-2">
<Files className="w-5 h-5" />
일괄 업로드
</DialogTitle>
<DialogDescription>
템플릿을 다운로드하여 파일명을 입력한 후, 실제 파일들을 업로드하세요.
</DialogDescription>
<div className="flex items-center gap-2 pt-2">
<Badge variant={projectType === "ship" ? "default" : "secondary"}>
{projectType === "ship" ? "조선 프로젝트" : "플랜트 프로젝트"}
</Badge>
<Badge variant="outline">
총 {documents.length}개 문서
</Badge>
</div>
</DialogHeader>
{/* 스크롤 가능한 메인 컨텐츠 영역 */}
<div className="flex-1 overflow-y-auto px-1">
{/* 단계별 진행 상태 */}
<div className="flex items-center gap-2 mb-4">
{[
{ key: 'template', label: '템플릿' },
{ key: 'files', label: '파일 업로드' },
{ key: 'review', label: '검토' },
{ key: 'upload', label: '업로드' },
].map((step, index) => (
<React.Fragment key={step.key}>
<div className={`flex items-center gap-1 px-2 py-1 rounded text-xs ${
currentStep === step.key ? 'bg-primary text-primary-foreground' :
['template', 'files', 'review'].indexOf(currentStep) > ['template', 'files', 'review'].indexOf(step.key) ? 'bg-green-100 text-green-700' :
'bg-gray-100 text-gray-500'
}`}>
{step.label}
</div>
{index < 3 && <div className="w-2 h-px bg-gray-300" />}
</React.Fragment>
))}
</div>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
{/* 1단계: 템플릿 다운로드 및 업로드 */}
{currentStep === 'template' && (
<div className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<Download className="w-4 h-4" />
1단계: 템플릿 다운로드
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-gray-600">
현재 문서 목록을 기반으로 업로드 템플릿을 생성합니다.
마지막 "fileNames" 칼럼에 업로드할 파일명을 ';'로 구분하여 입력하세요.
</p>
<Button type="button" onClick={exportTemplate} className="gap-2">
<Download className="w-4 h-4" />
템플릿 다운로드 ({documents.length}개 문서)
</Button>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<Upload className="w-4 h-4" />
작성된 템플릿 업로드
</CardTitle>
</CardHeader>
<CardContent>
<Dropzone
maxSize={10e6} // 10MB
multiple={false}
accept={{
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': ['.xlsx'],
'application/vnd.ms-excel': ['.xls']
}}
onDropAccepted={handleTemplateDropAccepted}
disabled={isUploading}
>
<DropzoneZone>
<FormControl>
<DropzoneInput />
</FormControl>
<div className="flex items-center gap-6">
<FileSpreadsheet className="w-8 h-8 text-gray-400" />
<div className="grid gap-0.5">
<DropzoneTitle>작성된 Excel 템플릿을 업로드하세요</DropzoneTitle>
<DropzoneDescription>
.xlsx, .xls 파일을 지원합니다
</DropzoneDescription>
</div>
</div>
</DropzoneZone>
</Dropzone>
{templateFile && (
<div className="mt-4 p-3 bg-green-50 border border-green-200 rounded-lg">
<div className="flex items-center gap-2">
<CheckCircle2 className="w-4 h-4 text-green-600" />
<span className="text-sm text-green-700">
템플릿 업로드 완료: {templateFile.name}
</span>
</div>
</div>
)}
</CardContent>
</Card>
</div>
)}
{/* 2단계: 파일 업로드 */}
{currentStep === 'files' && (
<div className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<Files className="w-4 h-4" />
2단계: 실제 파일들 업로드
</CardTitle>
</CardHeader>
<CardContent>
<div className="mb-4 p-3 bg-blue-50 border border-blue-200 rounded-lg">
<p className="text-sm text-blue-700">
템플릿에서 {parsedData.length}개 항목, 총 {parsedData.reduce((sum, item) => sum + item.fileNames.length, 0)}개 파일이 필요합니다.
</p>
</div>
<Dropzone
maxSize={3e9} // 3GB
multiple={true}
onDropAccepted={handleFilesDropAccepted}
disabled={isUploading}
>
<DropzoneZone>
<FormControl>
<DropzoneInput />
</FormControl>
<div className="flex items-center gap-6">
<DropzoneUploadIcon />
<div className="grid gap-0.5">
<DropzoneTitle>실제 파일들을 여기에 드롭하세요</DropzoneTitle>
<DropzoneDescription>
또는 클릭하여 파일들을 선택하세요
</DropzoneDescription>
</div>
</div>
</DropzoneZone>
</Dropzone>
{selectedFiles.length > 0 && (
<div className="mt-4 space-y-2">
<h6 className="text-sm font-semibold">
업로드된 파일 ({selectedFiles.length})
</h6>
<ScrollArea className="max-h-[200px]">
<FileList>
{selectedFiles.map((file, index) => (
<FileListItem key={index} className="p-3">
<FileListHeader>
<FileListIcon />
<FileListInfo>
<FileListName>{file.name}</FileListName>
<FileListSize>{prettyBytes(file.size)}</FileListSize>
</FileListInfo>
<FileListAction
onClick={() => removeFile(index)}
disabled={isUploading}
>
<X className="h-4 w-4" />
</FileListAction>
</FileListHeader>
</FileListItem>
))}
</FileList>
</ScrollArea>
</div>
)}
</CardContent>
</Card>
</div>
)}
{/* 3단계: 매칭 결과 검토 */}
{currentStep === 'review' && matchResult && (
<div className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<CheckCircle2 className="w-4 h-4" />
3단계: 매칭 결과 검토
</CardTitle>
</CardHeader>
<CardContent className="space-y-6">
{/* 통합된 매칭 결과 요약 */}
<div className="grid grid-cols-3 gap-4">
<div className="p-4 bg-green-50 border border-green-200 rounded-lg text-center">
<div className="text-2xl font-bold text-green-600">{matchResult.matched.length}</div>
<div className="text-sm text-green-700">매칭 성공</div>
</div>
<div className="p-4 bg-yellow-50 border border-yellow-200 rounded-lg text-center">
<div className="text-2xl font-bold text-yellow-600">{matchResult.unmatched.length}</div>
<div className="text-sm text-yellow-700">매칭 실패</div>
</div>
<div className="p-4 bg-red-50 border border-red-200 rounded-lg text-center">
<div className="text-2xl font-bold text-red-600">{matchResult.missingFiles.length}</div>
<div className="text-sm text-red-700">누락된 파일</div>
</div>
</div>
{/* 통합된 상세 결과 */}
<div className="border border-gray-200 rounded-lg overflow-hidden">
{/* 매칭 성공 섹션 */}
{matchResult.matched.length > 0 && (
<div className="border-b border-gray-200">
<div className="p-4 bg-green-50 flex items-center justify-between">
<h6 className="font-semibold text-green-700 flex items-center gap-2">
<CheckCircle2 className="w-4 h-4" />
매칭 성공 ({matchResult.matched.length}개)
</h6>
<Button
variant="ghost"
size="sm"
type="button"
onClick={(e) => {
e.preventDefault()
e.stopPropagation()
const element = document.getElementById('matched-details')
if (element) {
element.style.display = element.style.display === 'none' ? 'block' : 'none'
}
}}
>
{matchResult.matched.length <= 5 ? '모두보기' : '상세보기'}
</Button>
</div>
{/* 미리보기 */}
<div className="p-4 bg-green-25">
<div className="space-y-2">
{matchResult.matched.slice(0, 5).map((match, index) => (
<div key={index} className="flex items-center justify-between text-sm">
<span className="font-mono text-green-600 truncate max-w-[300px]" title={match.file.name}>
{match.file.name}
</span>
<span className="text-green-700 ml-4 whitespace-nowrap flex-shrink-0">
→ {match.item.docNumber} Rev.{match.item.revision}
</span>
</div>
))}
{matchResult.matched.length > 5 && (
<div className="text-gray-500 text-center text-sm py-2 border-t border-green-200">
... 외 {matchResult.matched.length - 5}개 (상세보기로 확인)
</div>
)}
</div>
{/* 펼침 상세 내용 */}
<div id="matched-details" style={{ display: 'none' }} className="mt-4 pt-4 border-t border-green-200">
<div className="max-h-64 overflow-y-auto">
<div className="space-y-2">
{matchResult.matched.map((match, index) => (
<div key={index} className="flex items-center justify-between text-sm py-1">
<span className="font-mono text-green-600 truncate max-w-[300px]" title={match.file.name}>
{match.file.name}
</span>
<span className="text-green-700 ml-4 whitespace-nowrap flex-shrink-0">
→ {match.item.docNumber} ({match.item.stage} Rev.{match.item.revision})
</span>
</div>
))}
</div>
</div>
</div>
</div>
</div>
)}
{/* 매칭 실패 섹션 */}
{matchResult.unmatched.length > 0 && (
<div className="border-b border-gray-200">
<div className="p-4 bg-yellow-50 flex items-center justify-between">
<h6 className="font-semibold text-yellow-700 flex items-center gap-2">
<AlertCircle className="w-4 h-4" />
매칭되지 않은 파일 ({matchResult.unmatched.length}개)
</h6>
<Button
variant="ghost"
size="sm"
type="button"
onClick={() => {
const element = document.getElementById('unmatched-details')
if (element) {
element.style.display = element.style.display === 'none' ? 'block' : 'none'
}
}}
>
상세보기
</Button>
</div>
<div className="p-4 bg-yellow-25">
<div className="space-y-1">
{matchResult.unmatched.slice(0, 3).map((file, index) => (
<div key={index} className="text-sm text-yellow-600 font-mono truncate max-w-full" title={file.name}>
{file.name}
</div>
))}
{matchResult.unmatched.length > 3 && (
<div className="text-gray-500 text-center text-sm py-2">
... 외 {matchResult.unmatched.length - 3}개
</div>
)}
</div>
<div id="unmatched-details" style={{ display: 'none' }} className="mt-4 pt-4 border-t border-yellow-200">
<div className="max-h-40 overflow-y-auto">
<div className="space-y-1">
{matchResult.unmatched.map((file, index) => (
<div key={index} className="text-sm text-yellow-600 font-mono truncate max-w-full" title={file.name}>
{file.name}
</div>
))}
</div>
</div>
</div>
</div>
</div>
)}
{/* 누락된 파일 섹션 */}
{matchResult.missingFiles.length > 0 && (
<div>
<div className="p-4 bg-red-50 flex items-center justify-between">
<h6 className="font-semibold text-red-700 flex items-center gap-2">
<X className="w-4 h-4" />
누락된 파일 ({matchResult.missingFiles.length}개)
</h6>
<Button
variant="ghost"
size="sm"
type="button"
onClick={() => {
const element = document.getElementById('missing-details')
if (element) {
element.style.display = element.style.display === 'none' ? 'block' : 'none'
}
}}
>
상세보기
</Button>
</div>
<div className="p-4 bg-red-25">
<div className="space-y-1">
{matchResult.missingFiles.slice(0, 3).map((fileName, index) => (
<div key={index} className="text-sm text-red-600 font-mono truncate max-w-full" title={fileName}>
{fileName}
</div>
))}
{matchResult.missingFiles.length > 3 && (
<div className="text-gray-500 text-center text-sm py-2">
... 외 {matchResult.missingFiles.length - 3}개
</div>
)}
</div>
<div id="missing-details" style={{ display: 'none' }} className="mt-4 pt-4 border-t border-red-200">
<div className="max-h-40 overflow-y-auto">
<div className="space-y-1">
{matchResult.missingFiles.map((fileName, index) => (
<div key={index} className="text-sm text-red-600 font-mono truncate max-w-full" title={fileName}>
{fileName}
</div>
))}
</div>
</div>
</div>
</div>
</div>
)}
</div>
{/* 업로드 불가 경고 */}
{!canProceedToUpload && (
<div className="p-4 bg-red-50 border border-red-200 rounded-lg">
<div className="flex items-center gap-2">
<AlertCircle className="w-5 h-5 text-red-600 flex-shrink-0" />
<span className="text-sm text-red-700">
누락된 파일이 있어 업로드를 진행할 수 없습니다. 누락된 파일들을 추가해주세요.
</span>
</div>
</div>
)}
</CardContent>
</Card>
{/* 추가 정보 입력 */}
<div className="grid grid-cols-1 gap-4">
<FormField
control={form.control}
name="uploaderName"
render={({ field }) => (
<FormItem>
<FormLabel>업로더명</FormLabel>
<FormControl>
<Input {...field} placeholder="업로더 이름" />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="comment"
render={({ field }) => (
<FormItem>
<FormLabel>코멘트 (선택)</FormLabel>
<FormControl>
<Textarea {...field} placeholder="일괄 업로드 코멘트" rows={2} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
)}
{/* 4단계: 업로드 진행 */}
{currentStep === 'upload' && (
<div className="space-y-4">
<Card>
<CardHeader>
<CardTitle className="text-lg flex items-center gap-2">
<Upload className="w-4 h-4" />
4단계: 업로드 진행중
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-4">
<div className="flex items-center gap-2">
<Loader2 className="h-4 w-4 animate-spin" />
<span className="text-sm">{uploadProgress}% 업로드 중...</span>
</div>
<div className="h-2 w-full bg-muted rounded-full overflow-hidden">
<div
className="h-full bg-primary rounded-full transition-all"
style={{ width: `${uploadProgress}%` }}
/>
</div>
{matchResult && (
<p className="text-sm text-gray-600">
{matchResult.matched.length}개 파일을 업로드하고 있습니다...
</p>
)}
</div>
</CardContent>
</Card>
</div>
)}
</form>
</Form>
</div>
{/* 고정 푸터 */}
<DialogFooter className="flex-shrink-0 pt-4 border-t bg-white">
<Button
type="button"
variant="outline"
onClick={handleDialogClose}
disabled={isUploading}
>
취소
</Button>
{currentStep === 'review' && (
<Button
type="submit"
disabled={!canProceedToUpload || isUploading}
onClick={form.handleSubmit(onSubmit)}
>
<Upload className="mr-2 h-4 w-4" />
일괄 업로드 ({matchResult?.matched.length || 0}개 파일)
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
)
}
|