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
|
"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 {
Sheet,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Textarea } from "@/components/ui/textarea"
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { toast } from "sonner"
import { PcrPoData } from "@/lib/pcr/types"
import { updatePcrRejectionReasonAction } from "@/lib/pcr/actions"
const pcrEditSchema = z.object({
rejectionReasonType: z.string().optional(),
customRejectionReason: z.string().optional(),
})
const REJECTION_REASON_OPTIONS = [
{ value: "제작완료", label: "제작완료" },
{ value: "납품 준비 중", label: "납품 준비 중" },
{ value: "제작을 위한 원소재 준비 완료", label: "제작을 위한 원소재 준비 완료" },
{ value: "협력사 거래중지로 인한 승인 불가", label: "협력사 거래중지로 인한 승인 불가" },
{ value: "기타", label: "기타" },
]
type PcrEditFormData = z.infer<typeof pcrEditSchema>
interface EditPcrSheetProps {
open: boolean
onOpenChange: (open: boolean) => void
pcrData: PcrPoData | null
onSuccess: () => void
}
export function EditPcrSheet({
open,
onOpenChange,
pcrData,
onSuccess,
}: EditPcrSheetProps) {
const [isSubmitting, setIsSubmitting] = React.useState(false)
const form = useForm<PcrEditFormData>({
resolver: zodResolver(pcrEditSchema),
defaultValues: {
rejectionReason: "",
},
})
// PCR 데이터가 변경될 때 폼 초기화
React.useEffect(() => {
if (pcrData) {
const rejectionReason = pcrData.rejectionReason || ""
// 기존 rejectionReason을 파싱해서 미리 정의된 옵션인지 확인
const predefinedOption = REJECTION_REASON_OPTIONS.find(option =>
option.value === rejectionReason
)
if (predefinedOption) {
form.reset({
rejectionReasonType: rejectionReason,
customRejectionReason: "",
})
} else {
form.reset({
rejectionReasonType: "기타",
customRejectionReason: rejectionReason,
})
}
}
}, [pcrData, form])
const onSubmit = async (data: PcrEditFormData) => {
if (!pcrData) {
toast.error("PCR 데이터가 없습니다")
return
}
setIsSubmitting(true)
try {
// rejectionReason 조합
let rejectionReason = ""
if (data.rejectionReasonType === "기타") {
rejectionReason = data.customRejectionReason || ""
} else {
rejectionReason = data.rejectionReasonType || ""
}
const result = await updatePcrRejectionReasonAction({
id: pcrData.id,
rejectionReason: rejectionReason,
})
if (result.success) {
toast.success("PCR이 성공적으로 수정되었습니다")
onOpenChange(false)
onSuccess()
} else {
toast.error(result.error || "PCR 수정에 실패했습니다")
}
} catch (error) {
console.error("PCR 수정 오류:", error)
toast.error("PCR 수정 중 오류가 발생했습니다")
} finally {
setIsSubmitting(false)
}
}
if (!pcrData) return null
return (
<Sheet open={open} onOpenChange={onOpenChange}>
<SheetContent className="w-[400px] sm:w-[540px]">
<SheetHeader>
<SheetTitle>PCR 수정</SheetTitle>
<SheetDescription>
PO/계약번호: {pcrData.poContractNumber}
</SheetDescription>
</SheetHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
{/* 읽기 전용 정보 표시 */}
<div className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div>
<label className="text-sm font-medium">프로젝트</label>
<p className="text-sm text-muted-foreground">{pcrData.project || "-"}</p>
</div>
<div>
<label className="text-sm font-medium">변경구분</label>
<p className="text-sm text-muted-foreground">{pcrData.changeType}</p>
</div>
</div>
<div>
<label className="text-sm font-medium">PCR 사유</label>
<p className="text-sm text-muted-foreground">{pcrData.pcrReason || "-"}</p>
</div>
<div>
<label className="text-sm font-medium">상세 사유</label>
<p className="text-sm text-muted-foreground">{pcrData.detailsReason || "-"}</p>
</div>
</div>
{/* 편집 가능한 필드 */}
<FormField
control={form.control}
name="rejectionReasonType"
render={({ field }) => (
<FormItem>
<FormLabel>거절 사유 *</FormLabel>
<Select onValueChange={field.onChange} value={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="거절 사유를 선택하세요" />
</SelectTrigger>
</FormControl>
<SelectContent>
{REJECTION_REASON_OPTIONS.map((option) => (
<SelectItem key={option.value} value={option.value}>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
{/* 기타 선택 시 텍스트 입력 필드 */}
{form.watch("rejectionReasonType") === "기타" && (
<FormField
control={form.control}
name="customRejectionReason"
render={({ field }) => (
<FormItem>
<FormLabel>기타 거절 사유</FormLabel>
<FormControl>
<Textarea
placeholder="거절 사유를 입력하세요"
className="resize-none"
rows={3}
{...field}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}
<SheetFooter>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isSubmitting}
>
취소
</Button>
<Button type="submit" disabled={isSubmitting}>
{isSubmitting ? "수정 중..." : "수정"}
</Button>
</SheetFooter>
</form>
</Form>
</SheetContent>
</Sheet>
)
}
|