"use client"; import * as React from "react"; import { useState, useEffect } from "react"; import { formatDate } from "@/lib/utils"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Skeleton } from "@/components/ui/skeleton"; import { Badge } from "@/components/ui/badge"; import { ProcurementRfqsView } from "@/db/schema"; import { fetchPrItemsByRfqId } from "../services"; import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import { Input } from "@/components/ui/input"; import { Search } from "lucide-react"; // PR 항목 타입 정의 interface PrItemView { id: number; procurementRfqsId: number; rfqItem: string | null; prItem: string | null; prNo: string | null; itemId: number | null; materialCode: string | null; materialCategory: string | null; acc: string | null; materialDescription: string | null; size: string | null; deliveryDate: Date | null; quantity: number | null; uom: string | null; grossWeight: number | null; gwUom: string | null; specNo: string | null; specUrl: string | null; trackingNo: string | null; majorYn: boolean | null; projectDef: string | null; projectSc: string | null; projectKl: string | null; projectLc: string | null; projectDl: string | null; remark: string | null; rfqCode: string | null; itemCode: string | null; itemName: string | null; } interface PrDetailsDialogProps { open: boolean; onOpenChange: (open: boolean) => void; selectedRfq: ProcurementRfqsView | null; } export function PrDetailsDialog({ open, onOpenChange, selectedRfq, }: PrDetailsDialogProps) { const [isLoading, setIsLoading] = useState(false); const [prItems, setPrItems] = useState([]); const [searchTerm, setSearchTerm] = useState(""); // 검색어로 필터링된 항목들 const filteredItems = React.useMemo(() => { if (!searchTerm.trim()) return prItems; const term = searchTerm.toLowerCase(); return prItems.filter(item => (item.materialDescription || "").toLowerCase().includes(term) || (item.materialCode || "").toLowerCase().includes(term) || (item.prNo || "").toLowerCase().includes(term) || (item.prItem || "").toLowerCase().includes(term) || (item.rfqItem || "").toLowerCase().includes(term) ); }, [prItems, searchTerm]); // 선택된 RFQ가 변경되면 PR 항목 데이터를 가져옴 useEffect(() => { async function loadPrItems() { if (!selectedRfq || !open) { setPrItems([]); return; } try { setIsLoading(true); const result = await fetchPrItemsByRfqId(selectedRfq.id); const mappedItems: PrItemView[] = result.data.map(item => ({ ...item, // procurementRfqsId가 null이면 selectedRfq.id 사용 procurementRfqsId: item.procurementRfqsId ?? selectedRfq.id, // 기타 필요한 필드에 대한 기본값 처리 rfqItem: item.rfqItem ?? null, prItem: item.prItem ?? null, prNo: item.prNo ?? null, // 다른 필드도 필요에 따라 추가 })); setPrItems(mappedItems); } catch (error) { console.error("PR 항목 로드 오류:", error); setPrItems([]); } finally { setIsLoading(false); } } if (open) { loadPrItems(); setSearchTerm(""); } }, [selectedRfq, open]); // 선택된 RFQ가 없는 경우 if (!selectedRfq) { return null; } return ( PR 상세 정보 - {selectedRfq.rfqCode} 프로젝트: {selectedRfq.projectName} ({selectedRfq.projectCode}) | 건수:{" "} {selectedRfq.prItemsCount || 0}건 {isLoading ? (
) : (
{/* 검색 필드 */}
setSearchTerm(e.target.value)} className="pl-8" />
{filteredItems.length === 0 ? (
{prItems.length === 0 ? "PR 항목이 없습니다" : "검색 결과가 없습니다"}
) : (
총 {filteredItems.length}개 항목 (전체 {prItems.length}개 중) RFQ Item PR 번호 PR Item 자재그룹 자재 코드 자재 카테고리 ACC 자재 설명 규격 납품일 수량 UOM 총중량 중량 단위 사양 번호 사양 URL 추적 번호 주요 항목 프로젝트 DEF 프로젝트 SC 프로젝트 KL 프로젝트 LC 프로젝트 DL 비고 {filteredItems.map((item) => ( {item.rfqItem || "-"} {item.prNo || "-"} {item.prItem || "-"} {item.itemCode || "-"} {item.materialCode || "-"} {item.materialCategory || "-"} {item.acc || "-"} {item.materialDescription || "-"} {item.size || "-"} {item.deliveryDate ? formatDate(item.deliveryDate) : "-"} {item.quantity || "-"} {item.uom || "-"} {item.grossWeight || "-"} {item.gwUom || "-"} {item.specNo || "-"} {item.specUrl || "-"} {item.trackingNo || "-"} {item.majorYn ? ( 주요 ) : ( "아니오" )} {item.projectDef || "-"} {item.projectSc || "-"} {item.projectKl || "-"} {item.projectLc || "-"} {item.projectDl || "-"} {item.remark || "-"} ))}
)}
)}
); }