summaryrefslogtreecommitdiff
path: root/lib/swp/table/swp-table-columns.tsx
blob: 14a8e002748ee92d90012c5b8c3aa5557d90a42e (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
"use client";

import React from "react";
import { ColumnDef } from "@tanstack/react-table";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Download, Loader2 } from "lucide-react";
import type { DocumentListItem } from "@/lib/swp/document-service";
import { toast } from "sonner";

export const swpDocumentColumns: ColumnDef<DocumentListItem>[] = [
  {
    accessorKey: "LTST_ACTV_STAT",
    header: "상태",
    cell: ({ row }) => {
      const status = row.original.LTST_ACTV_STAT;
      if (!status) return "-";

      const color =
        status.includes("Complete") ? "bg-green-100 text-green-800" :
        status.includes("Progress") ? "bg-blue-100 text-blue-800" :
        status.includes("Pending") || status.includes("Ready") ? "bg-yellow-100 text-yellow-800" :
        "bg-gray-100 text-gray-800";

      return (
        <Badge variant="outline" className={color}>
          {status}
        </Badge>
      );
    },
    size: 120,
  },
  {
    accessorKey: "OWN_DOC_NO",
    header: "OWN_DOC_NO",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.OWN_DOC_NO || "-"}</div>
    ),
    size: 250,
  },
  {
    accessorKey: "DOC_NO",
    header: "SHI DOC NO",
    cell: ({ row }) => (
      <div className="font-mono text-sm">{row.original.DOC_NO}</div>
    ),
    size: 250,
  },
  {
    accessorKey: "DOC_TITLE",
    header: "문서제목",
    cell: ({ row }) => (
      <div className="max-w-md truncate" title={row.original.DOC_TITLE}>
        {row.original.DOC_TITLE}
      </div>
    ),
    size: 300,
  },
  {
    accessorKey: "PROJ_NO",
    header: "프로젝트",
    cell: ({ row }) => (
      <div>
        <div className="font-medium">{row.original.PROJ_NO}</div>
        {row.original.PROJ_NM && (
          <div className="text-xs text-muted-foreground max-w-[150px] truncate">
            {row.original.PROJ_NM}
          </div>
        )}
      </div>
    ),
    size: 150,
  },
  {
    accessorKey: "PKG_NO",
    header: "패키지",
    cell: ({ row }) => row.original.PKG_NO || "-",
    size: 100,
  },
  {
    accessorKey: "VNDR_CD",
    header: "업체",
    cell: ({ row }) => (
      <div>
        {row.original.VNDR_CD && (
          <div className="text-xs text-muted-foreground">{row.original.VNDR_CD}</div>
        )}
        {row.original.CPY_NM && (
          <div className="text-sm truncate max-w-[120px]" title={row.original.CPY_NM}>
            {row.original.CPY_NM}
          </div>
        )}
      </div>
    ),
    size: 120,
  },
  {
    accessorKey: "STAGE",
    header: "최신 스테이지",
    cell: ({ row }) => {
      const stage = row.original.STAGE;
      if (!stage) return "-";

      const color =
        stage === "IFC" ? "bg-green-100 text-green-800" :
        stage === "IFA" ? "bg-blue-100 text-blue-800" :
        "bg-gray-100 text-gray-800";

      return (
        <Badge variant="outline" className={color}>
          {stage}
        </Badge>
      );
    },
    size: 80,
  },
  {
    accessorKey: "LTST_REV_NO",
    header: "최신 REV",
    cell: ({ row }) => row.original.LTST_REV_NO || "-",
    size: 80,
  },
  {
    id: "stats",
    header: "파일",
    cell: ({ row }) => (
      <div className="text-center">
        <div className="text-sm font-medium">
          {row.original.fileCount}개
        </div>
        {row.original.standbyFileCount > 0 && (
          <div className="text-xs text-yellow-600">
            대기중 {row.original.standbyFileCount}
          </div>
        )}
      </div>
    ),
    size: 100,
  },
  {
    id: "actions",
    header: "커버페이지 다운로드",
    cell: function ActionCell({ row }) {
      const [isDownloading, setIsDownloading] = React.useState(false);

      const handleDownloadCover = async (e: React.MouseEvent) => {
        e.stopPropagation(); // 행 클릭 이벤트 방지
        
        const docNumber = row.original.DOC_NO;
        const projectCode = row.original.PROJ_NO;

        if (!docNumber || !projectCode) {
          toast.error("문서 번호 또는 프로젝트 정보가 없습니다.");
          return;
        }

        setIsDownloading(true);

        try {
          // 1. 프로젝트 코드로 프로젝트 ID 조회
          const projectIdResponse = await fetch(
            `/api/projects/code-to-id?code=${encodeURIComponent(projectCode)}`
          );
          
          if (!projectIdResponse.ok) {
            toast.error("프로젝트 정보를 찾을 수 없습니다.");
            return;
          }

          const { projectId } = await projectIdResponse.json();

          // 2. 커버페이지 다운로드 API 호출
          const response = await fetch(
            `/api/projects/${projectId}/cover?docNumber=${encodeURIComponent(docNumber)}`
          );

          if (!response.ok) {
            const error = await response.json();
            throw new Error(error.message || "커버페이지 다운로드 실패");
          }

          // 3. 파일 다운로드
          const blob = await response.blob();
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement("a");
          a.href = url;
          a.download = `${docNumber}_cover.pdf`;
          document.body.appendChild(a);
          a.click();
          window.URL.revokeObjectURL(url);
          document.body.removeChild(a);

          toast.success("커버페이지 다운로드가 시작되었습니다.");

        } catch (error) {
          console.error("커버페이지 다운로드 오류:", error);
          toast.error(
            error instanceof Error 
              ? error.message 
              : "커버페이지 다운로드 중 오류가 발생했습니다."
          );
        } finally {
          setIsDownloading(false);
        }
      };

      return (
        <Button
          variant="ghost"
          size="sm"
          onClick={handleDownloadCover}
          disabled={isDownloading}
          className="h-8 w-8 p-0"
          title="커버페이지 다운로드"
        >
          {isDownloading ? (
            <Loader2 className="h-4 w-4 animate-spin" />
          ) : (
            <Download className="h-4 w-4" />
          )}
        </Button>
      );
    },
    size: 80,
  },
];