"use client"

import * as React from "react"
import { Button } from "@/components/ui/button"
import { Loader2, RefreshCw, Download, CheckCircle, XCircle } from "lucide-react"
import type { Table } from "@tanstack/react-table"
import { CreatePcrDialog } from "./create-pcr-dialog"
import { ApproveRejectPcrDialog } from "./approve-reject-pcr-dialog"
import { PcrPoData } from "@/lib/pcr/types"

interface PcrTableToolbarActionsProps<TData> {
  selection: Table<TData>
  onRefresh: () => void
  isEvcpPage?: boolean
  isPartnersPage?: boolean
  currentVendorId?: number
}

export function PcrTableToolbarActions<TData>({
  selection,
  onRefresh,
  isEvcpPage = false,
  isPartnersPage = false,
  currentVendorId,
}: PcrTableToolbarActionsProps<TData>) {
  const [approveDialogOpen, setApproveDialogOpen] = React.useState(false)
  const [rejectDialogOpen, setRejectDialogOpen] = React.useState(false)
  const [selectedPcr, setSelectedPcr] = React.useState<PcrPoData | null>(null)

  // 선택된 행들 가져오기
  const selectedRows = selection.getSelectedRowModel().rows
  const selectedPcrData = selectedRows.length === 1 ? (selectedRows[0].original as PcrPoData) : null

  // 승인/거절 가능 여부 확인
  // const canApproveOrReject = selectedPcrData && selectedPcrData.pcrApprovalStatus === '승인대기'
  const canApproveOrReject = selectedPcrData // 임시로 주석 처리하여 테스트



  const handleExport = () => {
    // 추후 구현
    console.log("Export functionality to be implemented")
  }

  const handleApprove = () => {
    if (selectedPcrData) {
      setSelectedPcr(selectedPcrData)
      setApproveDialogOpen(true)
    }
  }

  const handleReject = () => {
    if (selectedPcrData) {
      setSelectedPcr(selectedPcrData)
      setRejectDialogOpen(true)
    }
  }

  const handleActionSuccess = () => {
    // 액션 성공 시 선택 해제
    selection.resetRowSelection()
    // revalidatePath로 인해 자동으로 페이지 새로고침됨
  }

  return (
    <div className="flex items-center gap-2">
      {isPartnersPage && (
      <>
      {/* 승인 버튼 */}
      <Button
        variant="default"
        size="sm"
        onClick={handleApprove}
        disabled={!canApproveOrReject}
        className="gap-2 bg-green-600 hover:bg-green-700"
      >
        <CheckCircle className="size-4" />
        승인
      </Button>

      {/* 거절 버튼 */}
      <Button
        variant="destructive"
        size="sm"
        onClick={handleReject}
        disabled={!canApproveOrReject}
        className="gap-2"
      >
        <XCircle className="size-4" />
        거절
      </Button>
      </>
      )}

      {/* PCR 생성 다이얼로그 - Partners 페이지에서는 표시하지 않음 */}
      {!isPartnersPage && (
        <CreatePcrDialog
          isEvcpPage={isEvcpPage}
          currentVendorId={currentVendorId}
          onSuccess={onRefresh}
        />
      )}

      {/* 승인 다이얼로그 */}
      <ApproveRejectPcrDialog
        open={approveDialogOpen}
        onOpenChange={setApproveDialogOpen}
        pcrData={selectedPcr}
        actionType="approve"
        onSuccess={handleActionSuccess}
      />

      {/* 거절 다이얼로그 */}
      <ApproveRejectPcrDialog
        open={rejectDialogOpen}
        onOpenChange={setRejectDialogOpen}
        pcrData={selectedPcr}
        actionType="reject"
        onSuccess={handleActionSuccess}
      />
    </div>
  )
}
