summaryrefslogtreecommitdiff
path: root/lib/basic-contract/gtc-vendor/import-excel-dialog.tsx
blob: f37566fc8cb42413375001afd600ddd32139c8d1 (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
"use client"

import * as React from "react"
import { Upload, Download, FileText, AlertCircle, CheckCircle2, X } from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import { Badge } from "@/components/ui/badge"
import { Alert, AlertDescription } from "@/components/ui/alert"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Separator } from "@/components/ui/separator"

import { type ExcelColumnDef } from "@/lib/export"
import { downloadExcelTemplate, parseExcelFile } from "./excel-import"
import { type GtcClauseTreeView } from "@/db/schema/gtc"
import { toast } from "@/hooks/use-toast"

interface ImportExcelDialogProps {
  documentId: number
  columns: ExcelColumnDef[]
  onSuccess?: () => void
  onImport?: (data: Partial<GtcClauseTreeView>[]) => Promise<void>
  trigger?: React.ReactNode
}

type ImportStep = "upload" | "preview" | "importing" | "complete"

export function ImportExcelDialog({
  documentId,
  columns,
  onSuccess,
  onImport,
  trigger,
}: ImportExcelDialogProps) {
  const [open, setOpen] = React.useState(false)
  const [step, setStep] = React.useState<ImportStep>("upload")
  const [selectedFile, setSelectedFile] = React.useState<File | null>(null)
  const [parsedData, setParsedData] = React.useState<Partial<GtcClauseTreeView>[]>([])
  const [errors, setErrors] = React.useState<string[]>([])
  const [isProcessing, setIsProcessing] = React.useState(false)
  const fileInputRef = React.useRef<HTMLInputElement>(null)

  // 다이얼로그 열기/닫기 시 상태 초기화
  const handleOpenChange = (isOpen: boolean) => {
    setOpen(isOpen)
    if (!isOpen) {
      // 다이얼로그 닫을 때 상태 초기화
      setStep("upload")
      setSelectedFile(null)
      setParsedData([])
      setErrors([])
      setIsProcessing(false)
      if (fileInputRef.current) {
        fileInputRef.current.value = ""
      }
    }
  }

  // 템플릿 다운로드
  const handleDownloadTemplate = async () => {
    try {
      await downloadExcelTemplate(columns, {
        filename: `gtc-clauses-template-${new Date().toISOString().split('T')[0]}`,
        includeExampleData: true,
        useGroupHeader: true,
      })
      
      toast({
        title: "템플릿 다운로드 완료",
        description: "Excel 템플릿이 다운로드되었습니다. 템플릿에 데이터를 입력한 후 업로드해주세요.",
      })
    } catch (error) {
      toast({
        title: "템플릿 다운로드 실패",
        description: "템플릿 다운로드 중 오류가 발생했습니다.",
        variant: "destructive",
      })
    }
  }

  // 파일 선택
  const handleFileSelect = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files?.[0]
    if (file) {
      if (file.type !== "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" && 
          file.type !== "application/vnd.ms-excel") {
        toast({
          title: "잘못된 파일 형식",
          description: "Excel 파일(.xlsx, .xls)만 업로드할 수 있습니다.",
          variant: "destructive",
        })
        return
      }
      setSelectedFile(file)
    }
  }

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

    setIsProcessing(true)
    try {
      const result = await parseExcelFile<GtcClauseTreeView>(
        selectedFile,
        columns,
        {
          hasGroupHeader: true,
          sheetName: "GTC조항템플릿",
        }
      )

      setParsedData(result.data)
      setErrors(result.errors)

      if (result.errors.length > 0) {
        toast({
          title: "파싱 완료 (오류 있음)",
          description: `${result.data.length}개의 행을 파싱했지만 ${result.errors.length}개의 오류가 있습니다.`,
          variant: "destructive",
        })
      } else {
        toast({
          title: "파싱 완료",
          description: `${result.data.length}개의 행이 성공적으로 파싱되었습니다.`,
        })
      }

      setStep("preview")
    } catch (error) {
      toast({
        title: "파싱 실패",
        description: "파일 파싱 중 오류가 발생했습니다.",
        variant: "destructive",
      })
    } finally {
      setIsProcessing(false)
    }
  }

  // 데이터 가져오기 실행
  const handleImportData = async () => {
    if (parsedData.length === 0 || !onImport) return

    setStep("importing")
    try {
      await onImport(parsedData)
      setStep("complete")
      
      toast({
        title: "가져오기 완료",
        description: `${parsedData.length}개의 조항이 성공적으로 가져와졌습니다.`,
      })

      // 성공 콜백 호출 후 잠시 후 다이얼로그 닫기
      setTimeout(() => {
        onSuccess?.()
        setOpen(false)
      }, 2000)
    } catch (error) {
      toast({
        title: "가져오기 실패",
        description: "데이터 가져오기 중 오류가 발생했습니다.",
        variant: "destructive",
      })
      setStep("preview")
    }
  }

  const renderUploadStep = () => (
    <div className="space-y-6">
      <div className="text-center">
        <FileText className="mx-auto h-12 w-12 text-muted-foreground" />
        <h3 className="mt-4 text-lg font-semibold">Excel 파일로 조항 가져오기</h3>
        <p className="mt-2 text-sm text-muted-foreground">
          먼저 템플릿을 다운로드하여 데이터를 입력한 후 업로드해주세요.
        </p>
      </div>

      <div className="space-y-4">
        <div>
          <Button 
            onClick={handleDownloadTemplate}
            variant="outline"
            className="w-full"
          >
            <Download className="mr-2 h-4 w-4" />
            Excel 템플릿 다운로드
          </Button>
          <p className="mt-2 text-xs text-muted-foreground">
            템플릿에는 입력 가이드와 예시 데이터가 포함되어 있습니다.
          </p>
        </div>

        <Separator />

        <div>
          <input
            ref={fileInputRef}
            type="file"
            accept=".xlsx,.xls"
            onChange={handleFileSelect}
            className="hidden"
          />
          <Button
            onClick={() => fileInputRef.current?.click()}
            variant={selectedFile ? "secondary" : "outline"}
            className="w-full"
          >
            <Upload className="mr-2 h-4 w-4" />
            {selectedFile ? selectedFile.name : "Excel 파일 선택"}
          </Button>
        </div>

        {selectedFile && (
          <Button
            onClick={handleParseFile}
            disabled={isProcessing}
            className="w-full"
          >
            {isProcessing ? "파싱 중..." : "파일 분석하기"}
          </Button>
        )}
      </div>
    </div>
  )

  const renderPreviewStep = () => (
    <div className="space-y-4">
      <div className="flex items-center justify-between">
        <h3 className="text-lg font-semibold">데이터 미리보기</h3>
        <div className="flex items-center gap-2">
          <Badge variant="secondary">
            {parsedData.length}개 행
          </Badge>
          {errors.length > 0 && (
            <Badge variant="destructive">
              {errors.length}개 오류
            </Badge>
          )}
        </div>
      </div>

      {errors.length > 0 && (
        <Alert variant="destructive">
          <AlertCircle className="h-4 w-4" />
          <AlertDescription>
            <div className="space-y-1">
              <div className="font-medium">다음 오류들을 확인해주세요:</div>
              <ul className="list-disc list-inside space-y-1 text-sm">
                {errors.slice(0, 5).map((error, index) => (
                  <li key={index}>{error}</li>
                ))}
                {errors.length > 5 && (
                  <li>... 및 {errors.length - 5}개 추가 오류</li>
                )}
              </ul>
            </div>
          </AlertDescription>
        </Alert>
      )}

      <ScrollArea className="h-[300px] border rounded-md">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead className="w-12">#</TableHead>
              <TableHead>채번</TableHead>
              <TableHead>소제목</TableHead>
              <TableHead>상세항목</TableHead>
              <TableHead>분류</TableHead>
              <TableHead>상태</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {parsedData.map((item, index) => (
              <TableRow key={index}>
                <TableCell>{index + 1}</TableCell>
                <TableCell className="font-mono">
                  {item.itemNumber || "-"}
                </TableCell>
                <TableCell className="max-w-[200px] truncate">
                  {item.subtitle || "-"}
                </TableCell>
                <TableCell className="max-w-[300px] truncate">
                  {item.content || "-"}
                </TableCell>
                <TableCell>{item.category || "-"}</TableCell>
                <TableCell>
                  <Badge variant={item.isActive ? "default" : "secondary"}>
                    {item.isActive ? "활성" : "비활성"}
                  </Badge>
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </ScrollArea>

      <div className="flex gap-2">
        <Button
          variant="outline"
          onClick={() => setStep("upload")}
          className="flex-1"
        >
          다시 선택
        </Button>
        <Button
          onClick={handleImportData}
          disabled={parsedData.length === 0 || errors.length > 0}
          className="flex-1"
        >
          {errors.length > 0 ? "오류 수정 후 가져오기" : `${parsedData.length}개 조항 가져오기`}
        </Button>
      </div>
    </div>
  )

  const renderImportingStep = () => (
    <div className="text-center py-8">
      <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-primary mx-auto"></div>
      <h3 className="mt-4 text-lg font-semibold">가져오는 중...</h3>
      <p className="mt-2 text-sm text-muted-foreground">
        {parsedData.length}개의 조항을 데이터베이스에 저장하고 있습니다.
      </p>
    </div>
  )

  const renderCompleteStep = () => (
    <div className="text-center py-8">
      <CheckCircle2 className="mx-auto h-12 w-12 text-green-500" />
      <h3 className="mt-4 text-lg font-semibold">가져오기 완료!</h3>
      <p className="mt-2 text-sm text-muted-foreground">
        {parsedData.length}개의 조항이 성공적으로 가져와졌습니다.
      </p>
    </div>
  )

  return (
    <Dialog open={open} onOpenChange={handleOpenChange}>
      <DialogTrigger asChild>
        {trigger || (
          <Button variant="outline" size="sm">
            <Upload className="mr-2 h-4 w-4" />
            Excel에서 가져오기
          </Button>
        )}
      </DialogTrigger>
      <DialogContent className="max-w-4xl max-h-[80vh] overflow-hidden">
        <DialogHeader>
          <DialogTitle>Excel에서 조항 가져오기</DialogTitle>
          <DialogDescription>
            Excel 파일을 사용하여 여러 조항을 한 번에 가져올 수 있습니다.
          </DialogDescription>
        </DialogHeader>
        
        <div className="mt-4">
          {step === "upload" && renderUploadStep()}
          {step === "preview" && renderPreviewStep()}
          {step === "importing" && renderImportingStep()}
          {step === "complete" && renderCompleteStep()}
        </div>
      </DialogContent>
    </Dialog>
  )
}