summaryrefslogtreecommitdiff
path: root/lib/pcr/table/detail-table/pcr-detail-table.tsx
blob: 65a07146d391947bdb6639c01b3af12a2bec29ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use client"

import * as React from "react"
import { useEffect, useState, useCallback } from "react"
import { getPcrPrListByPoContractNumber } from "@/lib/pcr/service"
import { PcrPoData, PcrPrData } from "@/lib/pcr/types"
import { ClientDataTable } from "@/components/client-data-table/data-table"
import { getPcrDetailColumns } from "./pcr-detail-column"
import { PcrDetailToolbarAction } from "./pcr-detail-toolbar-action"
import { toast } from "sonner"
import { Skeleton } from "@/components/ui/skeleton"
import { Badge } from "@/components/ui/badge"

// 프로퍼티 정의
interface PcrDetailTablesProps {
  selectedPcrPo: PcrPoData | null
  maxHeight?: string | number
  isPartnersPage?: boolean
}

export function PcrDetailTables({ selectedPcrPo, maxHeight, isPartnersPage = false }: PcrDetailTablesProps) {
  // 상태 관리
  const [isLoading, setIsLoading] = useState(false)
  const [details, setDetails] = useState<PcrPrData[]>([])

  // 데이터 새로고침 함수
  const handleRefreshData = useCallback(async () => {
    if (!selectedPcrPo?.poContractNumber) {
      setDetails([])
      return
    }

    try {
      setIsLoading(true)

      const result = await getPcrPrListByPoContractNumber(selectedPcrPo.poContractNumber)

      // 데이터 변환
      const transformedData = result.map(item => ({
        ...item,
        // 필요한 추가 필드 변환
      }))

      setDetails(transformedData)
    } catch (error) {
      console.error("PCR_PR 데이터 로드 오류:", error)
      setDetails([])
      toast.error("PCR_PR 데이터를 불러오는 중 오류가 발생했습니다")
    } finally {
      setIsLoading(false)
    }
  }, [selectedPcrPo?.poContractNumber])

  // selectedPcrPo가 변경될 때 데이터 로드
  useEffect(() => {
    handleRefreshData()
  }, [handleRefreshData])

  // 칼럼 정의
  const columns = React.useMemo(() =>
    getPcrDetailColumns(),
    []
  )

  if (!selectedPcrPo) {
    return (
      <div className="flex items-center justify-center h-full text-muted-foreground">
        PCR_PO를 선택하세요
      </div>
    )
  }

  // 로딩 중인 경우
  if (isLoading) {
    return (
      <div className="p-4 space-y-4">
        <Skeleton className="h-8 w-1/2" />
        <Skeleton className="h-24 w-full" />
        <Skeleton className="h-48 w-full" />
      </div>
    )
  }

  return (
    <div className="h-full overflow-hidden">
      {/* 헤더 정보 */}
      <div className="p-4 bg-muted/50 border-b">
        <div className="flex items-center justify-between">
          <div className="space-y-1">
            <h3 className="text-sm font-medium">
              PO/계약번호: {selectedPcrPo.poContractNumber}
            </h3>
            <p className="text-xs text-muted-foreground">
              프로젝트: {selectedPcrPo.project || "N/A"}
            </p>
          </div>
          <div className="flex items-center gap-2">
            <Badge variant="outline">
              {details.length}건
            </Badge>
                <PcrDetailToolbarAction
                  selectedPcrPo={selectedPcrPo}
                  onRefresh={handleRefreshData}
                  isPartnersPage={isPartnersPage}
                />
          </div>
        </div>
      </div>

      {/* 테이블 표시 */}
      <ClientDataTable
        columns={columns}
        data={details}
        maxHeight={maxHeight}
        emptyStateMessage="PCR_PR 데이터가 없습니다. 새 항목을 생성해보세요."
      />
    </div>
  )
}