"use client"; import React, { useEffect, useState } from "react"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Loader2 } from "lucide-react"; import { apiClient } from "@/lib/api/client"; import { toast } from "sonner"; export interface CustomerRow { objid?: string | number; id?: string | number; customer_name?: string; contact_person?: string; business_number?: string; contact_phone?: string; address?: string; customer_code?: string; [key: string]: any; } /** * customer_mng.id (정수) → contract_mgmt.customer_objid 'C_xxxxxxxxxx' (10자리 padded) * 영업관리(contract_mgmt) 테이블이 사용하는 포맷. */ export const toContractCustomerObjid = (id: number | string | null | undefined) => { if (id === null || id === undefined || id === "") return ""; return `C_${String(id).padStart(10, "0")}`; }; interface CustomerSearchDialogProps { open: boolean; onOpenChange: (open: boolean) => void; onSelect: (customer: CustomerRow) => void; title?: string; description?: string; } export function CustomerSearchDialog({ open, onOpenChange, onSelect, title = "거래처 검색", description = "거래처를 검색하여 선택하세요.", }: CustomerSearchDialogProps) { const [keyword, setKeyword] = useState(""); const [results, setResults] = useState([]); const [loading, setLoading] = useState(false); useEffect(() => { if (open) { setKeyword(""); setResults([]); void search(""); } }, [open]); const search = async (kw?: string) => { const k = kw ?? keyword; setLoading(true); try { const filters: any[] = []; if (k) filters.push({ columnName: "customer_name", operator: "contains", value: k }); const res = await apiClient.post("/table-management/tables/customer_mng/data", { page: 1, size: 50, dataFilter: filters.length > 0 ? { enabled: true, filters } : undefined, autoFilter: true, }); const resData = res.data?.data; setResults(resData?.data || resData?.rows || []); } catch { toast.error("거래처 조회 실패"); } finally { setLoading(false); } }; const handleSelect = (cust: CustomerRow) => { onSelect(cust); onOpenChange(false); }; return ( {title} {description}
setKeyword(e.target.value)} onKeyDown={(e) => e.key === "Enter" && search()} className="text-sm" />
{results.length === 0 ? ( ) : results.map((row, i) => ( handleSelect(row)} > ))}
거래처명 대표자 사업자번호 연락처 선택
{loading ? "검색 중..." : "검색 결과가 없습니다."}
{row.customer_name || "-"} {row.contact_person || "-"} {row.business_number || "-"} {row.contact_phone || "-"}
); }