summaryrefslogtreecommitdiff
path: root/lib/vendor-investigation/table/vendor-details-dialog.tsx
blob: e5294d88f3095ab781dd1484435cbdf25b5547d8 (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
"use client"

import * as React from "react"
import { Building, Globe, Mail, MapPin, Phone, RefreshCw, Search } from "lucide-react"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogFooter,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Skeleton } from "@/components/ui/skeleton"
import { Badge } from "@/components/ui/badge"
import { Separator } from "@/components/ui/separator"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { useToast } from "@/hooks/use-toast"

// Import vendor service
import { getVendorById, getVendorItemsByVendorId } from "@/lib/vendor-investigation/service"
import { useRouter } from "next/navigation"

interface VendorDetailsDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
  vendorId: number | null
}

export function VendorDetailsDialog({
  open,
  onOpenChange,
  vendorId,
}: VendorDetailsDialogProps) {
  const { toast } = useToast()
  const router = useRouter()
  const [loading, setLoading] = React.useState(false)
  const [vendorData, setVendorData] = React.useState<any>(null)
  const [vendorItems, setVendorItems] = React.useState<any[]>([])
  const [activeTab, setActiveTab] = React.useState("details")

  // Fetch vendor details when the dialog opens
  React.useEffect(() => {
    if (open && vendorId) {
      setLoading(true)

      // Fetch vendor details
      Promise.all([
        getVendorById(vendorId),
        getVendorItemsByVendorId(vendorId)
      ])
        .then(([vendorDetails, items]) => {
          setVendorData(vendorDetails)
          setVendorItems(items || [])
        })
        .catch((error) => {
          console.error("Error fetching vendor data:", error)
          toast({
            title: "Error",
            description: "Failed to load vendor details. Please try again.",
            variant: "destructive",
          })
        })
        .finally(() => {
          setLoading(false)
        })
    } else {
      // Reset state when the dialog closes
      setVendorData(null)
      setVendorItems([])
    }
  }, [open, vendorId, toast])

  // Handle refresh button click
  const handleRefresh = () => {
    if (!vendorId) return

    setLoading(true)
    Promise.all([
      getVendorById(vendorId),
      getVendorItemsByVendorId(vendorId)
    ])
      .then(([vendorDetails, items]) => {
        setVendorData(vendorDetails)
        setVendorItems(items || [])
        toast({
          title: "Refreshed",
          description: "Vendor information has been refreshed.",
        })
      })
      .catch((error) => {
        console.error("Error refreshing vendor data:", error)
        toast({
          title: "Error",
          description: "Failed to refresh vendor details.",
          variant: "destructive",
        })
      })
      .finally(() => {
        setLoading(false)
      })
  }

  // Get vendor status badge variant
  const getStatusVariant = (status: string) => {
    switch (status?.toUpperCase()) {
      case "ACTIVE":
        return "default"
      case "PENDING":
        return "secondary"
      case "SUSPENDED":
        return "destructive"
      case "APPROVED":
        return "outline"
      default:
        return "secondary"
    }
  }

  // Navigate to full vendor profile page
  const navigateToVendorProfile = () => {
    if (!vendorId) return
    
    // Close dialog
    onOpenChange(false)
    
    // Navigate to vendor profile page with router
    router.push(`/evcp/vendors/${vendorId}/info`)
  }

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
      <DialogContent className="sm:max-w-[700px]">
        <DialogHeader>
          <div className="flex items-center justify-between">
            <DialogTitle>협력업체 상세정보</DialogTitle>
            <Button
              variant="outline"
              size="icon"
              onClick={handleRefresh}
              disabled={loading}
            >
              <RefreshCw className={`h-4 w-4 ${loading ? "animate-spin" : ""}`} />
              <span className="sr-only">새로고침</span>
            </Button>
          </div>
          <DialogDescription>
            협력업체 정보 상세보기
          </DialogDescription>
        </DialogHeader>

        {loading ? (
          <div className="space-y-4 py-4">
            <div className="flex items-center space-x-4">
              <Skeleton className="h-12 w-12 rounded-full" />
              <div className="space-y-2">
                <Skeleton className="h-4 w-[200px]" />
                <Skeleton className="h-4 w-[150px]" />
              </div>
            </div>
            <Skeleton className="h-[200px] w-full" />
          </div>
        ) : vendorData ? (
          <div className="py-4">
            {/* Vendor header with main info */}
            <div className="flex items-start justify-between mb-6">
              <div>
                <h2 className="text-xl font-semibold">{vendorData.name}</h2>
                <div className="flex items-center mt-1 space-x-2">
                  <span className="text-sm text-muted-foreground">업체코드: {vendorData.code}</span>
                  {vendorData.taxId && (
                    <>
                      <span className="text-muted-foreground">•</span>
                      <span className="text-sm text-muted-foreground">사업자등록번호: {vendorData.taxId}</span>
                    </>
                  )}
                </div>
              </div>
              {vendorData.status && (
                <Badge variant={getStatusVariant(vendorData.status)}>
                  {vendorData.status}
                </Badge>
              )}
            </div>

            <Tabs defaultValue="details" onValueChange={setActiveTab}>
              <TabsList className="mb-4">
                <TabsTrigger value="details">상세</TabsTrigger>
                <TabsTrigger value="items">공급품목({vendorItems.length})</TabsTrigger>
              </TabsList>

              {/* Details Tab */}
              <TabsContent value="details" className="space-y-4">
                {/* Contact Information Card */}
                <Card>
                  <CardHeader className="pb-2">
                    <CardTitle className="text-base">연락처 정보</CardTitle>
                  </CardHeader>
                  <CardContent className="space-y-2">
                    <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                      {/* Email */}
                      <div className="flex items-center space-x-2">
                        <Mail className="h-4 w-4 text-muted-foreground" />
                        <span className="text-sm">{vendorData.email || "No email provided"}</span>
                      </div>
                      
                      {/* Phone */}
                      <div className="flex items-center space-x-2">
                        <Phone className="h-4 w-4 text-muted-foreground" />
                        <span className="text-sm">{vendorData.phone || "No phone provided"}</span>
                      </div>
                      
                      {/* Website */}
                      {vendorData.website && (
                        <div className="flex items-center space-x-2">
                          <Globe className="h-4 w-4 text-muted-foreground" />
                          <a 
                            href={vendorData.website.startsWith('http') ? vendorData.website : `https://${vendorData.website}`}
                            target="_blank"
                            rel="noopener noreferrer"
                            className="text-sm text-blue-600 hover:underline"
                          >
                            {vendorData.website}
                          </a>
                        </div>
                      )}
                      
                      {/* Address */}
                      {vendorData.address && (
                        <div className="flex items-center space-x-2">
                          <MapPin className="h-4 w-4 text-muted-foreground" />
                          <span className="text-sm">{vendorData.address}</span>
                        </div>
                      )}
                      
                      {/* Country */}
                      {vendorData.country && (
                        <div className="flex items-center space-x-2">
                          <Building className="h-4 w-4 text-muted-foreground" />
                          <span className="text-sm">{vendorData.country}</span>
                        </div>
                      )}
                    </div>
                  </CardContent>
                </Card>

                {/* Additional Information */}
                {vendorData.description && (
                  <Card>
                    <CardHeader className="pb-2">
                      <CardTitle className="text-base">협력업체 설명</CardTitle>
                    </CardHeader>
                    <CardContent>
                      <p className="text-sm">{vendorData.description}</p>
                    </CardContent>
                  </Card>
                )}

                {/* Registration Information */}
                <Card>
                  <CardHeader className="pb-2">
                    <CardTitle className="text-base">등록 정보</CardTitle>
                  </CardHeader>
                  <CardContent>
                    <div className="grid grid-cols-2 gap-2">
                      <div>
                        <p className="text-xs text-muted-foreground">협력업체 생성일</p>
                        <p className="text-sm">
                          {vendorData.createdAt 
                            ? new Date(vendorData.createdAt).toLocaleDateString() 
                            : "Unknown"
                          }
                        </p>
                      </div>
                      <div>
                        <p className="text-xs text-muted-foreground">협력업체 정보 업데이트일</p>
                        <p className="text-sm">
                          {vendorData.updatedAt 
                            ? new Date(vendorData.updatedAt).toLocaleDateString() 
                            : "Unknown"
                          }
                        </p>
                      </div>
                    </div>
                  </CardContent>
                </Card>
              </TabsContent>

              {/* Items Tab */}
              <TabsContent value="items">
                <ScrollArea className="h-[300px] pr-4">
                  {vendorItems.length > 0 ? (
                    <div className="space-y-4">
                      {vendorItems.map((item) => (
                        <Card key={item.id}>
                          <CardHeader className="pb-2">
                            <CardTitle className="text-base">{item.itemName}</CardTitle>
                            <CardDescription>Code: {item.itemCode}</CardDescription>
                          </CardHeader>
                          {item.description && (
                            <CardContent>
                              <p className="text-sm">{item.description}</p>
                            </CardContent>
                          )}
                        </Card>
                      ))}
                    </div>
                  ) : (
                    <div className="flex flex-col items-center justify-center h-full text-center p-8">
                      <Search className="h-8 w-8 text-muted-foreground mb-2" />
                      <h3 className="text-lg font-semibold">No items found</h3>
                      <p className="text-sm text-muted-foreground">해당 업체는 아직 공급품목이 등록되지 않았습니다.</p>
                    </div>
                  )}
                </ScrollArea>
              </TabsContent>

            </Tabs>
          </div>
        ) : (
          <div className="py-6 text-center">
            <p className="text-muted-foreground">No vendor information available</p>
          </div>
        )}

        <DialogFooter>
          <Button variant="outline" onClick={() => onOpenChange(false)}>
            닫기
          </Button>
          {vendorData && (
            <Button onClick={navigateToVendorProfile}>
              전체 정보 보러가기
            </Button>
          )}
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}