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
|
"use client"
import * as React from "react"
import { useRouter } from "next/navigation"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { Plus } from "lucide-react"
import * as z from "zod"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
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 {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select"
import { toast } from "sonner"
import { createShipbuildingItem, createOffshoreTopItem, createOffshoreHullItem } from "../service"
import { ItemType } from "./delete-items-dialog"
// 조선 공종 유형 정의
const shipbuildingWorkTypes = [
{ label: "기장", value: "기장" },
{ label: "전장", value: "전장" },
{ label: "선실", value: "선실" },
{ label: "배관", value: "배관" },
{ label: "철의", value: "철의" },
] as const
// 해양 TOP 공종 유형 정의
const offshoreTopWorkTypes = [
{ label: "TM", value: "TM" },
{ label: "TS", value: "TS" },
{ label: "TE", value: "TE" },
{ label: "TP", value: "TP" },
] as const
// 해양 HULL 공종 유형 정의
const offshoreHullWorkTypes = [
{ label: "HA", value: "HA" },
{ label: "HE", value: "HE" },
{ label: "HH", value: "HH" },
{ label: "HM", value: "HM" },
{ label: "NC", value: "NC" },
] as const
// 기본 아이템 스키마
const itemFormSchema = z.object({
itemCode: z.string().min(1, "아이템 코드는 필수입니다"),
workType: z.string().min(1, "공종은 필수입니다"),
// 조선 및 해양 아이템 공통 필드
itemList: z.string().optional(),
// 조선 아이템 전용 필드
shipTypes: z.string().optional(),
subItemList: z.string().optional(),
})
type ItemFormValues = z.infer<typeof itemFormSchema>
interface AddItemDialogProps {
itemType: ItemType
}
export function AddItemDialog({ itemType }: AddItemDialogProps) {
const router = useRouter()
const [open, setOpen] = React.useState(false)
// 기본값 설정
const getDefaultValues = () => {
const defaults: ItemFormValues = {
itemCode: "",
workType: getDefaultWorkType(),
}
if (itemType === 'shipbuilding') {
defaults.shipTypes = "OPTION"
} else {
defaults.itemList = ""
defaults.subItemList = ""
}
return defaults
}
const getDefaultWorkType = () => {
switch (itemType) {
case 'shipbuilding':
return "기장"
case 'offshoreTop':
return "TM"
case 'offshoreHull':
return "HA"
default:
return ""
}
}
const form = useForm<ItemFormValues>({
resolver: zodResolver(itemFormSchema),
defaultValues: getDefaultValues(),
})
const onSubmit = async (data: ItemFormValues) => {
try {
switch (itemType) {
case 'shipbuilding':
if (!data.shipTypes) {
toast.error("선종은 필수입니다")
return
}
await createShipbuildingItem({
itemCode: data.itemCode,
workType: data.workType,
shipTypes: data.shipTypes,
itemList: data.itemList || null,
});
break;
case 'offshoreTop':
await createOffshoreTopItem({
itemCode: data.itemCode,
workType: data.workType as "TM" | "TS" | "TE" | "TP",
itemList: data.itemList || null,
subItemList: data.subItemList || null
});
break;
case 'offshoreHull':
await createOffshoreHullItem({
itemCode: data.itemCode,
workType: data.workType as "HA" | "HE" | "HH" | "HM" | "NC",
itemList: data.itemList || null,
subItemList: data.subItemList || null
});
break;
default:
toast.error("지원하지 않는 아이템 타입입니다");
return;
}
toast.success("아이템이 성공적으로 추가되었습니다")
setOpen(false)
form.reset(getDefaultValues())
router.refresh()
} catch (error) {
toast.error("아이템 추가 중 오류가 발생했습니다")
console.error(error)
}
}
const getItemTypeLabel = () => {
switch (itemType) {
case 'shipbuilding':
return '조선 아이템';
case 'offshoreTop':
return '해양 TOP 아이템';
case 'offshoreHull':
return '해양 HULL 아이템';
default:
return '아이템';
}
}
const getWorkTypeOptions = () => {
switch (itemType) {
case 'shipbuilding':
return shipbuildingWorkTypes;
case 'offshoreTop':
return offshoreTopWorkTypes;
case 'offshoreHull':
return offshoreHullWorkTypes;
default:
return [];
}
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button>
<Plus className="mr-2 h-4 w-4" />
{getItemTypeLabel()} 추가
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>
{getItemTypeLabel()} 추가
</DialogTitle>
<DialogDescription>
새로운 {getItemTypeLabel()}을 추가합니다.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
<FormField
control={form.control}
name="itemCode"
render={({ field }) => (
<FormItem>
<FormLabel>아이템 코드</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="workType"
render={({ field }) => (
<FormItem>
<FormLabel>공종</FormLabel>
<Select onValueChange={field.onChange} defaultValue={field.value}>
<FormControl>
<SelectTrigger>
<SelectValue placeholder="공종을 선택하세요" />
</SelectTrigger>
</FormControl>
<SelectContent>
{getWorkTypeOptions().map((type) => (
<SelectItem key={type.value} value={type.value}>
{type.label}
</SelectItem>
))}
</SelectContent>
</Select>
<FormMessage />
</FormItem>
)}
/>
{itemType === 'shipbuilding' && (
<>
<FormField
control={form.control}
name="shipTypes"
render={({ field }) => (
<FormItem>
<FormLabel>선종</FormLabel>
<FormControl>
<Input placeholder="선종을 입력하세요" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="itemList"
render={({ field }) => (
<FormItem>
<FormLabel>아이템 리스트</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
)}
{(itemType === 'offshoreTop' || itemType === 'offshoreHull') && (
<>
<FormField
control={form.control}
name="itemList"
render={({ field }) => (
<FormItem>
<FormLabel>아이템 리스트</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="subItemList"
render={({ field }) => (
<FormItem>
<FormLabel>서브 아이템 리스트</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
)}
<div className="flex justify-end space-x-2">
<Button type="button" variant="outline" onClick={() => setOpen(false)}>
취소
</Button>
<Button type="submit">추가</Button>
</div>
</form>
</Form>
</DialogContent>
</Dialog>
)
}
|