import { useState } from 'react'; import { Plus, Trash2 } from 'lucide-react'; import { useListItems } from '@/api/generated/item/item'; import { Typography } from '@/components/ui/typography'; import { Button } from '@/components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Combobox, type ComboOption } from '@/components/ui/combobox'; import { SUPPLIER_TYPE_OPTIONS, SupplierType, supplierTypeLabel } from '@/lib/enumLabels'; import { showToast } from '@/lib/notify'; import { useSupplierItems } from '../hooks/useSupplierItems'; // 협력사 상세의 취급상품 관리 섹션 — 상품 추가/삭제 + 공급유형(제조/유통/총판/없음) 수정. // 각 조작은 즉시 서버 반영(협력사 기본정보 저장과 독립). export function SupplierItemsManager({ supplierId }: { supplierId: string }) { const { items, isLoading, addItem, changeType, removeItem } = useSupplierItems(supplierId); const [q, setQ] = useState(''); const catalogQuery = useListItems({ search: q || undefined, size: 30 }); // 서버검색(상품 100개 이상도 검색으로 도달) const [pickItemId, setPickItemId] = useState(''); const [pickLabel, setPickLabel] = useState(''); const [pickType, setPickType] = useState(String(SupplierType.NONE)); // 기본 없음(0) const [busy, setBusy] = useState(false); const mappedIds = new Set(items.map((m) => m.item_id)); const options: ComboOption[] = (catalogQuery.data?.items ?? []) .filter((it) => !mappedIds.has(it.item_id)) .map((it) => ({ id: it.item_id, label: `${it.name}${it.code ? ` [${it.code}]` : ''}` })); const handleAdd = async () => { if (!pickItemId) { showToast('추가할 상품을 선택하세요.', 'error'); return; } setBusy(true); try { await addItem(pickItemId, Number(pickType)); setPickItemId(''); setPickLabel(''); setQ(''); showToast('취급상품이 추가되었습니다.', 'success'); } catch (err) { showToast(err instanceof Error ? err.message : '취급상품 추가 실패', 'error'); } finally { setBusy(false); } }; const handleChangeType = async (supplierItemId: string, v: string) => { try { await changeType(supplierItemId, Number(v)); } catch { showToast('공급유형 변경에 실패했습니다.', 'error'); } }; const handleRemove = async (supplierItemId: string) => { try { await removeItem(supplierItemId); showToast('취급상품이 삭제되었습니다.', 'info'); } catch { showToast('취급상품 삭제에 실패했습니다.', 'error'); } }; return (