'use client'; import React, { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Plus, Edit, Trash2, Package, GripVertical } from 'lucide-react'; import { toast } from 'sonner'; import type { BOMItem } from '@/contexts/ItemMasterContext'; interface BOMManagementSectionProps { title?: string; description?: string; bomItems: BOMItem[]; onAddItem: (item: Omit) => void; onUpdateItem: (id: number, item: Partial) => void; onDeleteItem: (id: number) => void; itemTypeOptions?: { value: string; label: string }[]; unitOptions?: { value: string; label: string }[]; } export function BOMManagementSection({ title = '부품 구성 (BOM)', description = '이 제품을 구성하는 하위 품목을 추가하세요', bomItems, onAddItem, onUpdateItem, onDeleteItem, itemTypeOptions = [ { value: 'product', label: '제품' }, { value: 'part', label: '부품' }, { value: 'material', label: '원자재' }, ], unitOptions = [ { value: 'EA', label: 'EA' }, { value: 'KG', label: 'KG' }, { value: 'M', label: 'M' }, { value: 'L', label: 'L' }, ], }: BOMManagementSectionProps) { const [isDialogOpen, setIsDialogOpen] = useState(false); const [editingId, setEditingId] = useState(null); const [itemCode, setItemCode] = useState(''); const [itemName, setItemName] = useState(''); const [quantity, setQuantity] = useState('1'); const [unit, setUnit] = useState('EA'); const [itemType, setItemType] = useState('part'); const [note, setNote] = useState(''); const handleOpenDialog = (item?: BOMItem) => { if (item) { setEditingId(item.id); setItemCode(item.item_code || ''); setItemName(item.item_name); setQuantity(item.quantity.toString()); setUnit(item.unit || 'EA'); setItemType('part'); setNote(item.spec || ''); } else { setEditingId(null); setItemCode(''); setItemName(''); setQuantity('1'); setUnit('EA'); setItemType('part'); setNote(''); } setIsDialogOpen(true); }; const handleSave = () => { if (!itemCode.trim() || !itemName.trim()) { return toast.error('품목코드와 품목명을 입력해주세요'); } const qty = parseFloat(quantity); if (isNaN(qty) || qty <= 0) { return toast.error('올바른 수량을 입력해주세요'); } const itemData = { item_code: itemCode, item_name: itemName, quantity: qty, unit, spec: note.trim() || undefined, }; if (editingId) { onUpdateItem(editingId, itemData); toast.success('BOM 품목이 수정되었습니다'); } else { onAddItem(itemData); toast.success('BOM 품목이 추가되었습니다'); } setIsDialogOpen(false); }; const handleDelete = (id: number) => { if (confirm('이 BOM 품목을 삭제하시겠습니까?')) { onDeleteItem(id); toast.success('BOM 품목이 삭제되었습니다'); } }; return ( <>
{title} {description}
{bomItems.length === 0 ? (

BOM 품목을 추가하여면 위의 버튼을 클릭하세요

품목의 품목명, 날짜, 상태정보 관리하는 수 있습니다

) : (
{bomItems.map((item) => (
{item.item_name} {item.item_code && ( {item.item_code} )}
수량: {item.quantity} {item.unit || 'EA'} {item.spec && • {item.spec}}
))}
)}
{/* BOM 품목 추가/수정 다이얼로그 */} { setIsDialogOpen(open); if (!open) { setEditingId(null); } }} > {editingId ? 'BOM 품목 수정' : 'BOM 품목 추가'} 하위 품목 정보를 입력하세요
setItemCode(e.target.value)} placeholder="예: PART-001" />
setItemName(e.target.value)} placeholder="예: 샤프트" />
setQuantity(e.target.value)} placeholder="1" min="0" step="0.01" />
setNote(e.target.value)} placeholder="추가 정보를 입력하세요" />
); }