'use client'

import * as React from 'react'

// 브라우저 환경 체크
const isBrowser = typeof window !== 'undefined'
import { useRouter } from 'next/navigation'
import { useTransition } from 'react'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge'
import { Label } from '@/components/ui/label'
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'
import {
  ArrowLeft,
  User,
  Users,
  Send,
  CheckCircle,
  XCircle,
  Save,
  FileText,
  Package,
  Trash2,
  Calendar,
  ChevronDown
} from 'lucide-react'
import { format } from 'date-fns'
import { formatDate } from '@/lib/utils'
import {
  getBiddingDetailsForPartners,
  submitPartnerResponse,
  updatePartnerBiddingParticipation,
  saveBiddingDraft,
  getPriceAdjustmentFormByBiddingCompanyId,
  getPartnerBiddingItemQuotations
} from '../detail/service'
import { cancelBiddingResponse } from '../detail/bidding-actions'
import { getPrItemsForBidding } from '@/lib/bidding/pre-quote/service'
import { getBiddingConditions } from '@/lib/bidding/service'
import { PrItemsPricingTable } from './components/pr-items-pricing-table'
import { SimpleFileUpload } from './components/simple-file-upload'
import { getTaxConditionName } from "@/lib/tax-conditions/types"
import {
  biddingStatusLabels,
  contractTypeLabels,
  biddingTypeLabels
} from '@/db/schema'
import { useToast } from '@/hooks/use-toast'
import { useSession } from 'next-auth/react'
import { getBiddingNotice } from '../service'
import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'

interface PartnersBiddingDetailProps {
  biddingId: number
  companyId: number
}

interface BiddingDetail {
  id: number
  biddingNumber: string
  revision: number | null
  projectName: string | null
  itemName: string | null
  title: string
  description: string | null
  contractType: string
  biddingType: string
  awardCount: string | null
  contractStartDate: Date | string | null
  contractEndDate: Date | string | null
  preQuoteDate: Date | string | null
  biddingRegistrationDate: Date | string | null
  submissionStartDate: Date | string | null
  submissionEndDate: Date | string | null
  evaluationDate: Date | string | null
  currency: string
  budget: number | null
  targetPrice: number | null
  status: string
  bidPicName: string | null // 입찰담당자
  bidPicPhone?: string | null // 입찰담당자 전화번호
  supplyPicName: string | null // 조달담당자
  biddingCompanyId: number
  biddingId: number
  invitationStatus: string
  finalQuoteAmount: number | null
  finalQuoteSubmittedAt: Date | null
  isFinalSubmission: boolean
  isWinner: boolean
  isAttendingMeeting: boolean | null
  isBiddingParticipated: boolean | null
  additionalProposals: string | null
  responseSubmittedAt: Date | null
  priceAdjustmentResponse: boolean | null // 연동제 적용 여부
  isPreQuoteParticipated: boolean | null // 입찰 참여 여부
  isPriceAdjustmentApplicableQuestion: boolean // 연동제 적용요건 문의 여부
}

interface PrItem {
  id: number
  biddingId: number
  itemNumber: string | null
  projectId: number | null
  projectInfo: string | null
  itemInfo: string | null
  shi: string | null
  materialGroupNumber: string | null
  materialGroupInfo: string | null
  materialNumber: string | null
  materialInfo: string | null
  requestedDeliveryDate: Date | string | null
  annualUnitPrice: string | null
  currency: string | null
  quantity: string | null
  quantityUnit: string | null
  totalWeight: string | null
  weightUnit: string | null
  priceUnit: string | null
  purchaseUnit: string | null
  materialWeight: string | null
  prNumber: string | null
  hasSpecDocument: boolean | null
  specification: string | null
  bidUnitPrice?: string | number | null
  bidAmount?: string | number | null
  proposedDeliveryDate?: string | Date | null
  technicalSpecification?: string | null
}

interface BiddingPrItemQuotation {
  prItemId: number
  bidUnitPrice: number
  bidAmount: number
  proposedDeliveryDate?: string
  technicalSpecification?: string
}

interface BiddingConditions {
  id?: number
  biddingId?: number
  paymentTerms?: string
  taxConditions?: string
  incoterms?: string
  incotermsOption?: string
  contractDeliveryDate?: string
  shippingPort?: string
  destinationPort?: string
  isPriceAdjustmentApplicable?: boolean
  sparePartOptions?: string
  createdAt?: string
  updatedAt?: string
}

export function PartnersBiddingDetail({ biddingId, companyId }: PartnersBiddingDetailProps) {
  const router = useRouter()
  const { toast } = useToast()
  const session = useSession()
  const [isPending, startTransition] = useTransition()
  const [biddingDetail, setBiddingDetail] = React.useState<BiddingDetail | null>(null)
  const [isLoading, setIsLoading] = React.useState(true)
  const [isUpdatingParticipation, setIsUpdatingParticipation] = React.useState(false)
  const [isSavingDraft, setIsSavingDraft] = React.useState(false)
  const [isSubmitting, setIsSubmitting] = React.useState(false)
  const [isCancelling, setIsCancelling] = React.useState(false)
  const [isFinalSubmission, setIsFinalSubmission] = React.useState(false)
  const [isBiddingNoticeLoading, setIsBiddingNoticeLoading] = React.useState(false)
  const [isExpired, setIsExpired] = React.useState(false)

  // 입찰공고 관련 상태
  const [biddingNotice, setBiddingNotice] = React.useState<{
    id?: number
    biddingId?: number | null
    title?: string
    content?: string
    isTemplate?: boolean
    createdAt?: string | Date
    updatedAt?: string | Date
  } | null>(null)
  const [biddingConditions, setBiddingConditions] = React.useState<BiddingConditions | null>(null)
  const [isNoticeOpen, setIsNoticeOpen] = React.useState(false)

  // 품목별 입찰 관련 상태
  const [prItems, setPrItems] = React.useState<PrItem[]>([])
  const [prItemQuotations, setPrItemQuotations] = React.useState<BiddingPrItemQuotation[]>([])
  const [totalQuotationAmount, setTotalQuotationAmount] = React.useState(0)

  // 응찰 폼 상태
  const [responseData, setResponseData] = React.useState({
    finalQuoteAmount: '',
    proposedContractDeliveryDate: '',
    additionalProposals: '',
    priceAdjustmentResponse: null as boolean | null, // null: 미선택, true: 적용, false: 미적용
  })

  // 연동제 폼 상태
  const [priceAdjustmentForm, setPriceAdjustmentForm] = React.useState({
    itemName: '',
    adjustmentReflectionPoint: '',
    majorApplicableRawMaterial: '',
    adjustmentFormula: '',
    rawMaterialPriceIndex: '',
    referenceDate: '',
    comparisonDate: '',
    adjustmentRatio: '',
    notes: '',
    adjustmentConditions: '',
    majorNonApplicableRawMaterial: '',
    adjustmentPeriod: '',
    contractorWriter: '',
    adjustmentDate: '',
    nonApplicableReason: '', // 연동제 미희망 사유
  })
  const userId = session.data?.user?.id || ''


  // 데이터 로드
  // 입찰공고 로드 함수
  const loadBiddingNotice = React.useCallback(async () => {
    setIsBiddingNoticeLoading(true)
    try {
      const notice = await getBiddingNotice(biddingId)
      console.log('입찰공고 로드 성공:', notice)
      setBiddingNotice(notice)
    } catch (error) {
      console.error('Failed to load bidding notice:', error)
    } finally {
      setIsBiddingNoticeLoading(false)
    }
  }, [biddingId])

  React.useEffect(() => {
    const loadData = async () => {
      try {
        setIsLoading(true)
        // 데이터 로드 시작 로그
        console.log('입찰 상세 데이터 로드 시작:', { biddingId, companyId })

        console.log('데이터베이스 쿼리 시작...')

        const [result, prItemsResult, noticeResult, conditionsResult] = await Promise.all([
          getBiddingDetailsForPartners(biddingId, companyId).catch(error => {
            console.error('Failed to get bidding details:', error)
            return null
          }),
          getPrItemsForBidding(biddingId, companyId).catch(error => {
            console.error('Failed to get PR items:', error)
            return []
          }),
          loadBiddingNotice().catch(error => {
            console.error('Failed to load bidding notice:', error)
            return null
          }),
          getBiddingConditions(biddingId).catch(error => {
            console.error('Failed to load bidding conditions:', error)
            return null
          })
        ])

        console.log('데이터베이스 쿼리 완료:', {
          resultExists: !!result,
          prItemsExists: !!prItemsResult,
          noticeExists: !!noticeResult,
          conditionsExists: !!conditionsResult
        })

        console.log('데이터 로드 완료:', {
          result: !!result,
          prItemsCount: Array.isArray(prItemsResult) ? prItemsResult.length : 0,
          noticeResult: !!noticeResult,
          conditionsResult: !!conditionsResult
        })

        if (result) {
          console.log('입찰 상세 데이터 로드 성공:', {
            biddingId: result.biddingId,
            isBiddingParticipated: result.isBiddingParticipated,
            invitationStatus: result.invitationStatus,
            finalQuoteAmount: result.finalQuoteAmount
          })

          setBiddingDetail(result)
          
          // 만료 여부 확인
          if (result.submissionEndDate) {
            const now = new Date()
            const deadline = new Date(result.submissionEndDate)
            setIsExpired(deadline < now)
          }

          // 기존 응답 데이터로 폼 초기화
          setResponseData({
            finalQuoteAmount: result.finalQuoteAmount?.toString() || '',
            proposedContractDeliveryDate: result.proposedContractDeliveryDate || '',
            additionalProposals: result.additionalProposals || '',
            priceAdjustmentResponse: result.priceAdjustmentResponse || null,
          })

          // 입찰 조건 로드
          if (conditionsResult) {
            console.log('입찰 조건 로드:', conditionsResult)
            setBiddingConditions(conditionsResult)
          }
        }

        // PR 아이템 설정
        setPrItems(prItemsResult)

        // PR 아이템 결과로부터 견적 정보 추출 및 설정
        if (Array.isArray(prItemsResult) && prItemsResult.length > 0) {
          const initialQuotations = prItemsResult.map((item: any) => ({
            prItemId: item.id,
            bidUnitPrice: item.bidUnitPrice ? Number(item.bidUnitPrice) : 0,
            bidAmount: item.bidAmount ? Number(item.bidAmount) : 0,
            proposedDeliveryDate: item.proposedDeliveryDate ? (item.proposedDeliveryDate instanceof Date ? item.proposedDeliveryDate.toISOString().split('T')[0] : item.proposedDeliveryDate) : undefined,
            technicalSpecification: item.technicalSpecification || undefined
          }));
          setPrItemQuotations(initialQuotations);
          
          // 총 금액 계산
          const total = initialQuotations.reduce((sum: number, q: any) => sum + q.bidAmount, 0);
          setTotalQuotationAmount(total);
          
          // 응찰 확정 시 총 금액 설정
          if (total > 0 && result?.isBiddingParticipated === true) {
            setResponseData(prev => ({
              ...prev,
              finalQuoteAmount: total.toString()
            }));
          }
        }

        // 입찰 데이터를 본입찰용으로 로드 (응찰 확정 시 또는 입찰이 있는 경우)
        if (result?.biddingCompanyId) {
          try {
            
            // 연동제 데이터 로드 (입찰에서 답변했으면 로드, 아니면 입찰 조건 확인)
            if (result.priceAdjustmentResponse !== null) {
              // 입찰에서 이미 답변한 경우 - 연동제 폼 로드
              const savedPriceAdjustmentForm = await getPriceAdjustmentFormByBiddingCompanyId(result.biddingCompanyId)
              if (savedPriceAdjustmentForm) {
                setPriceAdjustmentForm({
                  itemName: savedPriceAdjustmentForm.itemName || '',
                  adjustmentReflectionPoint: savedPriceAdjustmentForm.adjustmentReflectionPoint || '',
                  majorApplicableRawMaterial: savedPriceAdjustmentForm.majorApplicableRawMaterial || '',
                  adjustmentFormula: savedPriceAdjustmentForm.adjustmentFormula || '',
                  rawMaterialPriceIndex: savedPriceAdjustmentForm.rawMaterialPriceIndex || '',
                  referenceDate: savedPriceAdjustmentForm.referenceDate || '',
                  comparisonDate: savedPriceAdjustmentForm.comparisonDate || '',
                  adjustmentRatio: savedPriceAdjustmentForm.adjustmentRatio || '',
                  notes: savedPriceAdjustmentForm.notes || '',
                  adjustmentConditions: savedPriceAdjustmentForm.adjustmentConditions || '',
                  majorNonApplicableRawMaterial: savedPriceAdjustmentForm.majorNonApplicableRawMaterial || '',
                  adjustmentPeriod: savedPriceAdjustmentForm.adjustmentPeriod || '',
                  contractorWriter: savedPriceAdjustmentForm.contractorWriter || '',
                  adjustmentDate: savedPriceAdjustmentForm.adjustmentDate || '',
                  nonApplicableReason: savedPriceAdjustmentForm.nonApplicableReason || '',
                })
              }
            }
          } catch (error) {
            console.error('Failed to load pre-quote data:', error)
          }
        }
      } catch (error) {
        console.error('Failed to load bidding detail:', error)
        toast({
          title: '오류',
          description: '입찰 정보를 불러오는데 실패했습니다.',
          variant: 'destructive',
        })
      } finally {
        setIsLoading(false)
      }
    }

    loadData()
  }, [biddingId, companyId, toast])

  // 입찰 참여여부 결정 핸들러
  const handleParticipationDecision = async (participated: boolean) => {
    if (!biddingDetail) return

    // 만료 체크
    if (isExpired) {
      toast({
        title: "참여 불가",
        description: "제출 마감일이 지났습니다. 더 이상 입찰에 참여할 수 없습니다.",
        variant: "destructive",
      })
      return
    }

    setIsUpdatingParticipation(true)
    try {
      const result = await updatePartnerBiddingParticipation(
        biddingDetail.biddingCompanyId,
        participated,
        userId
      )

      if (result.success) {
        toast({
          title: participated ? '참여 확정' : '미참여 확정',
          description: participated 
            ? '입찰에 참여하셨습니다. 이제 입찰을 작성할 수 있습니다.' 
            : '입찰 참여를 거절하셨습니다.',
        })

        // 데이터 새로고침
        const updatedDetail = await getBiddingDetailsForPartners(biddingId, companyId)
        if (updatedDetail) {
          setBiddingDetail(updatedDetail)
          
          // 참여 확정 시 입찰 데이터가 있다면 로드
          if (participated && updatedDetail.biddingCompanyId) {
            try {
              const preQuoteData = await getPartnerBiddingItemQuotations(updatedDetail.biddingCompanyId)

              if (preQuoteData && Array.isArray(preQuoteData) && preQuoteData.length > 0) {
                console.log('참여확정 후 입찰 데이터:', preQuoteData)

                const convertedQuotations = preQuoteData
                  .filter(item => item && typeof item === 'object' && item.prItemId)
                  .map(item => ({
                    prItemId: item.prItemId,
                    bidUnitPrice: item.bidUnitPrice,
                    bidAmount: item.bidAmount,
                    proposedDeliveryDate: item.proposedDeliveryDate || undefined,
                    technicalSpecification: item.technicalSpecification || undefined
                  }))

                console.log('참여확정 후 변환된 입찰 데이터:', convertedQuotations)

                if (Array.isArray(convertedQuotations) && convertedQuotations.length > 0) {
                  setPrItemQuotations(convertedQuotations)
                  const total = convertedQuotations.reduce((sum, q) => {
                    const amount = Number(q.bidAmount) || 0
                    return sum + amount
                  }, 0)
                  setTotalQuotationAmount(total)
                  console.log('참여확정 후 계산된 총 금액:', total)

                  if (total > 0) {
                    setResponseData(prev => ({
                      ...prev,
                      finalQuoteAmount: total.toString()
                    }))
                  }
                }
              }
            } catch (error) {
              console.error('Failed to load pre-quote data after participation:', error)
            }
          }
        }
      } else {
        toast({
          title: '오류',
          description: result.error,
          variant: 'destructive',
        })
      }
    } catch (error) {
      console.error('Failed to update participation:', error)
      toast({
        title: '오류',
        description: '참여여부 업데이트에 실패했습니다.',
        variant: 'destructive',
      })
    } finally {
      setIsUpdatingParticipation(false)
    }
  }

  // 품목별 입찰 변경 핸들러
  const handleQuotationsChange = (quotations: BiddingPrItemQuotation[]) => {
    console.log('입찰 변경:', quotations)
    setPrItemQuotations(quotations)
  }

  // 총 금액 변경 핸들러
  const handleTotalAmountChange = (total: number) => {
    setTotalQuotationAmount(total)
    // 자동으로 총 입찰 금액도 업데이트
    setResponseData(prev => ({
      ...prev,
      finalQuoteAmount: total.toString()
    }))
  }

  // 임시 저장 핸들러
  const handleSaveDraft = async () => {
    if (!biddingDetail || !userId) return

    // 제출 마감일 체크 (상태 사용)
    if (isExpired) {
      toast({
        title: "접근 제한",
        description: "제출 마감일이 지났습니다. 더 이상 입찰에 참여할 수 없습니다.",
        variant: "destructive",
      })
      return
    }

    // 입찰 마감 상태 체크
    const biddingStatus = biddingDetail.status
    const isClosed = biddingStatus === 'bidding_closed' || biddingStatus === 'vendor_selected' || biddingStatus === 'bidding_disposal'

    if (isClosed) {
      toast({
        title: "접근 제한",
        description: "입찰이 마감되어 더 이상 입찰에 참여할 수 없습니다.",
        variant: "destructive",
      })
      return
    }
    
    if (prItemQuotations.length === 0) {
      toast({
        title: '저장할 데이터 없음',
        description: '저장할 품목별 입찰이 없습니다.',
        variant: 'destructive',
      })
      return
    }

    setIsSavingDraft(true)
    try {
      const quotationsForSave = prItemQuotations.map(q => ({
        prItemId: q.prItemId,
        bidUnitPrice: q.bidUnitPrice,
        bidAmount: q.bidAmount,
        proposedDeliveryDate: q.proposedDeliveryDate,
        technicalSpecification: q.technicalSpecification
      }))
      
      console.log('임시저장 - prItemQuotations:', prItemQuotations)
      console.log('임시저장 - quotationsForSave:', quotationsForSave)
      
      const result = await saveBiddingDraft(
        biddingDetail.biddingCompanyId,
        quotationsForSave,
        userId
      )

      if (result.success) {
        toast({
          title: '임시 저장 완료',
          description: '품목별 입찰이 임시 저장되었습니다.',
        })
      } else {
        toast({
          title: '임시 저장 실패',
          description: result.error,
          variant: 'destructive',
        })
      }
    } catch (error) {
      console.error('Failed to save draft:', error)
      toast({
        title: '오류',
        description: '임시 저장에 실패했습니다.',
        variant: 'destructive',
      })
    } finally {
      setIsSavingDraft(false)
    }
  }

  // 응찰 포기 핸들러
  const handleCancelResponse = async () => {
    if (!biddingDetail || !userId) return

    // 최종제출한 경우 취소 불가
    if (biddingDetail.isFinalSubmission) {
      toast({
        title: '취소 불가',
        description: '최종 제출된 응찰은 취소할 수 없습니다.',
        variant: 'destructive',
      })
      return
    }

    if (isBrowser && !window.confirm('응찰을 취소하시겠습니까? 작성한 입찰 내용이 모두 삭제됩니다.')) {
      return
    }

    setIsCancelling(true)
    try {
      const result = await cancelBiddingResponse(biddingDetail.biddingCompanyId, userId)

      if (result.success) {
        toast({
          title: '응찰 포기 완료',
          description: '응찰이 취소되었습니다.',
        })
        // 페이지 새로고침
        if (isBrowser) {
          window.location.reload()
        } else {
          // 서버사이드에서는 라우터로 이동
          router.push(`/partners/bid/${biddingId}`)
        }
      } else {
        toast({
          title: '응찰 포기 실패',
          description: result.error,
          variant: 'destructive',
        })
      }
    } catch (error) {
      console.error('Failed to cancel bidding response:', error)
      toast({
        title: '오류',
        description: '응찰 포기에 실패했습니다.',
        variant: 'destructive',
      })
    } finally {
      setIsCancelling(false)
    }
  }

  const handleSubmitResponse = () => {
    if (!biddingDetail) return

    // 제출 마감일 체크 (상태 사용)
    if (isExpired) {
      toast({
        title: "접근 제한",
        description: "제출 마감일이 지났습니다. 더 이상 입찰에 참여할 수 없습니다.",
        variant: "destructive",
      })
      return
    }

    // 입찰 마감 상태 체크
    const biddingStatus = biddingDetail.status
    const isClosed = biddingStatus === 'bidding_closed' || biddingStatus === 'vendor_selected' || biddingStatus === 'bidding_disposal'

    if (isClosed) {
      toast({
        title: "접근 제한",
        description: "입찰이 마감되어 더 이상 입찰에 참여할 수 없습니다.",
        variant: "destructive",
      })
      return
    }

    // 필수값 검증
    if (!responseData.finalQuoteAmount.trim()) {
      toast({
        title: '유효성 오류',
        description: '입찰 금액을 입력해주세요.',
        variant: 'destructive',
      })
      return
    }

    // 품목별 입찰이 있는지 확인
    if (prItems.length > 0 && prItemQuotations.length === 0) {
      toast({
        title: '유효성 오류',
        description: '품목별 입찰을 작성해주세요.',
        variant: 'destructive',
      })
      return
    }

    setIsSubmitting(true)
    startTransition(async () => {
      try {
        // 1. 입찰 참여 상태를 응찰로 변경
        const participationResult = await updatePartnerBiddingParticipation(
          biddingDetail.biddingCompanyId,
          true, // 응찰
          userId
        )

        if (!participationResult.success) {
          throw new Error(participationResult.error)
        }

        // 2. 최종 입찰 응답 제출 (PR 아이템별 입찰 포함)
        const result = await submitPartnerResponse(
          biddingDetail.biddingCompanyId,
          {
            finalQuoteAmount: parseFloat(responseData.finalQuoteAmount),
            proposedContractDeliveryDate: responseData.proposedContractDeliveryDate,
            additionalProposals: responseData.additionalProposals,
            isFinalSubmission, // 최종제출 여부 추가
            // 연동제 데이터 추가 (연동제 적용요건 문의가 있는 경우만)
            priceAdjustmentResponse: biddingDetail.isPriceAdjustmentApplicableQuestion ? (responseData.priceAdjustmentResponse ?? false) as boolean : false,
            priceAdjustmentForm: biddingDetail.isPriceAdjustmentApplicableQuestion && (responseData.priceAdjustmentResponse ?? false) as boolean ? {
              ...priceAdjustmentForm,
              adjustmentRatio: parseFloat(priceAdjustmentForm.adjustmentRatio) || 0
            } : undefined,
            prItemQuotations: prItemQuotations.length > 0 ? prItemQuotations.map(q => ({
              prItemId: q.prItemId,
              bidUnitPrice: q.bidUnitPrice,
              bidAmount: q.bidAmount,
              proposedDeliveryDate: q.proposedDeliveryDate,
              technicalSpecification: q.technicalSpecification
            })) : undefined,
          },
          userId
        )

        if (result.success) {
          toast({
            title: isFinalSubmission ? '응찰 완료' : '임시 저장 완료',
            description: isFinalSubmission ? '입찰이 최종 제출되었습니다.' : '입찰이 임시 저장되었습니다.',
          })

          // 데이터 새로고침
          const updatedDetail = await getBiddingDetailsForPartners(biddingId, companyId)
          if (updatedDetail) {
            setBiddingDetail(updatedDetail)
          }
        } else {
          throw new Error(result.error)
        }
      } catch (error) {
        console.error('Failed to submit response:', error)
        toast({
          title: '오류',
          description: error instanceof Error ? error.message : '응찰 제출에 실패했습니다.',
          variant: 'destructive',
        })
      } finally {
        setIsSubmitting(false)
      }
    })
  }

  if (isLoading) {
    return (
      <div className="flex items-center justify-center py-12">
        <div className="text-center">
          <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary mx-auto mb-4"></div>
          <p className="text-muted-foreground">입찰 정보를 불러오는 중...</p>
        </div>
      </div>
    )
  }

  if (!biddingDetail) {
    return (
      <div className="text-center py-12">
        <p className="text-muted-foreground">입찰 정보를 찾을 수 없습니다.</p>
        <Button onClick={() => router.back()} className="mt-4">
          <ArrowLeft className="w-4 h-4 mr-2" />
          돌아가기
        </Button>
      </div>
    )
  }

  return (
    <div className="space-y-6">
      {/* 헤더 */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-4">
          <Button variant="outline" onClick={() => router.back()}>
            <ArrowLeft className="w-4 h-4 mr-2" />
            목록으로
          </Button>
          <div>
            <h1 className="text-2xl font-semibold">{biddingDetail.title}</h1>
            <div className="flex items-center gap-2 mt-1">
              <Badge variant="outline" className="font-mono text-xs">
                {biddingDetail.biddingNumber}
              </Badge>
              <Badge variant={
                biddingDetail.status === 'bidding_disposal' ? 'destructive' :
                biddingDetail.status === 'vendor_selected' ? 'default' :
                'secondary'
              }>
                {biddingStatusLabels[biddingDetail.status]}
              </Badge>
            </div>
          </div>
        </div>

        {/* 입찰 참여여부 상태 표시 */}
        <div className="flex items-center gap-2">
          <Badge variant={
            biddingDetail.isBiddingParticipated === null ? 'outline' :
            biddingDetail.isBiddingParticipated === true ? 'default' : 'destructive'
          }>
            {biddingDetail.isBiddingParticipated === null ? '참여 결정 대기' :
             biddingDetail.isBiddingParticipated === true ? '응찰' : '응찰포기'}
          </Badge>
        </div>

      </div>

      {/* 입찰 공고 섹션 */}
      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <FileText className="w-5 h-5" />
            입찰 공고
          </CardTitle>
        </CardHeader>
        <CardContent className="space-y-4">
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">

            <div>
              <Label className="text-sm font-medium text-muted-foreground">계약구분</Label>
              <div className="mt-1">{contractTypeLabels[biddingDetail.contractType]}</div>
            </div>
            <div>
              <Label className="text-sm font-medium text-muted-foreground">입찰유형</Label>
              <div className="mt-1">{biddingTypeLabels[biddingDetail.biddingType]}</div>
            </div>
            <div>
              <Label className="text-sm font-medium text-muted-foreground">낙찰업체 수</Label>
              <div className="mt-1">{biddingDetail.awardCount === 'single' ? '단수' : biddingDetail.awardCount === 'multiple' ? '복수' : '미설정'}</div>
            </div>
            <div>
              <Label className="text-sm font-medium text-muted-foreground">입찰담당자</Label>
              <div className="flex flex-col mt-1">
                <div className="flex items-center gap-2">
                  <User className="w-4 h-4" />
                  <span>{biddingDetail.bidPicName || '미설정'}</span>
                </div>
                {biddingDetail.bidPicPhone && (
                  <div className="text-xs text-muted-foreground ml-6">
                    {biddingDetail.bidPicPhone}
                  </div>
                )}
              </div>
            </div>
            <div>
              <Label className="text-sm font-medium text-muted-foreground">조달담당자</Label>
              <div className="flex items-center gap-2 mt-1">
                <User className="w-4 h-4" />
                <span>{biddingDetail.supplyPicName || '미설정'}</span>
              </div>
            </div>
          </div>

          {/* 계약기간 */}
          {biddingDetail.contractStartDate && biddingDetail.contractEndDate && (
            <div className="pt-4 border-t">
              <Label className="text-sm font-medium text-muted-foreground mb-2 block">계약기간</Label>
              <div className="p-3 bg-muted/50 rounded-lg">
                <div className="flex items-center gap-2 text-sm">
                  <span className="font-medium">{format(new Date(biddingDetail.contractStartDate), "yyyy-MM-dd")}</span>
                  <span className="text-muted-foreground">~</span>
                  <span className="font-medium">{format(new Date(biddingDetail.contractEndDate), "yyyy-MM-dd")}</span>
                </div>
              </div>
            </div>
          )}


          {/* 제출 마감일 D-day */}
            {biddingDetail.submissionEndDate && (
              <div className="pt-4 border-t">
                <Label className="text-sm font-medium text-muted-foreground mb-2 block">제출 마감 정보</Label>
                {(() => {
                  const now = new Date()
                  const deadline = new Date(biddingDetail.submissionEndDate)
                  // isExpired 상태 사용
                  const timeLeft = deadline.getTime() - now.getTime()
                  const daysLeft = Math.floor(timeLeft / (1000 * 60 * 60 * 24))
                  const hoursLeft = Math.floor((timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
                  const kstDeadline = new Date(deadline.getTime() + 9 * 60 * 60 * 1000).toISOString().slice(0, 16).replace('T', ' ')

                  return (
                    <div className={`p-3 rounded-lg border-2 ${
                      isExpired
                        ? 'border-red-200 bg-red-50'
                        : daysLeft <= 1
                          ? 'border-orange-200 bg-orange-50'
                          : 'border-green-200 bg-green-50'
                    }`}>
                      <div className="flex items-center justify-between">
                        <div className="flex items-center gap-2">
                          <Calendar className="w-5 h-5" />
                          <span className="font-medium">제출 마감일:</span>
                          <span className="text-lg font-semibold">
                            {kstDeadline}
                          </span>
                        </div>
                        {isExpired ? (
                          <Badge variant="destructive" className="ml-2">
                            마감됨
                          </Badge>
                        ) : daysLeft <= 1 ? (
                          <Badge variant="secondary" className="ml-2 bg-orange-100 text-orange-800">
                            {daysLeft === 0 ? `${hoursLeft}시간 남음` : `${daysLeft}일 남음`}
                          </Badge>
                        ) : (
                          <Badge variant="secondary" className="ml-2 bg-green-100 text-green-800">
                            {daysLeft}일 남음
                          </Badge>
                        )}
                      </div>
                      {isExpired && (
                        <div className="mt-2 text-sm text-red-600">
                          ⚠️ 제출 마감일이 지났습니다. 입찰 제출이 불가능합니다.
                        </div>
                      )}
                    </div>
                  )
                })()}
              </div>
            )}

          {/* 일정 정보 */}
          <div className="pt-4 border-t">
            <Label className="text-sm font-medium text-muted-foreground mb-2 block">일정 정보</Label>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-2 text-sm">
              {biddingDetail.submissionStartDate && biddingDetail.submissionEndDate && (
                <div>
                  <span className="font-medium">입찰서 제출기간:</span> {(() => {
                    const start = new Date(biddingDetail.submissionStartDate!)
                    const end = new Date(biddingDetail.submissionEndDate!)
                    const kstStart = new Date(start.getTime() + 9 * 60 * 60 * 1000).toISOString().slice(0, 16).replace('T', ' ')
                    const kstEnd = new Date(end.getTime() + 9 * 60 * 60 * 1000).toISOString().slice(0, 16).replace('T', ' ')
                    return `${kstStart} ~ ${kstEnd}`
                  })()}
                </div>
              )}
              {biddingDetail.evaluationDate && (
                <div>
                  <span className="font-medium">평가일:</span> {format(new Date(biddingDetail.evaluationDate), "yyyy-MM-dd HH:mm")}
                </div>
              )}
            </div>
          </div>
        </CardContent>
      </Card>

      {/* 입찰공고 토글 섹션 */}
      {biddingNotice && (
        <Card>
          <Collapsible open={isNoticeOpen} onOpenChange={setIsNoticeOpen}>
            <CollapsibleTrigger asChild>
              <CardHeader className="cursor-pointer hover:bg-muted/50 transition-colors">
                <CardTitle className="flex items-center justify-between">
                  <div className="flex items-center gap-2">
                    <FileText className="w-5 h-5" />
                    입찰공고 내용
                  </div>
                  <ChevronDown className={`w-5 h-5 transition-transform ${isNoticeOpen ? 'rotate-180' : ''}`} />
                </CardTitle>
              </CardHeader>
            </CollapsibleTrigger>
            <CollapsibleContent>
              <CardContent className="pt-0">
                <div className="p-4 bg-muted/50 rounded-lg">
                  {biddingNotice.title && (
                    <h3 className="font-semibold text-lg mb-3">{biddingNotice.title}</h3>
                  )}
                  {biddingNotice.content ? (
                    <div
                      className="prose prose-sm max-w-none"
                      dangerouslySetInnerHTML={{
                        __html: biddingNotice.content
                      }}
                    />
                  ) : (
                    <p className="text-muted-foreground">입찰공고 내용이 없습니다.</p>
                  )}
                </div>
              </CardContent>
            </CollapsibleContent>
          </Collapsible>
        </Card>
      )}

      {/* 현재 설정된 조건 섹션 */}
      {biddingConditions && (
        <Card>
          <CardHeader>
            <CardTitle>현재 설정된 입찰 조건</CardTitle>
          </CardHeader>
          <CardContent>
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4 text-sm">
              <div>
                <Label className="text-muted-foreground">지급조건</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.paymentTerms || "미설정"}</p>
                </div>
              </div>

              <div>
                <Label className="text-muted-foreground">부가세구분</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">
                    {biddingConditions.taxConditions
                      ? getTaxConditionName(biddingConditions.taxConditions)
                      : "미설정"
                    }
                  </p>
                </div>
              </div>

              <div>
                <Label className="text-muted-foreground">인도조건</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.incoterms || "미설정"}</p>
                </div>
              </div>
              <div>
                <Label className="text-muted-foreground">인도조건2</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.incotermsOption || "미설정"}</p>
                </div>
              </div>

              <div>
                <Label className="text-muted-foreground">계약 납기일</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">
                    {biddingConditions.contractDeliveryDate 
                      ? formatDate(biddingConditions.contractDeliveryDate, 'KR')
                      : "미설정"
                    }
                  </p>
                </div>
              </div>

              <div>
                <Label className="text-muted-foreground">선적지</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.shippingPort || "미설정"}</p>
                </div>
              </div>

              <div>
                <Label className="text-muted-foreground">하역지</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.destinationPort || "미설정"}</p>
                </div>
              </div>

              {/* <div>
                <Label className="text-muted-foreground">하도급법 적용여부</Label>
                <div className="mt-1 p-3 bg-muted rounded-md">
                  <p className="font-medium">{biddingConditions.isPriceAdjustmentApplicable ? "적용 가능" : "적용 불가"}</p>
                </div>
              </div> */}


                <div >
                  <Label className="text-muted-foreground">스페어파트 옵션</Label>
                  <div className="mt-1 p-3 bg-muted rounded-md">
                    <p className="font-medium">{biddingConditions.sparePartOptions}</p>
                  </div>
                </div>
            </div>
          </CardContent>
        </Card>
      )}



      {/* 참여 상태에 따른 섹션 표시 */}
      {biddingDetail.isBiddingParticipated === false ? (
        /* 응찰포기 상태 표시 */
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <XCircle className="w-5 h-5 text-destructive" />
              응찰포기
            </CardTitle>
          </CardHeader>
          <CardContent>
            <div className="text-center py-8">
              <XCircle className="w-16 h-16 text-destructive mx-auto mb-4" />
              <h3 className="text-lg font-semibold text-destructive mb-2">입찰에 참여하지 않기로 결정했습니다</h3>
              <p className="text-muted-foreground">
                해당 입찰에 대한 입찰 제출 및 관련 기능은 이용할 수 없습니다.
              </p>
            </div>
          </CardContent>
        </Card>
      ) : biddingDetail.isBiddingParticipated === null ? (
        <>
          {/* 품목 정보 확인 (Read Only) */}
          <Card>
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <Package className="w-5 h-5" />
                입찰 품목 정보
              </CardTitle>
            </CardHeader>
            <CardContent>
              {prItems.length > 0 ? (
                <PrItemsPricingTable
                  prItems={prItems}
                  initialQuotations={prItemQuotations}
                  currency={biddingDetail?.currency || 'KRW'}
                  onQuotationsChange={() => {}}
                  onTotalAmountChange={() => {}}
                  readOnly={true}
                />
              ) : (
                <div className="border rounded-lg p-4 bg-muted/20">
                  <p className="text-sm text-muted-foreground text-center py-4">
                    등록된 품목이 없습니다.
                  </p>
                </div>
              )}
            </CardContent>
          </Card>

          {/* 참여 의사 확인 섹션 */}
          <Card className="mt-6">
            <CardHeader>
              <CardTitle className="flex items-center gap-2">
                <Users className="w-5 h-5" />
                입찰 참여 의사 확인
              </CardTitle>
            </CardHeader>
            <CardContent>
              <div className="text-center py-8">
                <div className="w-16 h-16 bg-primary/10 rounded-full flex items-center justify-center mx-auto mb-4">
                  <Users className="w-8 h-8 text-primary" />
                </div>
                <h3 className="text-lg font-semibold mb-2">이 입찰에 참여하시겠습니까?</h3>
                <p className="text-muted-foreground mb-6">
                  참여를 선택하시면 입찰 작성 및 제출이 가능합니다.
                </p>
                <div className="flex justify-center gap-4">
                  <Button 
                    onClick={() => handleParticipationDecision(true)}
                    disabled={isUpdatingParticipation || isExpired}
                    className="min-w-[120px]"
                  >
                    <CheckCircle className="w-4 h-4 mr-2" />
                    {isExpired ? '마감됨' : '참여하기'}
                  </Button>
                  <Button 
                    onClick={() => handleParticipationDecision(false)}
                    disabled={isUpdatingParticipation || isExpired}
                    variant="destructive"
                    className="min-w-[120px]"
                  >
                    <XCircle className="w-4 h-4 mr-2" />
                    미참여
                  </Button>
                </div>
              </div>
            </CardContent>
          </Card>
        </>
      ) : biddingDetail.isBiddingParticipated === true ? (
        /* 응찰 폼 섹션 */
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center gap-2">
              <Send className="w-5 h-5" />
              응찰하기
            </CardTitle>
          </CardHeader>
          <CardContent className="space-y-6">
              {/* 품목별 상세 입찰 테이블 */}
              {prItems.length > 0 ? (
                <PrItemsPricingTable
                  prItems={prItems}
                  initialQuotations={prItemQuotations}
                  currency={biddingDetail?.currency || 'KRW'}
                  onQuotationsChange={handleQuotationsChange}
                  onTotalAmountChange={handleTotalAmountChange}
                  readOnly={false}
                />
              ) : (
                <div className="border rounded-lg p-4 bg-muted/20">
                  <p className="text-sm text-muted-foreground text-center py-4">
                    등록된 품목이 없습니다.
                  </p>
            </div>
              )}

          {/* 입찰 첨부파일 섹션 */}
          {biddingDetail && userId && (
            <SimpleFileUpload
              biddingId={biddingId}
              companyId={companyId}
              userId={userId}
              readOnly={false}
            />
          )}

          {/* 연동제 적용 여부 - 협력업체 별 연동제 적용요건 문의 여부에 따라 표시 */}
          {biddingDetail.isPriceAdjustmentApplicableQuestion && (
            <>
              <div className="space-y-3 p-4 border rounded-lg bg-muted/30">
                <Label className="font-semibold text-base">연동제 적용 여부 *</Label>
                <RadioGroup
                  value={responseData.priceAdjustmentResponse === null ? 'none' : responseData.priceAdjustmentResponse ? 'apply' : 'not-apply'}
                  onValueChange={(value) => {
                    const newValue = value === 'apply' ? true : value === 'not-apply' ? false : null
                    setResponseData({...responseData, priceAdjustmentResponse: newValue})
                  }}
                >
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="apply" id="price-adjustment-apply" />
                    <Label htmlFor="price-adjustment-apply" className="font-normal cursor-pointer">
                      연동제 적용
                    </Label>
                  </div>
                  <div className="flex items-center space-x-2">
                    <RadioGroupItem value="not-apply" id="price-adjustment-not-apply" />
                    <Label htmlFor="price-adjustment-not-apply" className="font-normal cursor-pointer">
                      연동제 미적용
                    </Label>
                  </div>
                </RadioGroup>
              </div>

              {/* 연동제 상세 정보 */}
              {responseData.priceAdjustmentResponse !== null && (
                <Card className="mt-6">
                  <CardHeader>
                    <CardTitle className="text-lg">하도급대금등 연동표</CardTitle>
                  </CardHeader>
                  <CardContent className="space-y-4">
                    {/* 공통 필드 - 품목등의 명칭 */}
                    <div className="space-y-2">
                      <Label htmlFor="itemName">물품등의 명칭 *</Label>
                      <Input
                        id="itemName"
                        value={priceAdjustmentForm.itemName}
                        onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, itemName: e.target.value})}
                        placeholder="품목명을 입력하세요"
                        required
                      />
                    </div>

                    {/* 연동제 적용 시 - 모든 필드 표시 */}
                    {responseData.priceAdjustmentResponse === true && (
                      <>
                        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                          <div className="space-y-2">
                            <Label htmlFor="adjustmentReflectionPoint">조정대금 반영시점 *</Label>
                            <Input
                              id="adjustmentReflectionPoint"
                              value={priceAdjustmentForm.adjustmentReflectionPoint}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentReflectionPoint: e.target.value})}
                              placeholder="반영시점을 입력하세요"
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="adjustmentRatio">반영비율 (%) *</Label>
                            <Input
                              id="adjustmentRatio"
                              type="number"
                              step="0.01"
                              value={priceAdjustmentForm.adjustmentRatio}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentRatio: e.target.value})}
                              placeholder="비율을 입력하세요"
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="adjustmentPeriod">조정주기 *</Label>
                            <Input
                              id="adjustmentPeriod"
                              value={priceAdjustmentForm.adjustmentPeriod}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentPeriod: e.target.value})}
                              placeholder="조정주기를 입력하세요"
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="referenceDate">원재료 기준 가격의 변동률 산정을 위한 기준시점 *</Label>
                            <Input
                              id="referenceDate"
                              type="date"
                              value={priceAdjustmentForm.referenceDate}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, referenceDate: e.target.value})}
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="comparisonDate">원재료 기준 가격의 변동률 산정을 위한 비교시점 *</Label>
                            <Input
                              id="comparisonDate"
                              type="date"
                              value={priceAdjustmentForm.comparisonDate}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, comparisonDate: e.target.value})}
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="contractorWriter">수탁기업(협력사)작성자 *</Label>
                            <Input
                              id="contractorWriter"
                              value={priceAdjustmentForm.contractorWriter}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, contractorWriter: e.target.value})}
                              placeholder="작성자명을 입력하세요"
                              required
                            />
                          </div>

                          <div className="space-y-2">
                            <Label htmlFor="adjustmentDate">조정일 *</Label>
                            <Input
                              id="adjustmentDate"
                              type="date"
                              value={priceAdjustmentForm.adjustmentDate}
                              onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentDate: e.target.value})}
                              required
                            />
                          </div>
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="majorApplicableRawMaterial">연동대상 주요 원재료 *</Label>
                          <Textarea
                            id="majorApplicableRawMaterial"
                            value={priceAdjustmentForm.majorApplicableRawMaterial}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, majorApplicableRawMaterial: e.target.value})}
                            placeholder="연동 대상 원재료를 입력하세요"
                            rows={3}
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="adjustmentFormula">하도급대금등 연동 산식 *</Label>
                          <Textarea
                            id="adjustmentFormula"
                            value={priceAdjustmentForm.adjustmentFormula}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentFormula: e.target.value})}
                            placeholder="연동 산식을 입력하세요"
                            rows={3}
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="rawMaterialPriceIndex">원재료 가격 기준지표 *</Label>
                          <Textarea
                            id="rawMaterialPriceIndex"
                            value={priceAdjustmentForm.rawMaterialPriceIndex}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, rawMaterialPriceIndex: e.target.value})}
                            placeholder="가격 기준지표를 입력하세요"
                            rows={2}
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="adjustmentConditions">조정요건 *</Label>
                          <Textarea
                            id="adjustmentConditions"
                            value={priceAdjustmentForm.adjustmentConditions}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, adjustmentConditions: e.target.value})}
                            placeholder="조정요건을 입력하세요"
                            rows={2}
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="priceAdjustmentNotes">기타사항</Label>
                          <Textarea
                            id="priceAdjustmentNotes"
                            value={priceAdjustmentForm.notes}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, notes: e.target.value})}
                            placeholder="기타 사항을 입력하세요"
                            rows={2}
                          />
                        </div>
                      </>
                    )}

                    {/* 연동제 미적용 시 - 제한된 필드만 표시 */}
                    {responseData.priceAdjustmentResponse === false && (
                      <>
                        <div className="space-y-2">
                          <Label htmlFor="majorNonApplicableRawMaterial">연동제 미적용 주요 원재료 *</Label>
                          <Textarea
                            id="majorNonApplicableRawMaterial"
                            value={priceAdjustmentForm.majorNonApplicableRawMaterial}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, majorNonApplicableRawMaterial: e.target.value})}
                            placeholder="연동 미적용 원재료를 입력하세요"
                            rows={2}
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="contractorWriter">수탁기업(협력사) 작성자 *</Label>
                          <Input
                            id="contractorWriter"
                            value={priceAdjustmentForm.contractorWriter}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, contractorWriter: e.target.value})}
                            placeholder="작성자명을 입력하세요"
                            required
                          />
                        </div>

                        <div className="space-y-2">
                          <Label htmlFor="nonApplicableReason">연동제 미희망 사유 *</Label>
                          <Textarea
                            id="nonApplicableReason"
                            value={priceAdjustmentForm.nonApplicableReason}
                            onChange={(e) => setPriceAdjustmentForm({...priceAdjustmentForm, nonApplicableReason: e.target.value})}
                            placeholder="미희망 사유를 입력하세요"
                            rows={3}
                            required
                          />
                        </div>
                      </>
                    )}
                  </CardContent>
                </Card>
              )}

              {/* 참고 경고문 */}
              <div className="text-xs text-red-600 space-y-2 bg-red-50 p-3 rounded-md border border-red-200 mt-4">
                <p className="font-medium">※ 참고사항</p>
                <div className="space-y-1">
                  <p>• 납품대금의 10% 이상을 차지하는 주요 원재료가 있는 경우 모든 주요 원재료에 대해서 적용 또는 미적용에 대한 연동표를 작성해야 한다.</p>
                  <p>• 납품대급연동표를 허위로 작성하거나 근거자료를 허위로 제출할 경우 본 계약이 체결되지 않을 수 있으며, 본 계약이 체결되었더라도 계약의 전부 또는 일부를 해제 또는 해지할 수 있다.</p>
                </div>
              </div>
            </>
          )}

          {/* 최종제출 체크박스 */}
          {!biddingDetail.isFinalSubmission && (
            <div className="flex items-center space-x-2 p-4 border rounded-lg bg-muted/30">
              <input
                type="checkbox"
                id="finalSubmission"
                checked={isFinalSubmission}
                onChange={(e) => setIsFinalSubmission(e.target.checked)}
                disabled={isSubmitting || isSavingDraft}
                className="h-4 w-4 rounded border-gray-300"
              />
              <label htmlFor="finalSubmission" className="text-sm font-medium cursor-pointer">
                최종 제출 (체크 시 제출 후 수정 및 취소 불가)
              </label>
            </div>
          )}

          {/* 응찰 제출 버튼 - 참여 확정 상태일 때만 표시 */}
          <div className="flex justify-between pt-4 gap-2">
            {/* 응찰 포기 버튼 (최종제출 아닌 경우만) */}
            {biddingDetail.finalQuoteSubmittedAt && !biddingDetail.isFinalSubmission && (
              <Button
                variant="destructive"
                onClick={handleCancelResponse}
                disabled={isCancelling || isSubmitting}
                className="min-w-[100px]"
              >
                <Trash2 className="w-4 h-4 mr-2" />
                {isCancelling ? '취소 중...' : '응찰 포기'}
              </Button>
            )}
            <div className="flex gap-2 ml-auto">
              <Button
                variant="outline"
                onClick={handleSaveDraft}
                disabled={isSavingDraft || isSubmitting || biddingDetail.isFinalSubmission || isExpired}
                className="min-w-[100px]"
              >
                <Save className="w-4 h-4 mr-2" />
                {isSavingDraft ? '저장 중...' : '임시 저장'}
              </Button>
              <Button
                onClick={handleSubmitResponse}
                disabled={isSubmitting || isSavingDraft || biddingDetail.isFinalSubmission || isExpired}
                className="min-w-[100px]"
              >
                <Send className="w-4 h-4 mr-2" />
                {isSubmitting ? '제출 중...' : biddingDetail.isFinalSubmission ? '최종 제출 완료' : '응찰 제출'}
              </Button>
            </div>
          </div>
        </CardContent>
      </Card>
      ) : null}
    </div>
  )
}
