summaryrefslogtreecommitdiff
path: root/lib/pq/pq-review-table-new/send-results-dialog.tsx
blob: 3c8614cc2a47c73fac85b8465e1d1e00f51d8c72 (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
"use client"

import * as React from "react"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import * as z from "zod"


import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from "@/components/ui/dialog"
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/components/ui/form"
import { Textarea } from "@/components/ui/textarea"
import { Badge } from "@/components/ui/badge"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Separator } from "@/components/ui/separator"

// 실사 결과 발송을 위한 스키마
const sendResultsSchema = z.object({
  purchaseComment: z.string().optional(),
})

type SendResultsFormValues = z.infer<typeof sendResultsSchema>

interface AuditResult {
  id: number
  vendorCode: string
  vendorName: string
  vendorEmail: string
  vendorContactPerson: string
  pqNumber: string
  auditItem: string
  auditFactoryAddress: string
  auditMethod: string
  auditResult: string
  additionalNotes?: string
  investigationNotes?: string
}

interface SendResultsDialogProps {
  isOpen: boolean
  onClose: () => void
  onConfirm: (data: SendResultsFormValues) => Promise<void>
  selectedCount: number
  auditResults: AuditResult[]
}

export function SendResultsDialog({
  isOpen,
  onClose,
  onConfirm,
  selectedCount,
  auditResults,
}: SendResultsDialogProps) {
  const [isPending, setIsPending] = React.useState(false)

  const form = useForm<SendResultsFormValues>({
    resolver: zodResolver(sendResultsSchema),
    defaultValues: {
      purchaseComment: "",
    },
  })

  async function handleSubmit(data: SendResultsFormValues) {
    setIsPending(true)
    try {
      await onConfirm(data)
    } finally {
      setIsPending(false)
    }
  }

  const getResultBadgeVariant = (result: string) => {
    if (result.includes("Pass")) return "default"
    if (result.includes("Fail")) return "destructive"
    return "secondary"
  }

  return (
    <Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
      <DialogContent className="max-w-4xl max-h-[90vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle>실사 결과 발송</DialogTitle>
          <DialogDescription>
            선택한 {selectedCount}개 협력업체의 실사 결과를 발송하시겠습니까?
            완료된 실사만 결과를 발송할 수 있습니다.
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-6">
          {/* 실사 결과 미리보기 */}
          <div>
            <h3 className="text-lg font-semibold mb-4">실사 결과 미리보기</h3>
            <div className="space-y-4">
              {auditResults.map((result) => (
                <Card key={result.id}>
                  <CardHeader>
                    <CardTitle className="flex items-center justify-between">
                      <span>{result.vendorName} ({result.vendorCode})</span>
                      <Badge variant={getResultBadgeVariant(result.auditResult)}>
                        {result.auditResult}
                      </Badge>
                    </CardTitle>
                  </CardHeader>
                  <CardContent>
                    <div className="space-y-3 text-sm">
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">PQ No.</div>
                        <div className="col-span-2">{result.pqNumber}</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">Vendor</div>
                        <div className="col-span-2">{result.vendorCode} | {result.vendorName}</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">수신자</div>
                        <div className="col-span-2">{result.vendorContactPerson} ({result.vendorEmail})</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">실사품목</div>
                        <div className="col-span-2">{result.auditItem}</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">실사공장주소</div>
                        <div className="col-span-2">{result.auditFactoryAddress}</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">QM 실사방법</div>
                        <div className="col-span-2">{result.auditMethod}</div>
                      </div>
                      <div className="grid grid-cols-3 gap-4">
                        <div className="font-medium text-muted-foreground">실사결과</div>
                        <div className="col-span-2">
                          <Badge variant={getResultBadgeVariant(result.auditResult)}>
                            {result.auditResult}
                          </Badge>
                        </div>
                      </div>
                      {result.investigationNotes && (
                        <div className="grid grid-cols-3 gap-4">
                          <div className="font-medium text-muted-foreground">실사합격조건</div>
                          <div className="col-span-2">{result.investigationNotes}</div>
                        </div>
                      )}
                    </div>
                  </CardContent>
                </Card>
              ))}
            </div>
          </div>

          <Separator />

          {/* 추가 Comment 입력 */}
          <Form {...form}>
            <form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-4">
              <FormField
                control={form.control}
                name="purchaseComment"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel className="text-base font-medium">
                      추가 Comment (선택사항)
                    </FormLabel>
                    <FormControl>
                      <Textarea
                        placeholder="구매 담당자가 협력업체에 추가 설명/Comment 하고자 할 때 활용합니다. 입력하지 않으면 메일 본문에서 생략됩니다."
                        className="min-h-[100px]"
                        {...field}
                      />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )}
              />

              <DialogFooter>
                <Button
                  type="button"
                  variant="outline"
                  onClick={onClose}
                  disabled={isPending}
                >
                  취소
                </Button>
                <Button 
                  type="submit" 
                  disabled={isPending}
                >
                  {isPending ? "처리 중..." : "결과 발송"}
                </Button>
              </DialogFooter>
            </form>
          </Form>
        </div>
      </DialogContent>
    </Dialog>
  )
}