summaryrefslogtreecommitdiff
path: root/lib/esg-check-list/table/esg-excel-import.tsx
blob: 0990e0e8ee65633be75ab752b8a344bd09602080 (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
"use client"

import * as React from "react"
import { toast } from "sonner"
import { useTransition } from "react"
import { Upload, FileSpreadsheet, AlertCircle, CheckCircle, X } from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
} from "@/components/ui/tabs"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Checkbox } from "@/components/ui/checkbox"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Badge } from "@/components/ui/badge"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"

import { 
  parseEsgExcelFile, 
  validateExcelData, 
  type ParsedExcelData 
} from "./excel-utils"
import { 
  importEsgDataFromExcel, 
  checkDuplicateSerials,
  type ImportOptions 
} from "./excel-actions"

interface ExcelImportDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  onSuccess: () => void
}

export function ExcelImportDialog({
  open,
  onOpenChange,
  onSuccess,
}: ExcelImportDialogProps) {
  const [isPending, startTransition] = useTransition()
  const [file, setFile] = React.useState<File | null>(null)
  const [parsedData, setParsedData] = React.useState<ParsedExcelData | null>(null)
  const [validationErrors, setValidationErrors] = React.useState<string[]>([])
  const [duplicateSerials, setDuplicateSerials] = React.useState<string[]>([])
  const [currentStep, setCurrentStep] = React.useState<'upload' | 'preview' | 'options'>('upload')
  
  // 임포트 옵션
  const [importOptions, setImportOptions] = React.useState<ImportOptions>({
    skipDuplicates: false,
    updateExisting: false,
  })

  // 파일 선택 처리
  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const selectedFile = event.target.files?.[0]
    if (selectedFile) {
      if (!selectedFile.name.endsWith('.xlsx') && !selectedFile.name.endsWith('.xls')) {
        toast.error('Excel 파일(.xlsx, .xls)만 업로드 가능합니다.')
        return
      }
      setFile(selectedFile)
    }
  }

  // 파일 파싱
  const handleParseFile = async () => {
    if (!file) return

    startTransition(async () => {
      try {
        const data = await parseEsgExcelFile(file)
        setParsedData(data)

        // 검증
        const errors = validateExcelData(data)
        setValidationErrors(errors)

        // 중복 확인
        const serials = data.evaluations.map(e => e.serialNumber)
        const duplicates = await checkDuplicateSerials(serials)
        setDuplicateSerials(duplicates)

        setCurrentStep('preview')
      } catch (error) {
        console.error('Parsing error:', error)
        toast.error(error instanceof Error ? error.message : 'Excel 파일 파싱에 실패했습니다.')
      }
    })
  }

  // 임포트 실행
  const handleImport = async () => {
    if (!parsedData) return

    startTransition(async () => {
      try {
        const result = await importEsgDataFromExcel(parsedData, importOptions)
        
        if (result.success) {
          toast.success(result.message)
          onSuccess()
          onOpenChange(false)
        } else {
          toast.error(result.message)
        }

        // 상세 결과가 있으면 콘솔에 출력
        if (result.details.errors.length > 0) {
          console.warn('Import errors:', result.details.errors)
        }
      } catch (error) {
        console.error('Import error:', error)
        toast.error('임포트 중 오류가 발생했습니다.')
      }
    })
  }

  // 다이얼로그 닫기 시 상태 리셋
  const handleClose = () => {
    setFile(null)
    setParsedData(null)
    setValidationErrors([])
    setDuplicateSerials([])
    setCurrentStep('upload')
    setImportOptions({ skipDuplicates: false, updateExisting: false })
    onOpenChange(false)
  }

  const canProceed = parsedData && validationErrors.length === 0
  const hasDuplicates = duplicateSerials.length > 0

  return (
    <Dialog open={open} onOpenChange={handleClose}>
      <DialogContent className="max-w-4xl max-h-[80vh]  flex flex-col" style={{maxWidth:900, width:900}}>
      <DialogHeader className="flex-shrink-0">
          <DialogTitle className="flex items-center gap-2">
            <FileSpreadsheet className="w-5 h-5" />
            Excel 데이터 임포트
          </DialogTitle>
          <DialogDescription>
            Excel 파일에서 ESG 평가표 데이터를 임포트합니다.
          </DialogDescription>
        </DialogHeader>

        <div className="flex-1 overflow-y-auto px-1">
        <Tabs value={currentStep} className="w-full">
          <TabsList className="grid w-full grid-cols-3">
            <TabsTrigger value="upload">파일 업로드</TabsTrigger>
            <TabsTrigger value="preview" disabled={!parsedData}>데이터 미리보기</TabsTrigger>
            <TabsTrigger value="options" disabled={!canProceed}>임포트 옵션</TabsTrigger>
          </TabsList>

          {/* 파일 업로드 탭 */}
          <TabsContent value="upload" className="space-y-4">
            <div className="space-y-4">
              <div>
                <Label htmlFor="excel-file">Excel 파일 선택</Label>
                <Input
                  id="excel-file"
                  type="file"
                  accept=".xlsx,.xls"
                  onChange={handleFileChange}
                  className="mt-1"
                />
              </div>

              {file && (
                <div className="p-4 border rounded-lg bg-muted/50">
                  <div className="flex items-center gap-2">
                    <FileSpreadsheet className="w-4 h-4" />
                    <span className="font-medium">{file.name}</span>
                    <Badge variant="outline">
                      {(file.size / 1024).toFixed(1)} KB
                    </Badge>
                  </div>
                </div>
              )}

              <Button
                onClick={handleParseFile}
                disabled={!file || isPending}
                className="w-full"
              >
                {isPending ? '파싱 중...' : '파일 분석하기'}
              </Button>
            </div>
          </TabsContent>

          {/* 데이터 미리보기 탭 */}
          <TabsContent value="preview" className="space-y-4">
            {parsedData && (
              <div className="space-y-4">
                {/* 검증 결과 */}
                <div className="space-y-2">
                  {validationErrors.length > 0 ? (
                    <div className="p-4 border border-destructive/20 rounded-lg bg-destructive/10">
                      <div className="flex items-center gap-2 mb-2">
                        <AlertCircle className="w-4 h-4 text-destructive" />
                        <span className="font-medium text-destructive">검증 오류</span>
                      </div>
                      <ul className="space-y-1">
                        {validationErrors.map((error, index) => (
                          <li key={index} className="text-sm text-destructive">
                            • {error}
                          </li>
                        ))}
                      </ul>
                    </div>
                  ) : (
                    <div className="p-4 border border-green-200 rounded-lg bg-green-50">
                      <div className="flex items-center gap-2">
                        <CheckCircle className="w-4 h-4 text-green-600" />
                        <span className="font-medium text-green-800">검증 완료</span>
                      </div>
                    </div>
                  )}

                  {/* 중복 알림 */}
                  {hasDuplicates && (
                    <div className="p-4 border border-yellow-200 rounded-lg bg-yellow-50">
                      <div className="flex items-center gap-2 mb-2">
                        <AlertCircle className="w-4 h-4 text-yellow-600" />
                        <span className="font-medium text-yellow-800">중복 데이터 발견</span>
                      </div>
                      <p className="text-sm text-yellow-700 mb-2">
                        다음 시리얼번호가 이미 존재합니다:
                      </p>
                      <div className="flex flex-wrap gap-1">
                        {duplicateSerials.map(serial => (
                          <Badge key={serial} variant="outline" className="text-yellow-800">
                            {serial}
                          </Badge>
                        ))}
                      </div>
                    </div>
                  )}
                </div>

                {/* 데이터 요약 */}
                <div className="grid grid-cols-3 gap-4">
                  <div className="p-4 border rounded-lg text-center">
                    <div className="text-2xl font-bold text-blue-600">
                      {parsedData.evaluations.length}
                    </div>
                    <div className="text-sm text-muted-foreground">평가표</div>
                  </div>
                  <div className="p-4 border rounded-lg text-center">
                    <div className="text-2xl font-bold text-green-600">
                      {parsedData.evaluationItems.length}
                    </div>
                    <div className="text-sm text-muted-foreground">평가항목</div>
                  </div>
                  <div className="p-4 border rounded-lg text-center">
                    <div className="text-2xl font-bold text-purple-600">
                      {parsedData.answerOptions.length}
                    </div>
                    <div className="text-sm text-muted-foreground">답변옵션</div>
                  </div>
                </div>

                {/* 평가표 미리보기 */}
                <div>
                  <h4 className="font-medium mb-2">평가표 미리보기</h4>
                  <ScrollArea className="h-[200px] border rounded-lg">
                    <Table>
                      <TableHeader>
                        <TableRow>
                          <TableHead>시리얼번호</TableHead>
                          <TableHead>분류</TableHead>
                          <TableHead>점검항목</TableHead>
                        </TableRow>
                      </TableHeader>
                      <TableBody>
                        {parsedData.evaluations.slice(0, 10).map((evaluation, index) => (
                          <TableRow key={index}>
                            <TableCell className="font-medium">
                              {evaluation.serialNumber}
                              {duplicateSerials.includes(evaluation.serialNumber) && (
                                <Badge variant="destructive" className="ml-2 text-xs">
                                  중복
                                </Badge>
                              )}
                            </TableCell>
                            <TableCell>{evaluation.category}</TableCell>
                            <TableCell className="max-w-[200px] truncate">
                              {evaluation.inspectionItem}
                            </TableCell>
                          </TableRow>
                        ))}
                      </TableBody>
                    </Table>
                  </ScrollArea>
                  {parsedData.evaluations.length > 10 && (
                    <p className="text-sm text-muted-foreground mt-2">
                      ...외 {parsedData.evaluations.length - 10}개 더
                    </p>
                  )}
                </div>

                {canProceed && (
                  <Button 
                    onClick={() => setCurrentStep('options')}
                    className="w-full"
                  >
                    다음 단계
                  </Button>
                )}
              </div>
            )}
          </TabsContent>

          {/* 임포트 옵션 탭 */}
          <TabsContent value="options" className="space-y-4">
            <div className="space-y-4">
              <h4 className="font-medium">임포트 옵션</h4>
              
              {hasDuplicates && (
                <div className="space-y-3">
                  <div className="flex items-center space-x-2">
                    <Checkbox
                      id="skip-duplicates"
                      checked={importOptions.skipDuplicates}
                      onCheckedChange={(checked) =>
                        setImportOptions(prev => ({
                          ...prev,
                          skipDuplicates: !!checked,
                          updateExisting: false, // 상호 배타적
                        }))
                      }
                    />
                    <Label htmlFor="skip-duplicates" className="text-sm">
                      중복 데이터 건너뛰기
                    </Label>
                  </div>
                  
                  <div className="flex items-center space-x-2">
                    <Checkbox
                      id="update-existing"
                      checked={importOptions.updateExisting}
                      onCheckedChange={(checked) =>
                        setImportOptions(prev => ({
                          ...prev,
                          updateExisting: !!checked,
                          skipDuplicates: false, // 상호 배타적
                        }))
                      }
                    />
                    <Label htmlFor="update-existing" className="text-sm">
                      기존 데이터 업데이트 (덮어쓰기)
                    </Label>
                  </div>

                  <div className="p-3 border border-yellow-200 rounded-lg bg-yellow-50 text-sm">
                    <p className="text-yellow-800">
                      <strong>주의:</strong> 기존 데이터 업데이트를 선택하면 해당 평가표의 모든 평가항목과 답변옵션이 교체됩니다.
                    </p>
                  </div>
                </div>
              )}

              <Button 
                onClick={handleImport}
                disabled={isPending || (hasDuplicates && !importOptions.skipDuplicates && !importOptions.updateExisting)}
                className="w-full"
              >
                {isPending ? '임포트 중...' : '데이터 임포트 실행'}
              </Button>
            </div>
          </TabsContent>
        </Tabs>
        </div>
        <DialogFooter  className="flex-shrink-0">
          <Button variant="outline" onClick={handleClose}>
            취소
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}