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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
|
"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 {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger
} from "@/components/ui/popover";
import { Textarea } from "@/components/ui/textarea";
import { Switch } from "@/components/ui/switch";
import { Calendar } from "@/components/ui/calendar";
import { CalendarIcon, Loader2, Info, Package, Check, ChevronsUpDown } from "lucide-react";
import { format } from "date-fns";
import { ko } from "date-fns/locale";
import { cn } from "@/lib/utils";
import { toast } from "sonner";
import { updateVendorConditionsBatch } from "../service";
import { Badge } from "@/components/ui/badge";
import { TAX_CONDITIONS } from "@/lib/tax-conditions/types";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Checkbox } from "@/components/ui/checkbox";
import {
getIncotermsForSelection,
getPaymentTermsForSelection,
getPlaceOfShippingForSelection,
getPlaceOfDestinationForSelection
} from "@/lib/procurement-select/service";
interface BatchUpdateConditionsDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
rfqId: number;
rfqCode: string;
selectedVendors: Array<{
id: number;
vendorName: string;
vendorCode: string;
}>;
onSuccess: () => void;
}
// 타입 정의
interface SelectOption {
id: number;
code: string;
description: string;
}
// 폼 스키마
const formSchema = z.object({
currency: z.string().optional(),
paymentTermsCode: z.string().optional(),
incotermsCode: z.string().optional(),
incotermsDetail: z.string().optional(),
contractDuration: z.string().optional(),
taxCode: z.string().optional(),
placeOfShipping: z.string().optional(),
placeOfDestination: z.string().optional(),
deliveryDate: z.date().optional(),
materialPriceRelatedYn: z.boolean().default(false),
sparepartYn: z.boolean().default(false),
firstYn: z.boolean().default(false),
firstDescription: z.string().optional(),
sparepartDescription: z.string().optional(),
});
type FormValues = z.infer<typeof formSchema>;
const currencies = ["USD", "EUR", "KRW", "JPY", "CNY"];
export function BatchUpdateConditionsDialog({
open,
onOpenChange,
rfqId,
rfqCode,
selectedVendors,
onSuccess,
}: BatchUpdateConditionsDialogProps) {
const [isLoading, setIsLoading] = React.useState(false);
// Select 옵션들 상태
const [incoterms, setIncoterms] = React.useState<SelectOption[]>([]);
const [paymentTerms, setPaymentTerms] = React.useState<SelectOption[]>([]);
const [shippingPlaces, setShippingPlaces] = React.useState<SelectOption[]>([]);
const [destinationPlaces, setDestinationPlaces] = React.useState<SelectOption[]>([]);
// 로딩 상태
const [incotermsLoading, setIncotermsLoading] = React.useState(false);
const [paymentTermsLoading, setPaymentTermsLoading] = React.useState(false);
const [shippingLoading, setShippingLoading] = React.useState(false);
const [destinationLoading, setDestinationLoading] = React.useState(false);
// Popover 열림 상태
const [incotermsOpen, setIncotermsOpen] = React.useState(false);
const [paymentTermsOpen, setPaymentTermsOpen] = React.useState(false);
const [shippingOpen, setShippingOpen] = React.useState(false);
const [destinationOpen, setDestinationOpen] = React.useState(false);
const [calendarOpen, setCalendarOpen] = React.useState(false);
const [currencyOpen, setCurrencyOpen] = React.useState(false);
// 체크박스로 각 필드 업데이트 여부 관리
const [fieldsToUpdate, setFieldsToUpdate] = React.useState({
currency: false,
paymentTermsCode: false,
incoterms: false,
deliveryDate: false,
contractDuration: false,
taxCode: false,
shipping: false,
materialPrice: false,
sparepart: false,
first: false,
});
// 폼 초기화
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
currency: "",
paymentTermsCode: "",
incotermsCode: "",
incotermsDetail: "",
contractDuration: "",
taxCode: "",
placeOfShipping: "",
placeOfDestination: "",
materialPriceRelatedYn: false,
sparepartYn: false,
firstYn: false,
firstDescription: "",
sparepartDescription: "",
},
});
// 데이터 로드 함수들
const loadIncoterms = React.useCallback(async () => {
setIncotermsLoading(true);
try {
const data = await getIncotermsForSelection();
setIncoterms(data);
} catch (error) {
console.error("Failed to load incoterms:", error);
toast.error("Incoterms 목록을 불러오는데 실패했습니다.");
} finally {
setIncotermsLoading(false);
}
}, []);
const loadPaymentTerms = React.useCallback(async () => {
setPaymentTermsLoading(true);
try {
const data = await getPaymentTermsForSelection();
setPaymentTerms(data);
} catch (error) {
console.error("Failed to load payment terms:", error);
toast.error("결제조건 목록을 불러오는데 실패했습니다.");
} finally {
setPaymentTermsLoading(false);
}
}, []);
const loadShippingPlaces = React.useCallback(async () => {
setShippingLoading(true);
try {
const data = await getPlaceOfShippingForSelection();
setShippingPlaces(data);
} catch (error) {
console.error("Failed to load shipping places:", error);
toast.error("선적지 목록을 불러오는데 실패했습니다.");
} finally {
setShippingLoading(false);
}
}, []);
const loadDestinationPlaces = React.useCallback(async () => {
setDestinationLoading(true);
try {
const data = await getPlaceOfDestinationForSelection();
setDestinationPlaces(data);
} catch (error) {
console.error("Failed to load destination places:", error);
toast.error("도착지 목록을 불러오는데 실패했습니다.");
} finally {
setDestinationLoading(false);
}
}, []);
// 초기 데이터 로드
React.useEffect(() => {
if (open) {
loadIncoterms();
loadPaymentTerms();
loadShippingPlaces();
loadDestinationPlaces();
}
}, [open, loadIncoterms, loadPaymentTerms, loadShippingPlaces, loadDestinationPlaces]);
// 다이얼로그 닫힐 때 초기화
React.useEffect(() => {
if (!open) {
form.reset();
setFieldsToUpdate({
currency: false,
paymentTermsCode: false,
incoterms: false,
deliveryDate: false,
contractDuration: false,
taxCode: false,
shipping: false,
materialPrice: false,
sparepart: false,
first: false,
});
}
}, [open, form]);
// 제출 처리
const onSubmit = async (data: FormValues) => {
const hasFieldsToUpdate = Object.values(fieldsToUpdate).some(v => v);
if (!hasFieldsToUpdate) {
toast.error("최소 1개 이상의 변경할 항목을 선택해주세요.");
return;
}
// 선택된 필드만 포함하여 conditions 객체 생성
const conditions: any = {};
if (fieldsToUpdate.currency && data.currency) {
conditions.currency = data.currency;
}
if (fieldsToUpdate.paymentTermsCode && data.paymentTermsCode) {
conditions.paymentTermsCode = data.paymentTermsCode;
}
if (fieldsToUpdate.incoterms) {
if (data.incotermsCode) conditions.incotermsCode = data.incotermsCode;
if (data.incotermsDetail) conditions.incotermsDetail = data.incotermsDetail;
}
if (fieldsToUpdate.deliveryDate && data.deliveryDate) {
conditions.deliveryDate = data.deliveryDate;
}
if (fieldsToUpdate.contractDuration) {
conditions.contractDuration = data.contractDuration;
}
if (fieldsToUpdate.taxCode) {
conditions.taxCode = data.taxCode;
}
if (fieldsToUpdate.shipping) {
conditions.placeOfShipping = data.placeOfShipping;
conditions.placeOfDestination = data.placeOfDestination;
}
if (fieldsToUpdate.materialPrice) {
conditions.materialPriceRelatedYn = data.materialPriceRelatedYn;
}
if (fieldsToUpdate.sparepart) {
conditions.sparepartYn = data.sparepartYn;
if (data.sparepartYn) {
conditions.sparepartDescription = data.sparepartDescription;
}
}
if (fieldsToUpdate.first) {
conditions.firstYn = data.firstYn;
if (data.firstYn) {
conditions.firstDescription = data.firstDescription;
}
}
setIsLoading(true);
try {
const vendorIds = selectedVendors.map(v => v.id);
const result = await updateVendorConditionsBatch({
rfqId,
vendorIds,
conditions,
});
if (result.success) {
toast.success(result.data?.message || "조건이 성공적으로 업데이트되었습니다.");
onSuccess();
onOpenChange(false);
} else {
toast.error(result.error || "조건 업데이트에 실패했습니다.");
}
} catch (error) {
console.error("Submit error:", error);
toast.error("오류가 발생했습니다.");
} finally {
setIsLoading(false);
}
};
const getUpdateCount = () => {
return Object.values(fieldsToUpdate).filter(v => v).length;
};
// 선택된 옵션 찾기 헬퍼 함수들
const selectedIncoterm = incoterms.find(i => i.code === form.watch("incotermsCode"));
const selectedPaymentTerm = paymentTerms.find(p => p.code === form.watch("paymentTermsCode"));
const selectedShipping = shippingPlaces.find(s => s.code === form.watch("placeOfShipping"));
const selectedDestination = destinationPlaces.find(d => d.code === form.watch("placeOfDestination"));
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-3xl h-[90vh] p-0 flex flex-col">
{/* 헤더 */}
<DialogHeader className="p-6 pb-0">
<DialogTitle>조건 일괄 설정</DialogTitle>
<DialogDescription>
선택한 {selectedVendors.length}개 벤더에 동일한 조건을 적용합니다.
변경하려는 항목만 체크하고 값을 입력하세요.
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex flex-col flex-1 min-h-0">
{/* 스크롤 가능한 컨텐츠 영역 */}
<ScrollArea className="flex-1 px-6">
<div className="grid gap-4 py-4">
{/* 선택된 벤더 정보 */}
<Card>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<CardTitle className="text-lg flex items-center gap-2">
<Package className="h-5 w-5" />
대상 벤더
</CardTitle>
<Badge>{selectedVendors.length}개</Badge>
</div>
</CardHeader>
<CardContent>
<div className="flex flex-wrap gap-2">
{selectedVendors.map((vendor) => (
<Badge key={vendor.id} variant="secondary">
{vendor.vendorCode} - {vendor.vendorName}
</Badge>
))}
</div>
</CardContent>
</Card>
{/* 안내 메시지 */}
<Alert>
<Info className="h-4 w-4" />
<AlertDescription>
체크박스를 선택한 항목만 업데이트됩니다.
선택하지 않은 항목은 기존 값이 유지됩니다.
</AlertDescription>
</Alert>
{/* 기본 조건 설정 */}
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<CardTitle className="text-lg">기본 조건</CardTitle>
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
// 기본조건만 전체 선택/해제
const basicFields = ['currency', 'paymentTermsCode', 'incoterms', 'deliveryDate', 'contractDuration', 'taxCode', 'shipping'];
const allBasicSelected = basicFields.every(field => fieldsToUpdate[field as keyof typeof fieldsToUpdate]);
const newState = { ...fieldsToUpdate };
basicFields.forEach(field => {
newState[field as keyof typeof newState] = !allBasicSelected;
});
setFieldsToUpdate(newState);
}}
>
{['currency', 'paymentTermsCode', 'incoterms', 'deliveryDate', 'contractDuration', 'taxCode', 'shipping'].every(field => fieldsToUpdate[field as keyof typeof fieldsToUpdate]) ? '전체 해제' : '전체 선택'}
</Button>
</div>
</CardHeader>
<CardContent className="space-y-4">
{/* 통화 */}
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.currency}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, currency: !!checked })
}
/>
<FormField
control={form.control}
name="currency"
render={({ field }) => (
<FormItem className="flex-1 grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.currency && "text-muted-foreground"
)}>
통화
</FormLabel>
<div className="col-span-2">
<FormControl>
<Popover open={currencyOpen} onOpenChange={setCurrencyOpen}>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
aria-expanded={currencyOpen}
className="w-full justify-between"
disabled={!fieldsToUpdate.currency}
>
<span className="text-muted-foreground">
{field.value || "통화 선택"}
</span>
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="통화 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation(); // 이벤트 전파 차단
const target = e.currentTarget;
target.scrollTop += e.deltaY; // 직접 스크롤 처리
}}
>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{currencies.map((currency) => (
<CommandItem
key={currency}
value={currency}
onSelect={() => {
field.onChange(currency);
setCurrencyOpen(false);
}}
>
{currency}
<Check
className={cn(
"ml-auto h-4 w-4",
currency === field.value ? "opacity-100" : "opacity-0"
)}
/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
{/* 결제 조건 */}
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.paymentTermsCode}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, paymentTermsCode: !!checked })
}
/>
<FormField
control={form.control}
name="paymentTermsCode"
render={({ field }) => (
<FormItem className="flex-1 grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.paymentTermsCode && "text-muted-foreground"
)}>
결제 조건
</FormLabel>
<div className="col-span-2">
<Popover open={paymentTermsOpen} onOpenChange={setPaymentTermsOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
aria-expanded={paymentTermsOpen}
className="w-full justify-between"
disabled={!fieldsToUpdate.paymentTermsCode || paymentTermsLoading}
>
{selectedPaymentTerm ? (
<span className="truncate">
{selectedPaymentTerm.code} - {selectedPaymentTerm.description}
</span>
) : (
<span className="text-muted-foreground">
{paymentTermsLoading ? "로딩 중..." : "결제조건 선택"}
</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="코드 또는 설명으로 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation(); // 이벤트 전파 차단
const target = e.currentTarget;
target.scrollTop += e.deltaY; // 직접 스크롤 처리
}}
>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{paymentTerms.map((term) => (
<CommandItem
key={term.id}
value={`${term.code} ${term.description}`}
onSelect={() => {
field.onChange(term.code);
setPaymentTermsOpen(false);
}}
>
<div className="flex items-center gap-2 w-full">
<span className="font-medium">{term.code}</span>
<span className="text-muted-foreground">-</span>
<span className="truncate">{term.description}</span>
<Check
className={cn(
"ml-auto h-4 w-4",
term.code === field.value ? "opacity-100" : "opacity-0"
)}
/>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
{/* 인코텀즈 */}
<div className="flex items-start gap-4">
<Checkbox
className="mt-3"
checked={fieldsToUpdate.incoterms}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, incoterms: !!checked })
}
/>
<div className="flex-1 grid grid-cols-3 gap-4">
<Label className={cn(
"text-right pt-2",
!fieldsToUpdate.incoterms && "text-muted-foreground"
)}>
인코텀즈
</Label>
<div className="col-span-2 space-y-2">
<FormField
control={form.control}
name="incotermsCode"
render={({ field }) => (
<FormItem>
<Popover open={incotermsOpen} onOpenChange={setIncotermsOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
aria-expanded={incotermsOpen}
className="w-full justify-between"
disabled={!fieldsToUpdate.incoterms || incotermsLoading}
>
{selectedIncoterm ? (
<span className="truncate">
{selectedIncoterm.code} - {selectedIncoterm.description}
</span>
) : (
<span className="text-muted-foreground">
{incotermsLoading ? "로딩 중..." : "인코텀즈 선택"}
</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="코드 또는 설명으로 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation(); // 이벤트 전파 차단
const target = e.currentTarget;
target.scrollTop += e.deltaY; // 직접 스크롤 처리
}}>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{incoterms.map((incoterm) => (
<CommandItem
key={incoterm.id}
value={`${incoterm.code} ${incoterm.description}`}
onSelect={() => {
field.onChange(incoterm.code);
setIncotermsOpen(false);
}}
>
<div className="flex items-center gap-2 w-full">
<span className="font-medium">{incoterm.code}</span>
<span className="text-muted-foreground">-</span>
<span className="truncate">{incoterm.description}</span>
<Check
className={cn(
"ml-auto h-4 w-4",
incoterm.code === field.value ? "opacity-100" : "opacity-0"
)}
/>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</FormItem>
)}
/>
{/* <FormField
control={form.control}
name="incotermsDetail"
render={({ field }) => (
<FormItem>
<FormControl>
<Input
placeholder="인코텀즈 상세 (예: 부산항)"
{...field}
disabled={!fieldsToUpdate.incoterms}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/> */}
</div>
</div>
</div>
{/* 납기일 */}
{!rfqCode.startsWith("F") && (
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.deliveryDate}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, deliveryDate: !!checked })
}
/>
<FormField
control={form.control}
name="deliveryDate"
render={({ field }) => (
<FormItem className="flex-1 grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.deliveryDate && "text-muted-foreground"
)}>
납기일
</FormLabel>
<div className="col-span-2">
<Popover open={calendarOpen} onOpenChange={setCalendarOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
className={cn(
"w-full justify-start text-left font-normal",
!field.value && "text-muted-foreground"
)}
disabled={!fieldsToUpdate.deliveryDate}
>
<CalendarIcon className="mr-2 h-4 w-4" />
{field.value ? (
format(field.value, "yyyy-MM-dd", { locale: ko })
) : (
<span>날짜를 선택하세요</span>
)}
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-auto p-0">
<Calendar
mode="single"
selected={field.value}
onSelect={(date) => {
field.onChange(date);
setCalendarOpen(false);
}}
initialFocus
/>
</PopoverContent>
</Popover>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
)}
{/* 계약 기간 */}
{rfqCode.startsWith("F") && (
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.contractDuration}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, contractDuration: !!checked })
}
/>
<FormField
control={form.control}
name="contractDuration"
render={({ field }) => (
<FormItem className="flex-1 grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.contractDuration && "text-muted-foreground"
)}>
계약 기간
</FormLabel>
<div className="col-span-2">
<FormControl>
<Input
placeholder="예: 12개월"
{...field}
disabled={!fieldsToUpdate.contractDuration}
/>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
)}
{/* 세금 코드 */}
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.taxCode}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, taxCode: !!checked })
}
/>
<FormField
control={form.control}
name="taxCode"
render={({ field }) => (
<FormItem className="flex-1 grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.taxCode && "text-muted-foreground"
)}>
세금 코드
</FormLabel>
<div className="col-span-2">
<FormControl>
<Popover>
<PopoverTrigger asChild>
<Button
variant="outline"
role="combobox"
className="w-full justify-between"
disabled={!fieldsToUpdate.taxCode}
>
{field.value ? (
<span className="truncate">
{TAX_CONDITIONS.find(t => t.code === field.value)?.name || field.value}
</span>
) : (
<span className="text-muted-foreground">세금 코드 선택</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="세금 코드 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation();
const target = e.currentTarget;
target.scrollTop += e.deltaY;
}}
>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{TAX_CONDITIONS.map((condition) => (
<CommandItem
key={condition.code}
value={`${condition.code} ${condition.name}`}
onSelect={() => field.onChange(condition.code)}
>
<div className="flex items-center gap-2 w-full">
<span className="font-medium">{condition.code}</span>
<span className="text-muted-foreground">-</span>
<span className="truncate">{condition.name}</span>
<Check
className={cn(
"ml-auto h-4 w-4",
condition.code === field.value ? "opacity-100" : "opacity-0"
)}
/>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</FormControl>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
{/* 선적지/도착지 */}
<div className="flex items-start gap-4">
<Checkbox
className="mt-3"
checked={fieldsToUpdate.shipping}
onCheckedChange={(checked) =>
setFieldsToUpdate({ ...fieldsToUpdate, shipping: !!checked })
}
/>
<div className="flex-1 space-y-2">
<FormField
control={form.control}
name="placeOfShipping"
render={({ field }) => (
<FormItem className="grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.shipping && "text-muted-foreground"
)}>
선적지
</FormLabel>
<div className="col-span-2">
<Popover open={shippingOpen} onOpenChange={setShippingOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
aria-expanded={shippingOpen}
className="w-full justify-between"
disabled={!fieldsToUpdate.shipping || shippingLoading}
>
{selectedShipping ? (
<span className="truncate">
{selectedShipping.code} - {selectedShipping.description}
</span>
) : (
<span className="text-muted-foreground">
{shippingLoading ? "로딩 중..." : "선적지 선택"}
</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="선적지 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation(); // 이벤트 전파 차단
const target = e.currentTarget;
target.scrollTop += e.deltaY; // 직접 스크롤 처리
}}
>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{shippingPlaces.map((place) => (
<CommandItem
key={place.id}
value={`${place.code} ${place.description}`}
onSelect={() => {
field.onChange(place.code);
setShippingOpen(false);
}}
>
<div className="flex items-center gap-2 w-full">
<span className="font-medium">{place.code}</span>
<span className="text-muted-foreground">-</span>
<span className="truncate">{place.description}</span>
<Check
className={cn(
"ml-auto h-4 w-4",
place.code === field.value ? "opacity-100" : "opacity-0"
)}
/>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="placeOfDestination"
render={({ field }) => (
<FormItem className="grid grid-cols-3 items-center gap-4">
<FormLabel className={cn(
"text-right",
!fieldsToUpdate.shipping && "text-muted-foreground"
)}>
도착지
</FormLabel>
<div className="col-span-2">
<Popover open={destinationOpen} onOpenChange={setDestinationOpen}>
<PopoverTrigger asChild>
<FormControl>
<Button
variant="outline"
role="combobox"
aria-expanded={destinationOpen}
className="w-full justify-between"
disabled={!fieldsToUpdate.shipping || destinationLoading}
>
{selectedDestination ? (
<span className="truncate">
{selectedDestination.code} - {selectedDestination.description}
</span>
) : (
<span className="text-muted-foreground">
{destinationLoading ? "로딩 중..." : "도착지 선택"}
</span>
)}
<ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</FormControl>
</PopoverTrigger>
<PopoverContent className="w-full p-0" align="start">
<Command>
<CommandInput placeholder="도착지 검색..." />
<CommandList
onWheel={(e) => {
e.stopPropagation(); // 이벤트 전파 차단
const target = e.currentTarget;
target.scrollTop += e.deltaY; // 직접 스크롤 처리
}}
>
<CommandEmpty>검색 결과가 없습니다.</CommandEmpty>
<CommandGroup>
{destinationPlaces.map((place) => (
<CommandItem
key={place.id}
value={`${place.code} ${place.description}`}
onSelect={() => {
field.onChange(place.code);
setDestinationOpen(false);
}}
>
<div className="flex items-center gap-2 w-full">
<span className="font-medium">{place.code}</span>
<span className="text-muted-foreground">-</span>
<span className="truncate">{place.description}</span>
<Check
className={cn(
"ml-auto h-4 w-4",
place.code === field.value ? "opacity-100" : "opacity-0"
)}
/>
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
<FormMessage />
</div>
</FormItem>
)}
/>
</div>
</div>
</CardContent>
</Card>
{/* 추가 옵션 */}
<Card>
<CardHeader>
<CardTitle className="text-lg">추가 옵션</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
{/* 연동제 적용 */}
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.materialPrice}
onCheckedChange={(checked) => {
setFieldsToUpdate({ ...fieldsToUpdate, materialPrice: !!checked });
if (checked) {
form.setValue("materialPriceRelatedYn", true);
}
}}
/>
<FormField
control={form.control}
name="materialPriceRelatedYn"
render={({ field }) => (
<FormItem className="flex-1 flex items-center justify-between">
<div className="space-y-0.5">
<FormLabel className={cn(
!fieldsToUpdate.materialPrice && "text-muted-foreground"
)}>
연동제 적용
</FormLabel>
<div className="text-sm text-muted-foreground">
원자재 가격 연동 여부
</div>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
disabled={!fieldsToUpdate.materialPrice}
/>
</FormControl>
</FormItem>
)}
/>
</div>
{/* Spare Part */}
<div className="space-y-2">
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.sparepart}
onCheckedChange={(checked) => {
setFieldsToUpdate({ ...fieldsToUpdate, sparepart: !!checked });
if (checked) {
form.setValue("sparepartYn", true);
}
}}
/>
<FormField
control={form.control}
name="sparepartYn"
render={({ field }) => (
<FormItem className="flex-1 flex items-center justify-between">
<div className="space-y-0.5">
<FormLabel className={cn(
!fieldsToUpdate.sparepart && "text-muted-foreground"
)}>
Spare Part
</FormLabel>
<div className="text-sm text-muted-foreground">
예비 부품 요구사항
</div>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
disabled={!fieldsToUpdate.sparepart}
/>
</FormControl>
</FormItem>
)}
/>
</div>
{form.watch("sparepartYn") && fieldsToUpdate.sparepart && (
<FormField
control={form.control}
name="sparepartDescription"
render={({ field }) => (
<FormItem className="ml-7">
<FormControl>
<Textarea
placeholder="Spare Part 요구사항을 입력하세요..."
{...field}
disabled={!fieldsToUpdate.sparepart}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}
</div>
{/* 초도품 관리 */}
<div className="space-y-2">
<div className="flex items-center gap-4">
<Checkbox
checked={fieldsToUpdate.first}
onCheckedChange={(checked) => {
setFieldsToUpdate({ ...fieldsToUpdate, first: !!checked });
if (checked) {
form.setValue("firstYn", true);
}
}}
/>
<FormField
control={form.control}
name="firstYn"
render={({ field }) => (
<FormItem className="flex-1 flex items-center justify-between">
<div className="space-y-0.5">
<FormLabel className={cn(
!fieldsToUpdate.first && "text-muted-foreground"
)}>
초도품 관리
</FormLabel>
<div className="text-sm text-muted-foreground">
초도품 관리 요구사항
</div>
</div>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={field.onChange}
disabled={!fieldsToUpdate.first}
/>
</FormControl>
</FormItem>
)}
/>
</div>
{form.watch("firstYn") && fieldsToUpdate.first && (
<FormField
control={form.control}
name="firstDescription"
render={({ field }) => (
<FormItem className="ml-7">
<FormControl>
<Textarea
placeholder="초도품 관리 요구사항을 입력하세요..."
{...field}
disabled={!fieldsToUpdate.first}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
)}
</div>
</CardContent>
</Card>
</div>
</ScrollArea>
{/* 푸터 */}
<DialogFooter className="p-6 pt-4 border-t">
<div className="flex items-center justify-between w-full">
<div className="text-sm text-muted-foreground">
{getUpdateCount() > 0
? `${getUpdateCount()}개 항목 선택됨`
: '변경할 항목을 선택하세요'
}
</div>
<div className="flex gap-2">
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isLoading}
>
취소
</Button>
<Button
type="submit"
disabled={isLoading || getUpdateCount() === 0}
>
{isLoading && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
{getUpdateCount() > 0
? `${getUpdateCount()}개 항목 업데이트`
: '조건 업데이트'
}
</Button>
</div>
</div>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
}
|