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
|
"use client";
import * as React from "react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import {
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
} from "@/components/ui/command";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { Check, ChevronsUpDown, Loader2, User, Users } from "lucide-react";
import { cn } from "@/lib/utils";
import { toast } from "sonner";
import { getPUsersForFilter } from "@/lib/rfq-last/service";
import { assignPicToRfqs } from "../service";
import { Badge } from "@/components/ui/badge";
import { Alert, AlertDescription } from "@/components/ui/alert";
interface User {
id: number;
name: string;
userCode?: string;
email?: string;
}
interface RfqAssignPicDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
selectedRfqIds: number[];
selectedRfqCodes: string[];
onSuccess?: () => void;
}
export function RfqAssignPicDialog({
open,
onOpenChange,
selectedRfqIds,
selectedRfqCodes,
onSuccess,
}: RfqAssignPicDialogProps) {
const [users, setUsers] = React.useState<User[]>([]);
const [isLoadingUsers, setIsLoadingUsers] = React.useState(false);
const [isAssigning, setIsAssigning] = React.useState(false);
const [selectedUser, setSelectedUser] = React.useState<User | null>(null);
const [userPopoverOpen, setUserPopoverOpen] = React.useState(false);
const [userSearchTerm, setUserSearchTerm] = React.useState("");
// ITB만 필터링 (rfqCode가 "I"로 시작하는 것)
const itbCodes = React.useMemo(() => {
return selectedRfqCodes.filter(code => code.startsWith("I"));
}, [selectedRfqCodes]);
const itbIds = React.useMemo(() => {
return selectedRfqIds.filter((id, index) => selectedRfqCodes[index]?.startsWith("I"));
}, [selectedRfqIds, selectedRfqCodes]);
// 유저 목록 로드
React.useEffect(() => {
const loadUsers = async () => {
setIsLoadingUsers(true);
try {
const userList = await getPUsersForFilter();
setUsers(userList);
} catch (error) {
console.log("사용자 목록 로드 오류:", error);
toast.error("사용자 목록을 불러오는데 실패했습니다");
} finally {
setIsLoadingUsers(false);
}
};
if (open) {
loadUsers();
// 다이얼로그 열릴 때 초기화
setSelectedUser(null);
setUserSearchTerm("");
}
}, [open]);
// 유저 검색
const filteredUsers = React.useMemo(() => {
if (!userSearchTerm) return users;
const lowerSearchTerm = userSearchTerm.toLowerCase();
return users.filter(
(user) =>
user.name.toLowerCase().includes(lowerSearchTerm) ||
user.userCode?.toLowerCase().includes(lowerSearchTerm)
);
}, [users, userSearchTerm]);
const handleSelectUser = (user: User) => {
setSelectedUser(user);
setUserPopoverOpen(false);
};
const handleAssign = async () => {
if (!selectedUser) {
toast.error("담당자를 선택해주세요");
return;
}
if (itbIds.length === 0) {
toast.error("선택한 항목 중 ITB가 없습니다");
return;
}
setIsAssigning(true);
try {
const result = await assignPicToRfqs({
rfqIds: itbIds,
picUserId: selectedUser.id,
});
if (result.success) {
toast.success(result.message);
onSuccess?.();
onOpenChange(false);
} else {
toast.error(result.message);
}
} catch (error) {
console.error("담당자 지정 오류:", error);
toast.error("담당자 지정 중 오류가 발생했습니다");
} finally {
setIsAssigning(false);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
<Users className="h-5 w-5" />
담당자 지정
</DialogTitle>
<DialogDescription>
선택한 ITB에 구매 담당자를 지정합니다
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* 선택된 ITB 정보 */}
<div className="space-y-2">
<label className="text-sm font-medium">선택된 ITB</label>
<div className="p-3 bg-muted rounded-md">
<div className="flex items-center gap-2 mb-2">
<Badge variant="secondary">{itbCodes.length}건</Badge>
{itbCodes.length !== selectedRfqCodes.length && (
<span className="text-xs text-muted-foreground">
(전체 {selectedRfqCodes.length}건 중)
</span>
)}
</div>
<div className="max-h-[100px] overflow-y-auto">
<div className="flex flex-wrap gap-1">
{itbCodes.slice(0, 10).map((code, index) => (
<Badge key={index} variant="outline" className="text-xs">
{code}
</Badge>
))}
{itbCodes.length > 10 && (
<Badge variant="outline" className="text-xs">
+{itbCodes.length - 10}개
</Badge>
)}
</div>
</div>
</div>
{itbCodes.length === 0 && (
<Alert className="border-orange-200 bg-orange-50">
<AlertDescription className="text-orange-800">
선택한 항목 중 ITB (I로 시작하는 코드)가 없습니다.
</AlertDescription>
</Alert>
)}
</div>
{/* 담당자 선택 */}
<div className="space-y-2">
<label className="text-sm font-medium">구매 담당자</label>
<Popover open={userPopoverOpen} onOpenChange={setUserPopoverOpen}>
<PopoverTrigger asChild>
<Button
type="button"
variant="outline"
className="w-full justify-between h-10"
disabled={isLoadingUsers || itbCodes.length === 0}
>
{isLoadingUsers ? (
<>
<span>담당자 로딩 중...</span>
<Loader2 className="ml-2 h-4 w-4 animate-spin" />
</>
) : (
<>
<span className="flex items-center gap-2">
<User className="h-4 w-4" />
{selectedUser ? (
<>
{selectedUser.name}
{selectedUser.userCode && (
<span className="text-muted-foreground">
({selectedUser.userCode})
</span>
)}
</>
) : (
<span className="text-muted-foreground">
구매 담당자를 선택하세요
</span>
)}
</span>
<ChevronsUpDown className="h-4 w-4 opacity-50" />
</>
)}
</Button>
</PopoverTrigger>
<PopoverContent className="w-[460px] p-0">
<Command>
<CommandInput
placeholder="이름 또는 코드로 검색..."
value={userSearchTerm}
onValueChange={setUserSearchTerm}
/>
<CommandList className="max-h-[300px]">
<CommandEmpty>검색 결과가 없습니다</CommandEmpty>
<CommandGroup>
{filteredUsers.map((user) => (
<CommandItem
key={user.id}
onSelect={() => handleSelectUser(user)}
className="flex items-center justify-between"
>
<span className="flex items-center gap-2">
<User className="h-4 w-4" />
{user.name}
{user.userCode && (
<span className="text-muted-foreground text-sm">
({user.userCode})
</span>
)}
</span>
<Check
className={cn(
"h-4 w-4",
selectedUser?.id === user.id
? "opacity-100"
: "opacity-0"
)}
/>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{selectedUser && (
<p className="text-xs text-muted-foreground">
선택한 담당자: {selectedUser.name}
{selectedUser.userCode && ` (${selectedUser.userCode})`}
</p>
)}
</div>
</div>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isAssigning}
>
취소
</Button>
<Button
type="submit"
onClick={handleAssign}
disabled={!selectedUser || itbCodes.length === 0 || isAssigning}
>
{isAssigning ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
지정 중...
</>
) : (
"담당자 지정"
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|