'use client' import * as React from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Checkbox } from '@/components/ui/checkbox' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, } from '@/components/ui/command' import { Popover, PopoverContent, PopoverTrigger, } from '@/components/ui/popover' import { Check, ChevronsUpDown, Search } from 'lucide-react' import { cn } from '@/lib/utils' import { createBiddingDetailVendor } from '@/lib/bidding/detail/service' import { searchVendorsForBidding } from '@/lib/bidding/service' import { useToast } from '@/hooks/use-toast' import { useTransition } from 'react' interface BiddingDetailVendorCreateDialogProps { biddingId: number open: boolean onOpenChange: (open: boolean) => void onSuccess: () => void } interface Vendor { id: number vendorName: string vendorCode: string status: string } export function BiddingDetailVendorCreateDialog({ biddingId, open, onOpenChange, onSuccess }: BiddingDetailVendorCreateDialogProps) { const { toast } = useToast() const [isPending, startTransition] = useTransition() // Vendor 검색 상태 const [vendors, setVendors] = React.useState([]) const [selectedVendor, setSelectedVendor] = React.useState(null) const [vendorSearchOpen, setVendorSearchOpen] = React.useState(false) const [vendorSearchValue, setVendorSearchValue] = React.useState('') // 폼 상태 (간소화 - 필수 항목만) const [formData, setFormData] = React.useState({ awardRatio: 100, // 기본 100% }) // Vendor 검색 React.useEffect(() => { const search = async () => { if (vendorSearchValue.trim().length < 2) { setVendors([]) return } try { const result = await searchVendorsForBidding(vendorSearchValue.trim(), biddingId, 10) setVendors(result) } catch (error) { console.error('Vendor search failed:', error) setVendors([]) } } const debounceTimer = setTimeout(search, 300) return () => clearTimeout(debounceTimer) }, [vendorSearchValue]) const handleVendorSelect = (vendor: Vendor) => { setSelectedVendor(vendor) setVendorSearchValue(`${vendor.vendorName} (${vendor.vendorCode})`) setVendorSearchOpen(false) } const handleCreate = () => { if (!selectedVendor) { toast({ title: '오류', description: '업체를 선택해주세요.', variant: 'destructive', }) return } startTransition(async () => { const response = await createBiddingDetailVendor( biddingId, selectedVendor.id ) if (response.success) { toast({ title: '성공', description: response.message, }) onOpenChange(false) resetForm() onSuccess() } else { toast({ title: '오류', description: response.error, variant: 'destructive', }) } }) } const resetForm = () => { setSelectedVendor(null) setVendorSearchValue('') setFormData({ awardRatio: 100, // 기본 100% }) } return ( 협력업체 추가 검색해서 업체를 선택하고 견적 정보를 입력해주세요.
{/* Vendor 검색 */}
{vendorSearchValue.length < 2 ? "최소 2자 이상 입력해주세요" : "검색 결과가 없습니다"} {vendors.map((vendor) => ( handleVendorSelect(vendor)} >
{vendor.vendorName} {vendor.vendorCode}
))}
) }