'use client'; /** * 재고 조정 팝업 (기획서 Page 78) * * - 품목 선택 (검색) * - 유형 필터 셀렉트 박스 (전체, 유형 목록) * - 테이블: 로트번호, 품목코드, 품목유형, 품목명, 규격, 단위, 재고량, 증감 수량 * - 증감 수량: 양수/음수 입력 가능 * - 취소/저장 버튼 */ import { useState, useMemo, useCallback } from 'react'; import { Search, Loader2 } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, VisuallyHidden, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { toast } from 'sonner'; import type { InventoryAdjustmentItem } from './types'; // 목데이터 - 품목 유형 목록 const ITEM_TYPE_OPTIONS = [ { value: 'all', label: '전체' }, { value: 'raw', label: '원자재' }, { value: 'sub', label: '부자재' }, { value: 'part', label: '부품' }, { value: 'product', label: '완제품' }, ]; // 목데이터 - 재고 품목 목록 const MOCK_STOCK_ITEMS: InventoryAdjustmentItem[] = [ { id: '1', lotNo: 'LOT-2026-001', itemCode: 'STEEL-001', itemType: '원자재', itemName: 'SUS304 스테인리스 판재', specification: '1000x2000x3T', unit: 'EA', stockQty: 100 }, { id: '2', lotNo: 'LOT-2026-002', itemCode: 'ELEC-002', itemType: '부품', itemName: 'MCU 컨트롤러 IC', specification: 'STM32F103C8T6', unit: 'EA', stockQty: 500 }, { id: '3', lotNo: 'LOT-2026-003', itemCode: 'PLAS-003', itemType: '부자재', itemName: 'ABS 사출 케이스', specification: '150x100x50', unit: 'SET', stockQty: 200 }, { id: '4', lotNo: 'LOT-2026-004', itemCode: 'STEEL-002', itemType: '원자재', itemName: '알루미늄 프로파일', specification: '40x40x2000L', unit: 'EA', stockQty: 50 }, { id: '5', lotNo: 'LOT-2026-005', itemCode: 'ELEC-005', itemType: '부품', itemName: 'DC 모터 24V', specification: '24V 100RPM', unit: 'EA', stockQty: 80 }, { id: '6', lotNo: 'LOT-2026-006', itemCode: 'CHEM-001', itemType: '부자재', itemName: '에폭시 접착제', specification: '500ml', unit: 'EA', stockQty: 300 }, { id: '7', lotNo: 'LOT-2026-007', itemCode: 'ELEC-007', itemType: '부품', itemName: '커패시터 100uF', specification: '100uF 50V', unit: 'EA', stockQty: 1000 }, ]; interface Props { open: boolean; onOpenChange: (open: boolean) => void; onSave?: (items: InventoryAdjustmentItem[]) => void; } export function InventoryAdjustmentDialog({ open, onOpenChange, onSave }: Props) { const [search, setSearch] = useState(''); const [typeFilter, setTypeFilter] = useState('all'); const [adjustments, setAdjustments] = useState>({}); const [isSaving, setIsSaving] = useState(false); // 필터링된 품목 목록 const filteredItems = useMemo(() => { return MOCK_STOCK_ITEMS.filter((item) => { const matchesSearch = !search || item.itemCode.toLowerCase().includes(search.toLowerCase()) || item.itemName.toLowerCase().includes(search.toLowerCase()) || item.lotNo.toLowerCase().includes(search.toLowerCase()); const matchesType = typeFilter === 'all' || item.itemType === ITEM_TYPE_OPTIONS.find(o => o.value === typeFilter)?.label; return matchesSearch && matchesType; }); }, [search, typeFilter]); // 증감 수량 변경 const handleAdjustmentChange = useCallback((itemId: string, value: string) => { const numValue = value === '' || value === '-' ? undefined : Number(value); setAdjustments((prev) => ({ ...prev, [itemId]: numValue, })); }, []); // 저장 const handleSave = async () => { const itemsWithAdjustment = MOCK_STOCK_ITEMS .filter((item) => adjustments[item.id] !== undefined && adjustments[item.id] !== 0) .map((item) => ({ ...item, adjustmentQty: adjustments[item.id] })); if (itemsWithAdjustment.length === 0) { toast.error('증감 수량을 입력해주세요.'); return; } setIsSaving(true); try { onSave?.(itemsWithAdjustment); toast.success('재고 조정이 저장되었습니다.'); handleClose(); } catch { toast.error('저장 중 오류가 발생했습니다.'); } finally { setIsSaving(false); } }; // 닫기 (상태 초기화) const handleClose = () => { setSearch(''); setTypeFilter('all'); setAdjustments({}); onOpenChange(false); }; return ( 재고 조정 재고 수량 조정
{/* 품목 선택 - 검색 */}
품목 선택
setSearch(e.target.value)} placeholder="품목코드, 품목명, 로트번호 검색" className="pr-10" />
{/* 총 건수 + 유형 필터 */}
{filteredItems.length}
{/* 테이블 */}
로트번호 품목코드 품목유형 품목명 규격 단위 재고량 증감 수량 {filteredItems.map((item) => ( {item.lotNo} {item.itemCode} {item.itemType} {item.itemName} {item.specification} {item.unit} {item.stockQty} handleAdjustmentChange(item.id, e.target.value)} className="h-8 text-sm text-center w-[80px] mx-auto" placeholder="0" /> ))} {filteredItems.length === 0 && ( 검색 결과가 없습니다. )}
{/* 하단 버튼 */}
); }