'use client'

import * as React from 'react'
import { UseFormReturn } from 'react-hook-form'
import { ChevronRight, Upload, FileText, Eye, User, Building, Calendar, DollarSign, Plus } from 'lucide-react'
import { toast } from 'sonner'

import { Button } from '@/components/ui/button'
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form'
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from '@/components/ui/select'
import { Input } from '@/components/ui/input'
import { Textarea } from '@/components/ui/textarea'
import { Switch } from '@/components/ui/switch'
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'

import type { CreateBiddingSchema } from '@/lib/bidding/validation'
import { contractTypeLabels, biddingTypeLabels, awardCountLabels, biddingNoticeTypeLabels } from '@/db/schema'
import {
  getIncotermsForSelection,
  getPaymentTermsForSelection,
  getPlaceOfShippingForSelection,
  getPlaceOfDestinationForSelection,
} from '@/lib/procurement-select/service'
import { TAX_CONDITIONS } from '@/lib/tax-conditions/types'
import { getBiddingNoticeTemplate } from '@/lib/bidding/service'
import TiptapEditor from '@/components/qna/tiptap-editor'

// Dropzone components
import {
  Dropzone,
  DropzoneDescription,
  DropzoneInput,
  DropzoneTitle,
  DropzoneTrigger,
  DropzoneUploadIcon,
  DropzoneZone,
} from "@/components/ui/dropzone"
import { PurchaseGroupCodeSelector } from '@/components/common/selectors/purchase-group-code'
import { ProcurementManagerSelector } from '@/components/common/selectors/procurement-manager'
import type { PurchaseGroupCodeWithUser } from '@/components/common/selectors/purchase-group-code/purchase-group-code-service'
import type { ProcurementManagerWithUser } from '@/components/common/selectors/procurement-manager/procurement-manager-service'
import { createBidding } from '@/lib/bidding/service'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/navigation'

interface BiddingCreateDialogProps {
  form: UseFormReturn<CreateBiddingSchema>
  onSuccess?: () => void
}

export function BiddingCreateDialog({ form, onSuccess }: BiddingCreateDialogProps) {
  const { data: session } = useSession()
  const router = useRouter()
  const userId = session?.user?.id ? Number(session.user.id) : null;

  const [isOpen, setIsOpen] = React.useState(false)
  const [isSubmitting, setIsSubmitting] = React.useState(false)

  const [paymentTermsOptions, setPaymentTermsOptions] = React.useState<Array<{ code: string; description: string }>>([])
  const [incotermsOptions, setIncotermsOptions] = React.useState<Array<{ code: string; description: string }>>([])
  const [shippingPlaces, setShippingPlaces] = React.useState<Array<{ code: string; description: string }>>([])
  const [destinationPlaces, setDestinationPlaces] = React.useState<Array<{ code: string; description: string }>>([])

  const [biddingConditions, setBiddingConditions] = React.useState({
    paymentTerms: '',
    taxConditions: 'V1',
    incoterms: 'DAP',
    incotermsOption: '',
    contractDeliveryDate: '',
    shippingPort: '',
    destinationPort: '',
    isPriceAdjustmentApplicable: false,
    sparePartOptions: '',
  })

  // 구매요청자 정보 (현재 사용자)
  // React.useEffect(() => {
  //   // 실제로는 현재 로그인한 사용자의 정보를 가져와야 함
  //   // 임시로 기본값 설정
  //   form.setValue('requesterName', '김두진') // 실제로는 API에서 가져와야 함
  // }, [form])

  const [shiAttachmentFiles, setShiAttachmentFiles] = React.useState<File[]>([])
  const [vendorAttachmentFiles, setVendorAttachmentFiles] = React.useState<File[]>([])

  // 담당자 selector 상태
  const [selectedBidPic, setSelectedBidPic] = React.useState<PurchaseGroupCodeWithUser | undefined>(undefined)
  const [selectedSupplyPic, setSelectedSupplyPic] = React.useState<ProcurementManagerWithUser | undefined>(undefined)

  // 입찰공고 템플릿 관련 상태
  const [noticeTemplate, setNoticeTemplate] = React.useState<string>('')
  const [isLoadingTemplate, setIsLoadingTemplate] = React.useState(false)

  // -- 데이터 로딩 및 상태 동기화 로직
  const loadPaymentTerms = React.useCallback(async () => {
    try {
      const data = await getPaymentTermsForSelection()
      setPaymentTermsOptions(data)
      const p008Exists = data.some((item) => item.code === 'P008')
      if (p008Exists) {
        setBiddingConditions((prev) => ({ ...prev, paymentTerms: 'P008' }))
        form.setValue('biddingConditions.paymentTerms', 'P008')
      }
    } catch (error) {
      console.error('Failed to load payment terms:', error)
      toast.error('결제조건 목록을 불러오는데 실패했습니다.')
    }
  }, [form])

  const loadIncoterms = React.useCallback(async () => {
    try {
      const data = await getIncotermsForSelection()
      setIncotermsOptions(data)
      const dapExists = data.some((item) => item.code === 'DAP')
      if (dapExists) {
        setBiddingConditions((prev) => ({ ...prev, incoterms: 'DAP' }))
        form.setValue('biddingConditions.incoterms', 'DAP')
      }
    } catch (error) {
      console.error('Failed to load incoterms:', error)
      toast.error('운송조건 목록을 불러오는데 실패했습니다.')
    }
  }, [form])

  const loadShippingPlaces = React.useCallback(async () => {
    try {
      const data = await getPlaceOfShippingForSelection()
      setShippingPlaces(data)
    } catch (error) {
      console.error('Failed to load shipping places:', error)
      toast.error('선적지 목록을 불러오는데 실패했습니다.')
    }
  }, [])

  const loadDestinationPlaces = React.useCallback(async () => {
    try {
      const data = await getPlaceOfDestinationForSelection()
      setDestinationPlaces(data)
    } catch (error) {
      console.error('Failed to load destination places:', error)
      toast.error('하역지 목록을 불러오는데 실패했습니다.')
    }
  }, [])

  React.useEffect(() => {
    if (isOpen) {
      if (userId && session?.user?.name) {
        // 현재 사용자의 정보를 임시로 입찰담당자로 설정
        form.setValue('bidPicName', session.user.name)
        form.setValue('bidPicId', userId)
        // userCode는 현재 세션에 없으므로 이름으로 설정 (실제로는 API에서 가져와야 함)
        // form.setValue('bidPicCode', session.user.name)
      }
      loadPaymentTerms()
      loadIncoterms()
      loadShippingPlaces()
      loadDestinationPlaces()
      const v1Exists = TAX_CONDITIONS.some((item) => item.code === 'V1')
      if (v1Exists) {
        setBiddingConditions((prev) => ({ ...prev, taxConditions: 'V1' }))
        form.setValue('biddingConditions.taxConditions', 'V1')
      }

      // 초기 표준 템플릿 로드
      const loadInitialTemplate = async () => {
        try {
          const standardTemplate = await getBiddingNoticeTemplate('standard')
          if (standardTemplate) {
            console.log('standardTemplate', standardTemplate)
            setNoticeTemplate(standardTemplate.content)
            form.setValue('content', standardTemplate.content)
          }
        } catch (error) {
          console.error('Failed to load initial template:', error)
          toast.error('기본 템플릿을 불러오는데 실패했습니다.')
        }
      }
      loadInitialTemplate()
    }
  }, [isOpen, loadPaymentTerms, loadIncoterms, loadShippingPlaces, loadDestinationPlaces, form])

  // 입찰공고 템플릿 로딩
  const noticeTypeValue = form.watch('noticeType')
  const selectedNoticeType = React.useMemo(() => noticeTypeValue, [noticeTypeValue])

  React.useEffect(() => {
    const loadNoticeTemplate = async () => {
      setIsLoadingTemplate(true)
      try {
        // 처음 로드할 때는 무조건 standard 템플릿 사용
        const templateType = selectedNoticeType || 'standard'
        const template = await getBiddingNoticeTemplate(templateType)
        if (template) {
          setNoticeTemplate(template.content)
          // 폼의 content 필드도 업데이트
          form.setValue('content', template.content)
        } else {
          // 템플릿이 없으면 표준 템플릿 사용
          const defaultTemplate = await getBiddingNoticeTemplate('standard')
          if (defaultTemplate) {
            setNoticeTemplate(defaultTemplate.content)
            form.setValue('content', defaultTemplate.content)
          }
        }
      } catch (error) {
        console.error('Failed to load notice template:', error)
        toast.error('입찰공고 템플릿을 불러오는데 실패했습니다.')
      } finally {
        setIsLoadingTemplate(false)
      }
    }

    loadNoticeTemplate()
  }, [selectedNoticeType, form])

  // SHI용 파일 첨부 핸들러
  const handleShiFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    const files = Array.from(event.target.files || [])
    setShiAttachmentFiles(prev => [...prev, ...files])
  }

  const removeShiFile = (index: number) => {
    setShiAttachmentFiles(prev => prev.filter((_, i) => i !== index))
  }

  // 협력업체용 파일 첨부 핸들러
  const handleVendorFileUpload = (event: React.ChangeEvent<HTMLInputElement>) => {
    const files = Array.from(event.target.files || [])
    setVendorAttachmentFiles(prev => [...prev, ...files])
  }

  const removeVendorFile = (index: number) => {
    setVendorAttachmentFiles(prev => prev.filter((_, i) => i !== index))
  }

  // 입찰담당자 선택 핸들러
  const handleBidPicSelect = (code: PurchaseGroupCodeWithUser) => {
    setSelectedBidPic(code)
    form.setValue('bidPicName', code.DISPLAY_NAME || '')
    form.setValue('bidPicCode', code.PURCHASE_GROUP_CODE || '')
    // ID도 저장 (실제로는 사용자 ID가 필요)
    if (code.user) {
      form.setValue('bidPicId', code.user.id || undefined)
    }
  }

  // 조달담당자 선택 핸들러
  const handleSupplyPicSelect = (manager: ProcurementManagerWithUser) => {
    setSelectedSupplyPic(manager)
    form.setValue('supplyPicName', manager.DISPLAY_NAME || '')
    form.setValue('supplyPicCode', manager.PROCUREMENT_MANAGER_CODE || '')
    // ID도 저장 (실제로는 사용자 ID가 필요)
    if (manager.user) {
      form.setValue('supplyPicId', manager.user.id || undefined)
    }
  }

  const handleSubmit = async (data: CreateBiddingSchema) => {
    setIsSubmitting(true)
    try {
      // 폼 validation 실행
      const isFormValid = await form.trigger()

      if (!isFormValid) {
        toast.error('필수 정보를 모두 입력해주세요.')
        return
      }

      // 첨부파일 정보 설정

      // sparePartOptions가 undefined인 경우 빈 문자열로 설정
      const biddingData = {
        ...data,
        attachments: shiAttachmentFiles, // 실제 파일 객체들 전달
        vendorAttachments: vendorAttachmentFiles, // 실제 파일 객체들 전달
        biddingConditions: {
          ...data.biddingConditions,
          sparePartOptions: data.biddingConditions.sparePartOptions || '',
          incotermsOption: data.biddingConditions.incotermsOption || '',
          contractDeliveryDate: data.biddingConditions.contractDeliveryDate || '',
          shippingPort: data.biddingConditions.shippingPort || '',
          destinationPort: data.biddingConditions.destinationPort || '',
        },
      }

      const result = await createBidding(biddingData, userId?.toString() || '') // 실제로는 현재 사용자 ID

      if (result.success) {
        toast.success("입찰이 성공적으로 생성되었습니다.")

        // 생성된 입찰의 상세 페이지로 이동
        if ('data' in result && result.data?.id) {
          router.push(`/evcp/bid/${result.data.id}/info`)
        }

        setIsOpen(false)
        form.reset()
        setShiAttachmentFiles([])
        setVendorAttachmentFiles([])
        setSelectedBidPic(undefined)
        setSelectedSupplyPic(undefined)
        setNoticeTemplate('')
        if (onSuccess) {
          onSuccess()
        }
      } else {
        toast.error((result as { success: false; error: string }).error || "입찰 생성에 실패했습니다.")
      }
    } catch (error) {
      console.error("Failed to create bidding:", error)
      toast.error("입찰 생성 중 오류가 발생했습니다.")
    } finally {
      setIsSubmitting(false)
    }
  }

  const handleOpenChange = (open: boolean) => {
    setIsOpen(open)
    if (!open) {
      // 다이얼로그 닫을 때 폼 초기화
      form.reset()
      setShiAttachmentFiles([])
      setVendorAttachmentFiles([])
      setSelectedBidPic(undefined)
      setSelectedSupplyPic(undefined)
      setNoticeTemplate('')
      setBiddingConditions({
        paymentTerms: '',
        taxConditions: 'V1',
        incoterms: 'DAP',
        incotermsOption: '',
        contractDeliveryDate: '',
        shippingPort: '',
        destinationPort: '',
        isPriceAdjustmentApplicable: false,
        sparePartOptions: '',
      })
    }
  }

  return (
    <Dialog open={isOpen} onOpenChange={handleOpenChange}>
      <DialogTrigger asChild>
        <Button>
          <Plus className="mr-2 h-4 w-4" />
          입찰 신규생성
        </Button>
      </DialogTrigger>
      <DialogContent className="max-w-6xl max-h-[90vh] overflow-y-auto">
        <DialogHeader>
          <DialogTitle>입찰 신규생성</DialogTitle>
          <DialogDescription>
            새로운 입찰을 생성합니다. 기본 정보와 입찰 조건을 설정하세요.
          </DialogDescription>
        </DialogHeader>

        <div className="space-y-6">
          <Form {...form}>
            <form onSubmit={form.handleSubmit(handleSubmit)} className="space-y-6">
              {/* 통합된 기본 정보 및 입찰 조건 카드 */}
              <Card>
                <CardHeader>
                  <CardTitle className="flex items-center gap-2">
                    <FileText className="h-5 w-5" />
                    기본 정보 및 입찰 조건
                  </CardTitle>
                </CardHeader>
                <CardContent className="space-y-4">
                  {/* 1행: 입찰명, 낙찰업체 수, 입찰유형, 계약구분 */}
                  <div className="grid grid-cols-4 gap-4">
                    <FormField
                      control={form.control}
                      name="title"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>입찰명 <span className="text-red-500">*</span></FormLabel>
                          <FormControl>
                            <Input placeholder="입찰명을 입력하세요" {...field} />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="awardCount"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>낙찰업체 수<span className="text-red-500">*</span></FormLabel>
                          <Select onValueChange={field.onChange} defaultValue={field.value}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="낙찰업체 수 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {Object.entries(awardCountLabels).map(([value, label]) => (
                                <SelectItem key={value} value={value}>
                                  {label}
                                </SelectItem>
                              ))}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingType"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>입찰유형 <span className="text-red-500">*</span></FormLabel>
                          <Select onValueChange={field.onChange} defaultValue={field.value}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="입찰유형 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {Object.entries(biddingTypeLabels).map(([value, label]) => (
                                <SelectItem key={value} value={value}>
                                  {label}
                                </SelectItem>
                              ))}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="contractType"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>계약구분 <span className="text-red-500">*</span></FormLabel>
                          <Select onValueChange={field.onChange} defaultValue={field.value}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="계약구분 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {Object.entries(contractTypeLabels).map(([value, label]) => (
                                <SelectItem key={value} value={value}>
                                  {label}
                                </SelectItem>
                              ))}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 기타 입찰유형 선택 시 직접입력 필드 */}
                  {form.watch('biddingType') === 'other' && (
                    <div className="grid grid-cols-4 gap-4">
                      <div></div>
                      <div></div>
                      <FormField
                        control={form.control}
                        name="biddingTypeCustom"
                        render={({ field }) => (
                          <FormItem>
                            <FormLabel>기타 입찰유형 <span className="text-red-500">*</span></FormLabel>
                            <FormControl>
                              <Input placeholder="직접 입력하세요" {...field} />
                            </FormControl>
                            <FormMessage />
                          </FormItem>
                        )}
                      />
                      <div></div>
                    </div>
                  )}

                  {/* 2행: 예산, 실적가, 내정가, P/R번호 (조회용) */}
                  <div className="grid grid-cols-4 gap-4">
                    <FormField
                      control={form.control}
                      name="budget"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <DollarSign className="h-3 w-3" />
                            예산
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="예산 입력" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="finalBidPrice"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <DollarSign className="h-3 w-3" />
                            실적가
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="실적가" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="targetPrice"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Eye className="h-3 w-3" />
                            내정가
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="내정가" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="prNumber"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Eye className="h-3 w-3" />
                            P/R번호
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="P/R번호" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 3행: 입찰담당자, 조달담당자 */}
                  <div className="grid grid-cols-2 gap-4">
                    <FormField
                      control={form.control}
                      name="bidPicName"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <User className="h-3 w-3" />
                            입찰담당자 <span className="text-red-500">*</span>
                          </FormLabel>
                          <FormControl>
                            <PurchaseGroupCodeSelector
                              selectedCode={selectedBidPic}
                              onCodeSelect={(code) => {
                                handleBidPicSelect(code)
                                field.onChange(code.DISPLAY_NAME || '')
                              }}
                              placeholder="입찰담당자 선택"
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="supplyPicName"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <User className="h-3 w-3" />
                            조달담당자
                          </FormLabel>
                          <FormControl>
                            <ProcurementManagerSelector
                              selectedManager={selectedSupplyPic}
                              onManagerSelect={(manager) => {
                                handleSupplyPicSelect(manager)
                                field.onChange(manager.DISPLAY_NAME || '')
                              }}
                              placeholder="조달담당자 선택"
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 4행: 하도급법적용여부, SHI 지급조건 */}
                  <div className="grid grid-cols-2 gap-4">
                    <FormField
                      control={form.control}
                      name="biddingConditions.isPriceAdjustmentApplicable"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>하도급법적용여부</FormLabel>
                          <div className="flex items-center space-x-2">
                            <Switch
                              id="price-adjustment"
                              checked={biddingConditions.isPriceAdjustmentApplicable}
                              onCheckedChange={(checked) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  isPriceAdjustmentApplicable: checked
                                }))
                                field.onChange(checked)
                              }}
                            />
                            <FormLabel htmlFor="price-adjustment" className="text-sm">
                              하도급법 적용여부
                            </FormLabel>
                          </div>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingConditions.paymentTerms"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 지급조건 <span className="text-red-500">*</span></FormLabel>
                          <FormControl>
                            <Select
                              value={biddingConditions.paymentTerms}
                              onValueChange={(value) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  paymentTerms: value
                                }))
                                field.onChange(value)
                              }}
                            >
                              <SelectTrigger>
                                <SelectValue placeholder="지급조건 선택" />
                              </SelectTrigger>
                              <SelectContent>
                                {paymentTermsOptions.length > 0 ? (
                                  paymentTermsOptions.map((option) => (
                                    <SelectItem key={option.code} value={option.code}>
                                      {option.code} {option.description && `(${option.description})`}
                                    </SelectItem>
                                  ))
                                ) : (
                                  <SelectItem value="loading" disabled>
                                    데이터를 불러오는 중...
                                  </SelectItem>
                                )}
                              </SelectContent>
                            </Select>
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 5행: SHI 인도조건, SHI 인도조건2 */}
                  <div className="grid grid-cols-2 gap-4">
                    <FormField
                      control={form.control}
                      name="biddingConditions.incoterms"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 인도조건 <span className="text-red-500">*</span></FormLabel>
                          <Select
                            value={biddingConditions.incoterms}
                            onValueChange={(value) => {
                              setBiddingConditions(prev => ({
                                ...prev,
                                incoterms: value
                              }))
                              field.onChange(value)
                            }}
                          >
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="인코텀즈 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {incotermsOptions.length > 0 ? (
                                incotermsOptions.map((option) => (
                                  <SelectItem key={option.code} value={option.code}>
                                    {option.code} {option.description && `(${option.description})`}
                                  </SelectItem>
                                ))
                              ) : (
                                <SelectItem value="loading" disabled>
                                  데이터를 불러오는 중...
                                </SelectItem>
                              )}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingConditions.incotermsOption"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 인도조건2</FormLabel>
                          <FormControl>
                            <Input
                              placeholder="인도조건 추가 정보"
                              value={biddingConditions.incotermsOption}
                              onChange={(e) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  incotermsOption: e.target.value
                                }))
                                field.onChange(e.target.value)
                              }}
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 6행: SHI 매입부가가치세, SHI 선적지 */}
                  <div className="grid grid-cols-2 gap-4">
                    <FormField
                      control={form.control}
                      name="biddingConditions.taxConditions"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 매입부가가치세 <span className="text-red-500">*</span></FormLabel>
                          <FormControl>
                            <Select
                              value={biddingConditions.taxConditions}
                              onValueChange={(value) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  taxConditions: value
                                }))
                                field.onChange(value)
                              }}
                            >
                              <SelectTrigger>
                                <SelectValue placeholder="세금조건 선택" />
                              </SelectTrigger>
                              <SelectContent>
                                {TAX_CONDITIONS.map((condition) => (
                                  <SelectItem key={condition.code} value={condition.code}>
                                    {condition.name}
                                  </SelectItem>
                                ))}
                              </SelectContent>
                            </Select>
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingConditions.shippingPort"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 선적지</FormLabel>
                          <Select
                            value={biddingConditions.shippingPort}
                            onValueChange={(value) => {
                              setBiddingConditions(prev => ({
                                ...prev,
                                shippingPort: value
                              }))
                              field.onChange(value)
                            }}
                          >
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="선적지 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {shippingPlaces.length > 0 ? (
                                shippingPlaces.map((place) => (
                                  <SelectItem key={place.code} value={place.code}>
                                    {place.code} {place.description && `(${place.description})`}
                                  </SelectItem>
                                ))
                              ) : (
                                <SelectItem value="loading" disabled>
                                  데이터를 불러오는 중...
                                </SelectItem>
                              )}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 7행: SHI 하역지, 계약 납품일 */}
                  <div className="grid grid-cols-2 gap-4">
                    <FormField
                      control={form.control}
                      name="biddingConditions.destinationPort"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>SHI 하역지</FormLabel>
                          <Select
                            value={biddingConditions.destinationPort}
                            onValueChange={(value) => {
                              setBiddingConditions(prev => ({
                                ...prev,
                                destinationPort: value
                              }))
                              field.onChange(value)
                            }}
                          >
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="하역지 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {destinationPlaces.length > 0 ? (
                                destinationPlaces.map((place) => (
                                  <SelectItem key={place.code} value={place.code}>
                                    {place.code} {place.description && `(${place.description})`}
                                  </SelectItem>
                                ))
                              ) : (
                                <SelectItem value="loading" disabled>
                                  데이터를 불러오는 중...
                                </SelectItem>
                              )}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingConditions.contractDeliveryDate"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>계약 납품일</FormLabel>
                          <FormControl>
                            <Input
                              type="date"
                              value={biddingConditions.contractDeliveryDate}
                              onChange={(e) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  contractDeliveryDate: e.target.value
                                }))
                                field.onChange(e.target.value)
                              }}
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 8행: 계약기간 시작/종료, 진행상태, 구매조직 */}
                  <div className="grid grid-cols-4 gap-4">
                    <FormField
                      control={form.control}
                      name="contractStartDate"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Calendar className="h-3 w-3" />
                            계약기간 시작
                          </FormLabel>
                          <FormControl>
                            <Input type="date" {...field} />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="contractEndDate"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Calendar className="h-3 w-3" />
                            계약기간 종료
                          </FormLabel>
                          <FormControl>
                            <Input type="date" {...field} />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="status"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Eye className="h-3 w-3" />
                            진행상태
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="진행상태" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="purchasingOrganization"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <Building className="h-3 w-3" />
                            구매조직 <span className="text-red-500">*</span>
                          </FormLabel>
                          <Select onValueChange={field.onChange} value={field.value || ''}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="구매조직 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              <SelectItem value="Shipbuild & Offshore">Shipbuild & Offshore</SelectItem>
                              <SelectItem value="Wind Energy">Wind Energy</SelectItem>
                              <SelectItem value="Power & Control Sys.">Power & Control Sys.</SelectItem>
                              <SelectItem value="SHI NINGBO Co., LTD">SHI NINGBO Co., LTD</SelectItem>
                              <SelectItem value="RONGCHENG Co.LTD">RONGCHENG Co.LTD</SelectItem>
                              <SelectItem value="RONGCHENGGAYA Co.LTD">RONGCHENGGAYA Co.LTD</SelectItem>
                              <SelectItem value="S&Sys">S&Sys</SelectItem>
                              <SelectItem value="Energy & Infra Solut">Energy & Infra Solut</SelectItem>
                              <SelectItem value="test pur.org222">test pur.org222</SelectItem>
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 9행: 구매요청자, 구매유형, 통화, 스페어파트 옵션 */}
                  <div className="grid grid-cols-4 gap-4">
                    <FormField
                      control={form.control}
                      name="requesterName"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel className="flex items-center gap-1">
                            <User className="h-3 w-3" />
                            구매요청자
                          </FormLabel>
                          <FormControl>
                            <Input placeholder="구매요청자" {...field} readOnly className="bg-muted" />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="noticeType"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>구매유형 <span className="text-red-500">*</span></FormLabel>
                          <Select onValueChange={field.onChange} defaultValue={field.value}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="구매유형 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              {Object.entries(biddingNoticeTypeLabels).map(([value, label]) => (
                                <SelectItem key={value} value={value}>
                                  {label}
                                </SelectItem>
                              ))}
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="currency"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>통화 <span className="text-red-500">*</span></FormLabel>
                          <Select onValueChange={field.onChange} defaultValue={field.value}>
                            <FormControl>
                              <SelectTrigger>
                                <SelectValue placeholder="통화 선택" />
                              </SelectTrigger>
                            </FormControl>
                            <SelectContent>
                              <SelectItem value="KRW">KRW (원)</SelectItem>
                              <SelectItem value="USD">USD (달러)</SelectItem>
                              <SelectItem value="EUR">EUR (유로)</SelectItem>
                              <SelectItem value="JPY">JPY (엔)</SelectItem>
                            </SelectContent>
                          </Select>
                          <FormMessage />
                        </FormItem>
                      )}
                    />

                    <FormField
                      control={form.control}
                      name="biddingConditions.sparePartOptions"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>스페어파트 옵션</FormLabel>
                          <FormControl>
                            <Textarea
                              placeholder="스페어파트 관련 옵션을 입력하세요"
                              value={biddingConditions.sparePartOptions}
                              onChange={(e) => {
                                setBiddingConditions(prev => ({
                                  ...prev,
                                  sparePartOptions: e.target.value
                                }))
                                field.onChange(e.target.value)
                              }}
                              rows={3}
                              className="min-h-[40px]"
                            />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>

                  {/* 입찰개요 추가 */}
                  <div className="mt-6 pt-4 border-t">
                    <FormField
                      control={form.control}
                      name="description"
                      render={({ field }) => (
                        <FormItem>
                          <FormLabel>입찰개요</FormLabel>
                          <FormControl>
                            <Textarea placeholder="입찰에 대한 설명을 입력하세요" rows={3} {...field} />
                          </FormControl>
                          <FormMessage />
                        </FormItem>
                      )}
                    />
                  </div>
                </CardContent>
              </Card>

              {/* 입찰공고 내용 */}
              <Card>
                <CardHeader>
                  <CardTitle>입찰공고 내용</CardTitle>
                  <p className="text-sm text-muted-foreground">
                    선택한 입찰공고 타입의 템플릿이 자동으로 로드됩니다. 필요에 따라 수정하세요.
                  </p>
                </CardHeader>
                <CardContent className="space-y-4">
                  <FormField
                    control={form.control}
                    name="content"
                    render={({ field }) => (
                      <FormItem>
                        <FormControl>
                          <div className="border rounded-lg">
                            <TiptapEditor
                              content={field.value || noticeTemplate}
                              setContent={(content) => {
                                field.onChange(content)
                              }}
                              disabled={isLoadingTemplate}
                              height="300px"
                            />
                          </div>
                        </FormControl>
                        <FormMessage />
                      </FormItem>
                    )}
                  />

                  {/* {isLoadingTemplate && (
                    <div className="flex items-center justify-center p-4 text-sm text-muted-foreground">
                      <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-gray-900 mr-2"></div>
                      입찰공고 템플릿을 불러오는 중...
                    </div>
                  )} */}
                </CardContent>
              </Card>

              {/* SHI용 첨부파일 */}
              <Card>
                <CardHeader>
                  <CardTitle className="flex items-center gap-2">
                    <Upload className="h-5 w-5" />
                    SHI용 첨부파일
                  </CardTitle>
                  <p className="text-sm text-muted-foreground">
                    내부 보관를 위해 필요한 문서나 파일을 업로드 하세요.
                  </p>
                </CardHeader>
                <CardContent className="space-y-4">
                  <Dropzone
                    maxSize={6e8} // 600MB
                    onDropAccepted={(files) => {
                      const newFiles = Array.from(files)
                      setShiAttachmentFiles(prev => [...prev, ...newFiles])
                    }}
                    onDropRejected={() => {
                      toast({
                        title: "File upload rejected",
                        description: "Please check file size and type.",
                        variant: "destructive",
                      })
                    }}
                  >
                    {() => (
                      <DropzoneTrigger asChild>
                        <DropzoneZone className="flex justify-center h-32">
                          <div className="flex items-center gap-6">
                            <DropzoneUploadIcon />
                            <div className="grid gap-0.5">
                              <DropzoneTitle>파일을 드래그하여 업로드</DropzoneTitle>
                            </div>
                          </div>
                        </DropzoneZone>
                      </DropzoneTrigger>
                    )}
                  </Dropzone>

                  {shiAttachmentFiles.length > 0 && (
                    <div className="space-y-2">
                      <h4 className="text-sm font-medium">업로드된 파일</h4>
                      <div className="space-y-2">
                        {shiAttachmentFiles.map((file, index) => (
                          <div
                            key={index}
                            className="flex items-center justify-between p-3 bg-muted rounded-lg"
                          >
                            <div className="flex items-center gap-3">
                              <FileText className="h-4 w-4 text-muted-foreground" />
                              <div>
                                <p className="text-sm font-medium">{file.name}</p>
                                <p className="text-xs text-muted-foreground">
                                  {(file.size / 1024 / 1024).toFixed(2)} MB
                                </p>
                              </div>
                            </div>
                            <Button
                              type="button"
                              variant="ghost"
                              size="sm"
                              onClick={() => removeShiFile(index)}
                            >
                              제거
                            </Button>
                          </div>
                        ))}
                      </div>
                    </div>
                  )}
                </CardContent>
              </Card>

              {/* 협력업체용 첨부파일 */}
              <Card>
                <CardHeader>
                  <CardTitle className="flex items-center gap-2">
                    <Upload className="h-5 w-5" />
                    협력업체용 첨부파일
                  </CardTitle>
                  <p className="text-sm text-muted-foreground">
                    협력사로 제공하는 문서나 파일을 업로드 하세요.
                  </p>
                </CardHeader>
                <CardContent className="space-y-4">
                  <Dropzone
                    maxSize={6e8} // 600MB
                    onDropAccepted={(files) => {
                      const newFiles = Array.from(files)
                      setVendorAttachmentFiles(prev => [...prev, ...newFiles])
                    }}
                    onDropRejected={() => {
                      toast({
                        title: "File upload rejected",
                        description: "Please check file size and type.",
                        variant: "destructive",
                      })
                    }}
                  >
                    {() => (
                      <DropzoneTrigger asChild>
                        <DropzoneZone className="flex justify-center h-32">
                          <div className="flex items-center gap-6">
                            <DropzoneUploadIcon />
                            <div className="grid gap-0.5">
                              <DropzoneTitle>파일을 드래그하여 업로드</DropzoneTitle>
                            </div>
                          </div>
                        </DropzoneZone>
                      </DropzoneTrigger>
                    )}
                  </Dropzone>

                  {vendorAttachmentFiles.length > 0 && (
                    <div className="space-y-2">
                      <h4 className="text-sm font-medium">업로드된 파일</h4>
                      <div className="space-y-2">
                        {vendorAttachmentFiles.map((file, index) => (
                          <div
                            key={index}
                            className="flex items-center justify-between p-3 bg-muted rounded-lg"
                          >
                            <div className="flex items-center gap-3">
                              <FileText className="h-4 w-4 text-muted-foreground" />
                              <div>
                                <p className="text-sm font-medium">{file.name}</p>
                                <p className="text-xs text-muted-foreground">
                                  {(file.size / 1024 / 1024).toFixed(2)} MB
                                </p>
                              </div>
                            </div>
                            <Button
                              type="button"
                              variant="ghost"
                              size="sm"
                              onClick={() => removeVendorFile(index)}
                            >
                              제거
                            </Button>
                          </div>
                        ))}
                      </div>
                    </div>
                  )}
                </CardContent>
              </Card>

              {/* 네비게이션 버튼 */}
              <div className="flex justify-between">
                <div></div>
                <div className="flex gap-2">
                  <Button
                    type="submit"
                    disabled={isSubmitting}
                    className="flex items-center gap-2"
                  >
                    {isSubmitting ? '저장 중...' : '저장'}
                    <ChevronRight className="h-4 w-4" />
                  </Button>
                </div>
              </div>
            </form>
          </Form>
        </div>
      </DialogContent>
    </Dialog>
  )
}