summaryrefslogtreecommitdiff
path: root/lib/vendors/table/request-vendor-investigate-dialog.tsx
blob: a0d841280b7d6743f6bd54a4a2bc3e9acfaf8b46 (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
"use client"

import * as React from "react"
import { type Row } from "@tanstack/react-table"
import { Loader, Check, SendHorizonal, AlertCircle, AlertTriangle } from "lucide-react"
import { toast } from "sonner"

import { useMediaQuery } from "@/hooks/use-media-query"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerFooter,
  DrawerHeader,
  DrawerTitle,
  DrawerTrigger,
} from "@/components/ui/drawer"
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table"
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/components/ui/accordion"
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Badge } from "@/components/ui/badge"
import { Separator } from "@/components/ui/separator"

import { Vendor } from "@/db/schema/vendors"
import { requestInvestigateVendors, getExistingInvestigationsForVendors } from "@/lib/vendor-investigation/service"
import { useSession } from "next-auth/react"
import { formatDate } from "@/lib/utils"

interface ApprovalVendorDialogProps
  extends React.ComponentPropsWithoutRef<typeof Dialog> {
  vendors: Row<Vendor>["original"][]
  showTrigger?: boolean
  onSuccess?: () => void
}

// Helper function to get status badge variant and text
function getStatusBadge(status: string) {
  switch (status) {
    case "REQUESTED":
      return { variant: "secondary", text: "Requested" }
    case "SCHEDULED":
      return { variant: "warning", text: "Scheduled" }
    case "IN_PROGRESS":
      return { variant: "default", text: "In Progress" }
    case "COMPLETED":
      return { variant: "success", text: "Completed" }
    case "CANCELLED":
      return { variant: "destructive", text: "Cancelled" }
    default:
      return { variant: "outline", text: status }
  }
}

export function RequestVendorsInvestigateDialog({
  vendors,
  showTrigger = true,
  onSuccess,
  ...props
}: ApprovalVendorDialogProps) {
  const [isApprovePending, startApproveTransition] = React.useTransition()
  const [isLoading, setIsLoading] = React.useState(true)
  const [existingInvestigations, setExistingInvestigations] = React.useState<any[]>([])
  const isDesktop = useMediaQuery("(min-width: 640px)")
  const { data: session } = useSession()

  // Fetch existing investigations when dialog opens
  React.useEffect(() => {
    if (vendors.length > 0) {
      setIsLoading(true)
      const fetchExistingInvestigations = async () => {
        try {
          const vendorIds = vendors.map(vendor => vendor.id)
          const result = await getExistingInvestigationsForVendors(vendorIds)
          setExistingInvestigations(result)
        } catch (error) {
          console.error("Failed to fetch existing investigations:", error)
          toast.error("Failed to fetch existing investigations")
        } finally {
          setIsLoading(false)
        }
      }
      
      fetchExistingInvestigations()
    }
  }, [vendors])

  // Group vendors by investigation status
  const vendorsWithInvestigations = React.useMemo(() => {
    if (!existingInvestigations.length) return { withInvestigations: [], withoutInvestigations: vendors }
    
    const vendorMap = new Map(vendors.map(v => [v.id, v]))
    const withInvestigations: Array<{ vendor: typeof vendors[0], investigation: any }> = []
    
    // Find vendors with existing investigations
    existingInvestigations.forEach(inv => {
      const vendor = vendorMap.get(inv.vendorId)
      if (vendor) {
        withInvestigations.push({ vendor, investigation: inv })
        vendorMap.delete(inv.vendorId)
      }
    })
    
    // Remaining vendors don't have investigations
    const withoutInvestigations = Array.from(vendorMap.values())
    
    return { withInvestigations, withoutInvestigations }
  }, [vendors, existingInvestigations])

  function onApprove() {
    if (!session?.user?.id) {
      toast.error("사용자 인증 정보를 찾을 수 없습니다.")
      return
    }
    
    // Only request investigations for vendors without existing ones
    const vendorsToRequest = vendorsWithInvestigations.withoutInvestigations
    
    if (vendorsToRequest.length === 0) {
      toast.info("모든 선택된 업체에 이미 실사 요청이 있습니다.")
      props.onOpenChange?.(false)
      return
    }
    
    startApproveTransition(async () => {
      const { error } = await requestInvestigateVendors({
        ids: vendorsToRequest.map((vendor) => vendor.id),
        userId: Number(session.user.id)
      })

      if (error) {
        toast.error(error)
        return
      }

      props.onOpenChange?.(false)
      toast.success(`${vendorsToRequest.length}개 업체에 대한 실사 요청을 보냈습니다.`)
      onSuccess?.()
    })
  }

  const renderContent = () => {
    return (
      <>
        <div className="space-y-4">
          {isLoading ? (
            <div className="flex items-center justify-center py-4">
              <Loader className="size-6 animate-spin text-muted-foreground" />
            </div>
          ) : (
            <>
              {vendorsWithInvestigations.withInvestigations.length > 0 && (
                <Alert>
                  <AlertTriangle className="h-4 w-4" />
                  <AlertTitle>기존 실사 요청 정보가 있습니다</AlertTitle>
                  <AlertDescription>
                    선택한 {vendors.length}개 업체 중 {vendorsWithInvestigations.withInvestigations.length}개 업체에 대한 
                    기존 실사 요청이 있습니다. 새로운 요청은 기존 데이터가 없는 업체에만 적용됩니다.
                  </AlertDescription>
                </Alert>
              )}

              {vendorsWithInvestigations.withInvestigations.length > 0 && (
                <Accordion type="single" collapsible className="w-full">
                  <AccordionItem value="existing-investigations">
                    <AccordionTrigger className="font-medium">
                      기존 실사 요청 ({vendorsWithInvestigations.withInvestigations.length})
                    </AccordionTrigger>
                    <AccordionContent>
                      <ScrollArea className="max-h-[200px]">
                        <Table>
                          <TableHeader>
                            <TableRow>
                              <TableHead>업체명</TableHead>
                              <TableHead>상태</TableHead>
                              <TableHead>요청일</TableHead>
                              <TableHead>예정 일정</TableHead>
                            </TableRow>
                          </TableHeader>
                          <TableBody>
                            {vendorsWithInvestigations.withInvestigations.map(({ vendor, investigation }) => {
                              const status = getStatusBadge(investigation.investigationStatus)
                              return (
                                <TableRow key={investigation.investigationId}>
                                  <TableCell className="font-medium">{vendor.vendorName}</TableCell>
                                  <TableCell>
                                    <Badge variant={status.variant as any}>{status.text}</Badge>
                                  </TableCell>
                                  <TableCell>{formatDate(investigation.createdAt, "KR")}</TableCell>
                                  <TableCell>
                                    {investigation.scheduledStartAt 
                                      ? formatDate(investigation.scheduledStartAt, "KR")
                                      : "미정"}
                                  </TableCell>
                                </TableRow>
                              )
                            })}
                          </TableBody>
                        </Table>
                      </ScrollArea>
                    </AccordionContent>
                  </AccordionItem>
                </Accordion>
              )}

              <div>
                <h3 className="text-sm font-medium mb-2">
                  새로운 실사가 요청될 업체 ({vendorsWithInvestigations.withoutInvestigations.length})
                </h3>
                {vendorsWithInvestigations.withoutInvestigations.length > 0 ? (
                  <ScrollArea className="max-h-[200px]">
                    <ul className="space-y-1">
                      {vendorsWithInvestigations.withoutInvestigations.map((vendor) => (
                        <li key={vendor.id} className="text-sm py-1 px-2 border-b">
                          {vendor.vendorName} ({vendor.vendorCode || "코드 없음"})
                        </li>
                      ))}
                    </ul>
                  </ScrollArea>
                ) : (
                  <p className="text-sm text-muted-foreground py-2">
                    모든 선택된 업체에 이미 실사 요청이 있습니다.
                  </p>
                )}
              </div>
            </>
          )}
        </div>
      </>
    )
  }

  if (isDesktop) {
    return (
      <Dialog {...props}>
        {showTrigger ? (
          <DialogTrigger asChild>
            <Button variant="outline" size="sm" className="gap-2">
              <SendHorizonal className="size-4" aria-hidden="true" />
              실사 요청 ({vendors.length})
            </Button>
          </DialogTrigger>
        ) : null}
        <DialogContent className="max-w-md">
          <DialogHeader>
            <DialogTitle>Confirm Vendor Investigation Request</DialogTitle>
            <DialogDescription>
              선택한 {vendors.length}개 업체에 대한 실사 요청을 확인합니다.
              요청 후 협력업체실사담당자에게 알림이 전송됩니다.
            </DialogDescription>
          </DialogHeader>
          
          {renderContent()}
          
          <DialogFooter className="gap-2 sm:space-x-0 mt-4">
            <DialogClose asChild>
              <Button variant="outline">취소</Button>
            </DialogClose>
            <Button
              aria-label="Request selected vendors"
              variant="default"
              onClick={onApprove}
              disabled={isApprovePending || isLoading || vendorsWithInvestigations.withoutInvestigations.length === 0}
            >
              {isApprovePending && (
                <Loader
                  className="mr-2 size-4 animate-spin"
                  aria-hidden="true"
                />
              )}
              요청하기
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    )
  }

  return (
    <Drawer {...props}>
      {showTrigger ? (
        <DrawerTrigger asChild>
          <Button variant="outline" size="sm" className="gap-2">
            <Check className="size-4" aria-hidden="true" />
            Investigation Request ({vendors.length})
          </Button>
        </DrawerTrigger>
      ) : null}
      <DrawerContent>
        <DrawerHeader>
          <DrawerTitle>Confirm Vendor Investigation</DrawerTitle>
          <DrawerDescription>
            선택한 {vendors.length}개 업체에 대한 실사 요청을 확인합니다.
            요청 후 협력업체실사담당자에게 알림이 전송됩니다.
          </DrawerDescription>
        </DrawerHeader>
        
        <div className="px-4">
          {renderContent()}
        </div>
        
        <DrawerFooter className="gap-2 sm:space-x-0 mt-4">
          <DrawerClose asChild>
            <Button variant="outline">취소</Button>
          </DrawerClose>
          <Button
            aria-label="Request selected vendors"
            variant="default"
            onClick={onApprove}
            disabled={isApprovePending || isLoading || vendorsWithInvestigations.withoutInvestigations.length === 0}
          >
            {isApprovePending && (
              <Loader className="mr-2 size-4 animate-spin" aria-hidden="true" />
            )}
            요청하기
          </Button>
        </DrawerFooter>
      </DrawerContent>
    </Drawer>
  )
}