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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
"use client"
import * as React from "react"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { toast } from "sonner"
import { z } from "zod"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
import { Plus, Loader2 } from "lucide-react"
import { addTechVendor } from "../service"
// 폼 스키마 정의
const addVendorSchema = z.object({
vendorName: z.string().min(1, "업체명을 입력해주세요"),
vendorCode: z.string().optional(),
email: z.string().email("올바른 이메일 주소를 입력해주세요"),
taxId: z.string().optional(),
country: z.string().optional(),
countryEng: z.string().optional(),
countryFab: z.string().optional(),
agentName: z.string().optional(),
agentPhone: z.string().optional(),
agentEmail: z.string().email("올바른 이메일 주소를 입력해주세요").optional().or(z.literal("")),
address: z.string().optional(),
phone: z.string().optional(),
website: z.string().optional(),
techVendorType: z.array(z.enum(["조선", "해양TOP", "해양HULL"])).min(1, "최소 하나의 벤더 타입을 선택해주세요"),
representativeName: z.string().optional(),
representativeEmail: z.string().email("올바른 이메일 주소를 입력해주세요").optional().or(z.literal("")),
representativePhone: z.string().optional(),
representativeBirth: z.string().optional(),
isQuoteComparison: z.boolean().default(false),
})
type AddVendorFormData = z.infer<typeof addVendorSchema>
interface AddVendorDialogProps {
onSuccess?: () => void
}
export function AddVendorDialog({ onSuccess }: AddVendorDialogProps) {
const [open, setOpen] = React.useState(false)
const [isLoading, setIsLoading] = React.useState(false)
const form = useForm<AddVendorFormData>({
resolver: zodResolver(addVendorSchema),
defaultValues: {
vendorName: "",
vendorCode: "",
email: "",
taxId: "",
country: "",
countryEng: "",
countryFab: "",
agentName: "",
agentPhone: "",
agentEmail: "",
address: "",
phone: "",
website: "",
techVendorType: [],
representativeName: "",
representativeEmail: "",
representativePhone: "",
representativeBirth: "",
isQuoteComparison: false,
},
})
const onSubmit = async (data: AddVendorFormData) => {
setIsLoading(true)
try {
const result = await addTechVendor({
...data,
vendorCode: data.vendorCode || null,
country: data.country || null,
countryEng: data.countryEng || null,
countryFab: data.countryFab || null,
agentName: data.agentName || null,
agentPhone: data.agentPhone || null,
agentEmail: data.agentEmail || null,
address: data.address || null,
phone: data.phone || null,
website: data.website || null,
techVendorType: data.techVendorType.join(','),
representativeName: data.representativeName || null,
representativeEmail: data.representativeEmail || null,
representativePhone: data.representativePhone || null,
representativeBirth: data.representativeBirth || null,
taxId: data.taxId || "",
isQuoteComparison: data.isQuoteComparison,
})
if (result.success) {
toast.success("벤더가 성공적으로 추가되었습니다.")
form.reset()
setOpen(false)
onSuccess?.()
} else {
toast.error(result.error || "벤더 추가 중 오류가 발생했습니다.")
}
} catch (error) {
console.error("벤더 추가 오류:", error)
toast.error("벤더 추가 중 오류가 발생했습니다.")
} finally {
setIsLoading(false)
}
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button size="sm" className="gap-2">
<Plus className="size-4" />
벤더 추가
</Button>
</DialogTrigger>
<DialogContent className="max-w-2xl max-h-[90vh] overflow-y-auto">
<DialogHeader>
<DialogTitle>새 기술영업 벤더 추가</DialogTitle>
<DialogDescription>
새로운 기술영업 벤더 정보를 입력하고 사용자 계정을 생성합니다.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
{/* 기본 정보 */}
<div className="space-y-4">
<h3 className="text-lg font-medium">기본 정보</h3>
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="vendorName"
render={({ field }) => (
<FormItem>
<FormLabel>업체명 *</FormLabel>
<FormControl>
<Input placeholder="업체명을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="vendorCode"
render={({ field }) => (
<FormItem>
<FormLabel>업체 코드</FormLabel>
<FormControl>
<Input placeholder="업체 코드를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="email"
render={({ field }) => (
<FormItem>
<FormLabel>이메일 *</FormLabel>
<FormControl>
<Input type="email" placeholder="이메일을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="taxId"
render={({ field }) => (
<FormItem>
<FormLabel>사업자등록번호</FormLabel>
<FormControl>
<Input placeholder="사업자등록번호를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField
control={form.control}
name="techVendorType"
render={({ field }) => (
<FormItem>
<FormLabel>벤더 타입 *</FormLabel>
<div className="space-y-2">
{["조선", "해양TOP", "해양HULL"].map((type) => (
<div key={type} className="flex items-center space-x-2">
<input
type="checkbox"
id={type}
checked={field.value?.includes(type as "조선" | "해양TOP" | "해양HULL")}
onChange={(e) => {
const currentValue = field.value || [];
if (e.target.checked) {
field.onChange([...currentValue, type]);
} else {
field.onChange(currentValue.filter((v) => v !== type));
}
}}
className="w-4 h-4"
/>
<label htmlFor={type} className="text-sm font-medium cursor-pointer">
{type}
</label>
</div>
))}
</div>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="isQuoteComparison"
render={({ field }) => (
<FormItem className="flex flex-row items-start space-x-3 space-y-0">
<FormControl>
<input
type="checkbox"
checked={field.value}
onChange={field.onChange}
className="w-4 h-4 mt-1"
/>
</FormControl>
<div className="space-y-1 leading-none">
<FormLabel className="cursor-pointer">
견적비교용 벤더
</FormLabel>
<p className="text-sm text-muted-foreground">
체크 시 초대 메일을 발송하고 벤더가 직접 가입할 수 있습니다.
</p>
</div>
</FormItem>
)}
/>
</div>
{/* 연락처 정보 */}
<div className="space-y-4">
<h3 className="text-lg font-medium">연락처 정보</h3>
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="phone"
render={({ field }) => (
<FormItem>
<FormLabel>전화번호</FormLabel>
<FormControl>
<Input placeholder="전화번호를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="website"
render={({ field }) => (
<FormItem>
<FormLabel>웹사이트</FormLabel>
<FormControl>
<Input placeholder="웹사이트 URL을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField
control={form.control}
name="address"
render={({ field }) => (
<FormItem>
<FormLabel>주소</FormLabel>
<FormControl>
<Textarea placeholder="주소를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
{/* 국가 정보 */}
<div className="space-y-4">
<h3 className="text-lg font-medium">국가 정보</h3>
<div className="grid grid-cols-3 gap-4">
<FormField
control={form.control}
name="country"
render={({ field }) => (
<FormItem>
<FormLabel>국가</FormLabel>
<FormControl>
<Input placeholder="국가를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="countryEng"
render={({ field }) => (
<FormItem>
<FormLabel>국가 (영문)</FormLabel>
<FormControl>
<Input placeholder="국가 영문명을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="countryFab"
render={({ field }) => (
<FormItem>
<FormLabel>제조국가</FormLabel>
<FormControl>
<Input placeholder="제조국가를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
{/* 대표자 정보 */}
<div className="space-y-4">
<h3 className="text-lg font-medium">대표자 정보</h3>
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="representativeName"
render={({ field }) => (
<FormItem>
<FormLabel>대표자명</FormLabel>
<FormControl>
<Input placeholder="대표자명을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="representativePhone"
render={({ field }) => (
<FormItem>
<FormLabel>대표자 전화번호</FormLabel>
<FormControl>
<Input placeholder="대표자 전화번호를 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-2 gap-4">
<FormField
control={form.control}
name="representativeEmail"
render={({ field }) => (
<FormItem>
<FormLabel>대표자 이메일</FormLabel>
<FormControl>
<Input type="email" placeholder="대표자 이메일을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="representativeBirth"
render={({ field }) => (
<FormItem>
<FormLabel>대표자 생년월일</FormLabel>
<FormControl>
<Input placeholder="YYYY-MM-DD" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setOpen(false)}
disabled={isLoading}
>
취소
</Button>
<Button type="submit" disabled={isLoading}>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
벤더 추가
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
)
}
|