'use client'

import * as React from 'react'
import { Bidding } from '@/db/schema'
import { BiddingDetailVendorTableContent } from '../detail/table/bidding-detail-vendor-table'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { getBiddingDetailData } from '../detail/service'

interface VendorSelectionTableProps {
  biddingId: number
  bidding: Bidding
  onRefresh: () => void
}

export function VendorSelectionTable({ biddingId, bidding, onRefresh }: VendorSelectionTableProps) {
  const [vendors, setVendors] = React.useState<any[]>([])
  const [loading, setLoading] = React.useState(true)

  React.useEffect(() => {
    const loadData = async () => {
      try {
        setLoading(true)
        const data = await getBiddingDetailData(biddingId)
        setVendors(data.quotationVendors)
      } catch (error) {
        console.error('Failed to load vendors:', error)
      } finally {
        setLoading(false)
      }
    }

    loadData()
  }, [biddingId])

  if (loading) {
    return (
      <Card>
        <CardHeader>
          <CardTitle>업체선정</CardTitle>
        </CardHeader>
        <CardContent>
          <div className="flex items-center justify-center py-8">
            <div className="text-sm text-muted-foreground">로딩 중...</div>
          </div>
        </CardContent>
      </Card>
    )
  }

  return (
    <Card>
      <CardHeader>
        <CardTitle>업체선정</CardTitle>
      </CardHeader>
      <CardContent>
        <BiddingDetailVendorTableContent
          biddingId={biddingId}
          bidding={bidding}
          vendors={vendors}
          onRefresh={onRefresh}
          onOpenSelectionReasonDialog={() => {}}
        />
      </CardContent>
    </Card>
  )
}
