"use client";

import * as React from "react";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { Separator } from "@/components/ui/separator";
import {
  Building2,
  Calendar,
  DollarSign,
  FileText,
  Package,
  Globe,
  MapPin,
  Clock,
  CheckCircle,
  XCircle,
  AlertCircle,
  Download,
  Eye,
  User,
  Mail,
  Phone,
  CreditCard,
  Truck,
  Shield,
  Paperclip,
  Info,
  Edit,
  X,
} from "lucide-react";
import { format } from "date-fns";
import { ko } from "date-fns/locale";
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components/ui/table";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { cn } from "@/lib/utils";
import { formatFileSize, quickDownload, smartFileAction } from "@/lib/file-download";

// Props 타입 정의
interface VendorResponseDetailDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  data: any; // mergedData의 row
  rfqId: number;
}

// 상태별 설정
const getStatusConfig = (status: string) => {
  switch (status) {
    case "초대됨":
      return {
        icon: <Mail className="h-4 w-4" />,
        color: "text-blue-600",
        bgColor: "bg-blue-50",
        variant: "secondary" as const,
      };
    case "작성중":
      return {
        icon: <Clock className="h-4 w-4" />,
        color: "text-yellow-600",
        bgColor: "bg-yellow-50",
        variant: "outline" as const,
      };
    case "제출완료":
      return {
        icon: <CheckCircle className="h-4 w-4" />,
        color: "text-green-600",
        bgColor: "bg-green-50",
        variant: "default" as const,
      };
    case "수정요청":
      return {
        icon: <AlertCircle className="h-4 w-4" />,
        color: "text-orange-600",
        bgColor: "bg-orange-50",
        variant: "secondary" as const,
      };
    case "최종확정":
      return {
        icon: <Shield className="h-4 w-4" />,
        color: "text-indigo-600",
        bgColor: "bg-indigo-50",
        variant: "default" as const,
      };
    case "취소":
      return {
        icon: <XCircle className="h-4 w-4" />,
        color: "text-red-600",
        bgColor: "bg-red-50",
        variant: "destructive" as const,
      };
    default:
      return {
        icon: <Info className="h-4 w-4" />,
        color: "text-gray-600",
        bgColor: "bg-gray-50",
        variant: "outline" as const,
      };
  }
};

export function VendorResponseDetailDialog({
  open,
  onOpenChange,
  data,
  rfqId,
}: VendorResponseDetailDialogProps) {
  if (!data) return null;

  const response = data.response;
  const statusConfig = getStatusConfig(response?.status || "초대됨");
  const hasSubmitted = !!response?.submission?.submittedAt;

  // 이메일 발송 정보 파싱
  let emailRecipients = { to: [], cc: [], sentBy: "" };
  try {
    if (data.emailSentTo) {
      emailRecipients = JSON.parse(data.emailSentTo);
    }
  } catch (e) {
    console.error("Failed to parse emailSentTo");
  }

  // 견적 아이템 (실제로는 response.quotationItems에서 가져옴)
  const quotationItems = response?.quotationItems || [];
  
  // 첨부파일 (실제로는 response.attachments에서 가져옴)
  const attachments = response?.attachments || [];

  return (
    <Dialog open={open} onOpenChange={onOpenChange}>
    <DialogContent className="max-w-5xl max-h-[90vh] p-0 flex flex-col">
      <DialogHeader className="flex-shrink-0 sticky top-0 z-20 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60 border-b px-6 py-4">
          <div className="flex items-center justify-between">
            <div>
              <DialogTitle className="text-xl font-bold">
                벤더 응답 상세
              </DialogTitle>
              <DialogDescription className="mt-1">
                {data.vendorName} ({data.vendorCode || "코드없음"}) - {data.rfqCode}
              </DialogDescription>
            </div>
            <div className="flex items-center gap-2">
              <Button
                variant="ghost"
                size="sm"
                onClick={() => onOpenChange(false)}
                className="h-8 w-8 p-0"
              >
                <X className="h-4 w-4" />
                <span className="sr-only">창 닫기</span>
              </Button>
              {/* {onEdit && (
                <Button variant="outline" size="sm" onClick={onEdit}>
                  <Edit className="h-4 w-4 mr-2" />
                  수정
                </Button>
              )} */}
            </div>
          </div>
        </DialogHeader>

      <div className="flex-1 overflow-y-auto px-6 ">

        <Tabs defaultValue="overview" className="mb-2">
          <TabsList className="grid w-full grid-cols-4">
            <TabsTrigger value="overview">개요</TabsTrigger>
            <TabsTrigger value="quotation">견적정보</TabsTrigger>
            <TabsTrigger value="items">품목상세</TabsTrigger>
            <TabsTrigger value="documents">
              제출 문서
            </TabsTrigger>
          </TabsList>

          {/* 개요 탭 */}
          <TabsContent value="overview" className="space-y-4">
            {/* 상태 정보 */}
            <Card>
              <CardHeader>
                <CardTitle className="text-base">응답 상태</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-2 gap-4">
                  <div className="space-y-3">
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">현재 상태</span>
                      <Badge variant={statusConfig.variant}>
                        {statusConfig.icon}
                        <span className="ml-1">{response?.status || "초대됨"}</span>
                      </Badge>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">응답 버전</span>
                      <span className="font-medium">v{response?.responseVersion || 1}</span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">Short List</span>
                      <Badge variant={data.shortList ? "default" : "outline"}>
                        {data.shortList ? "선정" : "대기"}
                      </Badge>
                    </div>
                  </div>
                  <div className="space-y-3">
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">제출일시</span>
                      <span className="text-sm">
                        {response?.submission?.submittedAt
                          ? format(new Date(response.submission.submittedAt), "yyyy-MM-dd HH:mm", { locale: ko })
                          : "-"}
                      </span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">제출자</span>
                      <span className="text-sm">{response?.submission?.submittedByName || "-"}</span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">최종 수정일</span>
                      <span className="text-sm">
                        {data.updatedAt
                          ? format(new Date(new Date(data.updatedAt).getTime() + 9 * 60 * 60 * 1000), "yyyy-MM-dd HH:mm", { locale: ko })
                          : "-"}
                      </span>
                    </div>
                  </div>
                </div>
              </CardContent>
            </Card>

            {/* 벤더 정보 */}
            <Card>
              <CardHeader>
                <CardTitle className="text-base">벤더 정보</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-2 gap-4">
                  <div className="space-y-3">
                    <div className="flex items-center gap-2">
                      <Building2 className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">업체명</span>
                      <span className="font-medium ml-auto">{data.vendorName}</span>
                    </div>
                    <div className="flex items-center gap-2">
                      <Package className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">업체코드</span>
                      <span className="font-medium ml-auto">{data.vendorCode || "-"}</span>
                    </div>
                    <div className="flex items-center gap-2">
                      <Globe className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">국가</span>
                      <Badge variant={data.vendorCountry === "KR" || data.vendorCountry === "한국" ? "default" : "secondary"} className="ml-auto">
                        {data.vendorCountry === "KR" || data.vendorCountry === "한국" ? `내자(${data.vendorCountry})` : `외자(${data.vendorCountry})`}
                      </Badge>
                    </div>
                  </div>
                  <div className="space-y-3">
                    <div className="flex items-center gap-2">
                      <Mail className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">이메일</span>
                      <span className="text-sm ml-auto">{response?.vendor?.email || "-"}</span>
                    </div>
                    <div className="flex items-center gap-2">
                      <Shield className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">업체분류</span>
                      <span className="font-medium ml-auto">{data.vendorCategory || "-"}</span>
                    </div>
                    <div className="flex items-center gap-2">
                      <Info className="h-4 w-4 text-muted-foreground" />
                      <span className="text-sm text-muted-foreground">AVL 등급</span>
                      <span className="font-medium ml-auto">{data.vendorGrade || "-"}</span>
                    </div>
                  </div>
                </div>
              </CardContent>
            </Card>

            {/* 이메일 발송 정보 */}
            {data.emailSentAt && (
              <Card>
                <CardHeader>
                  <CardTitle className="text-base">이메일 발송 정보</CardTitle>
                </CardHeader>
                <CardContent>
                  <div className="space-y-3">
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">최초 발송일시</span>
                      <span className="text-sm">
                        {format(new Date(new Date(data.emailSentAt).getTime() + 9 * 60 * 60 * 1000), "yyyy-MM-dd HH:mm", { locale: ko })}
                      </span>
                    </div>
                    {data.lastEmailSentAt && data.emailResentCount > 1 && (
                      <div className="flex items-center justify-between">
                        <span className="text-sm text-muted-foreground">최근 재발송일시</span>
                        <span className="text-sm">
                          {format(new Date(new Date(data.lastEmailSentAt).getTime() + 9 * 60 * 60 * 1000), "yyyy-MM-dd HH:mm", { locale: ko })}
                          <Badge variant="secondary" className="ml-2">
                            재발송 {data.emailResentCount - 1}회
                          </Badge>
                        </span>
                      </div>
                    )}
                    {emailRecipients.to.length > 0 && (
                      <div className="flex items-start justify-between">
                        <span className="text-sm text-muted-foreground">수신자</span>
                        <span className="text-sm text-right">{emailRecipients.to.join(", ")}</span>
                      </div>
                    )}
                    {emailRecipients.cc.length > 0 && (
                      <div className="flex items-start justify-between">
                        <span className="text-sm text-muted-foreground">참조</span>
                        <span className="text-sm text-right">{emailRecipients.cc.join(", ")}</span>
                      </div>
                    )}
                    {emailRecipients.sentBy && (
                      <div className="flex items-center justify-between">
                        <span className="text-sm text-muted-foreground">발신자</span>
                        <span className="text-sm">{emailRecipients.sentBy}</span>
                      </div>
                    )}
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">발송 상태</span>
                      <Badge variant={data.emailStatus === "failed" ? "destructive" : "default"}>
                        {data.emailStatus === "failed" ? "발송 실패" : "발송 완료"}
                      </Badge>
                    </div>
                  </div>
                </CardContent>
              </Card>
            )}
          </TabsContent>

          {/* 견적정보 탭 */}
          <TabsContent value="quotation" className="space-y-4">
            {/* 요청 조건 */}
            <Card>
              <CardHeader>
                <CardTitle className="text-base">요청 조건</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-2 gap-4">
                  <div className="space-y-3">
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">통화</span>
                      <Badge variant="outline">{data.currency}</Badge>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">지급조건</span>
                      <TooltipProvider>
                        <Tooltip>
                          <TooltipTrigger>
                            <span className="font-medium">{data.paymentTermsCode}</span>
                          </TooltipTrigger>
                          {data.paymentTermsDescription && (
                            <TooltipContent>
                              <p>{data.paymentTermsDescription}</p>
                            </TooltipContent>
                          )}
                        </Tooltip>
                      </TooltipProvider>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">인코텀즈</span>
                      <TooltipProvider>
                        <Tooltip>
                          <TooltipTrigger>
                            <span className="font-medium">{data.incotermsCode}</span>
                          </TooltipTrigger>
                          {data.incotermsDescription && (
                            <TooltipContent>
                              <p>{data.incotermsDescription}</p>
                              {data.incotermsDetail && <p className="text-xs">{data.incotermsDetail}</p>}
                            </TooltipContent>
                          )}
                        </Tooltip>
                      </TooltipProvider>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">Tax</span>
                      <span className="font-medium">{data.taxCode || "-"}</span>
                    </div>
                  </div>
                  <div className="space-y-3">
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">PR납기 요청일</span>
                      <span className="text-sm">
                        {data.deliveryDate
                          ? format(new Date(data.deliveryDate), "yyyy-MM-dd")
                          : "-"}
                      </span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">계약기간</span>
                      <span className="text-sm">{data.contractDuration || "-"}</span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">선적지</span>
                      <span className="text-sm">{data.placeOfShipping || "-"}</span>
                    </div>
                    <div className="flex items-center justify-between">
                      <span className="text-sm text-muted-foreground">도착지</span>
                      <span className="text-sm">{data.placeOfDestination || "-"}</span>
                    </div>
                  </div>
                </div>

                {/* 추가 조건 */}
                <Separator className="my-4" />
                <div className="space-y-3">
                  {data.firstYn && (
                    <div className="flex items-start justify-between">
                      <div className="flex items-center gap-2">
                        <Badge variant="outline">초도품</Badge>
                        <span className="text-sm text-muted-foreground">요구사항</span>
                      </div>
                      <span className="text-sm text-right max-w-xs">{data.firstDescription || "초도품 제출 필요"}</span>
                    </div>
                  )}
                  {data.sparepartYn && (
                    <div className="flex items-start justify-between">
                      <div className="flex items-center gap-2">
                        <Badge variant="outline">스페어파트</Badge>
                        <span className="text-sm text-muted-foreground">요구사항</span>
                      </div>
                      <span className="text-sm text-right max-w-xs">{data.sparepartDescription || "스페어파트 제공 필요"}</span>
                    </div>
                  )}
                  {data.materialPriceRelatedYn && (
                    <div className="flex items-center justify-between">
                      <div className="flex items-center gap-2">
                        <Badge variant="outline">연동제</Badge>
                        <span className="text-sm text-muted-foreground">적용</span>
                      </div>
                      <span className="text-sm">적용</span>
                    </div>
                  )}
                </div>
              </CardContent>
            </Card>

            {/* 벤더 제안 조건 (제출된 경우) */}
            {hasSubmitted && (
              <Card>
                <CardHeader>
                  <CardTitle className="text-base">벤더 제안 조건</CardTitle>
                </CardHeader>
                <CardContent>
                  <div className="grid grid-cols-2 gap-4">
                    <div className="space-y-3">
                      <div className="space-y-1">
                        <div className="flex items-center justify-between">
                          <span className="text-sm text-muted-foreground">제안 통화</span>
                          <div className="flex items-center gap-2">
                            <Badge variant={response?.pricing?.vendorCurrency === data.currency ? "outline" : "default"}>
                              {response?.pricing?.vendorCurrency || data.currency}
                            </Badge>
                            {response?.pricing?.vendorCurrency && response.pricing.vendorCurrency !== data.currency && (
                              <Badge variant="outline" className="text-xs">변경</Badge>
                            )}
                          </div>
                        </div>
                        {response?.pricing?.vendorCurrency && 
                         response.pricing.vendorCurrency !== data.currency &&
                         response?.changeReasons?.currency && (
                          <p className="text-xs text-muted-foreground text-right pl-4">
                            사유: {response.changeReasons.currency}
                          </p>
                        )}
                      </div>
                      <div className="space-y-1">
                        <div className="flex items-center justify-between">
                          <span className="text-sm text-muted-foreground">제안 지급조건</span>
                          <div className="flex items-center gap-2">
                            <span className="font-medium">
                              {response?.vendorTerms?.paymentTermsCode || data.paymentTermsCode}
                            </span>
                            {response?.vendorTerms?.paymentTermsCode &&
                             response.vendorTerms.paymentTermsCode !== data.paymentTermsCode && (
                              <Badge variant="outline" className="text-xs">변경</Badge>
                            )}
                          </div>
                        </div>
                        {response?.vendorTerms?.paymentTermsCode &&
                         response.vendorTerms.paymentTermsCode !== data.paymentTermsCode &&
                         response?.changeReasons?.paymentTerms && (
                          <p className="text-xs text-muted-foreground text-right pl-4">
                            사유: {response.changeReasons.paymentTerms}
                          </p>
                        )}
                      </div>
                      <div className="space-y-1">
                        <div className="flex items-center justify-between">
                          <span className="text-sm text-muted-foreground">제안 인코텀즈</span>
                          <div className="flex items-center gap-2">
                            <span className="font-medium">
                              {response?.vendorTerms?.incotermsCode || data.incotermsCode}
                            </span>
                            {response?.vendorTerms?.incotermsCode &&
                             response.vendorTerms.incotermsCode !== data.incotermsCode && (
                              <Badge variant="outline" className="text-xs">변경</Badge>
                            )}
                          </div>
                        </div>
                        {response?.vendorTerms?.incotermsCode &&
                         response.vendorTerms.incotermsCode !== data.incotermsCode &&
                         response?.changeReasons?.incoterms && (
                          <p className="text-xs text-muted-foreground text-right pl-4">
                            사유: {response.changeReasons.incoterms}
                          </p>
                        )}
                      </div>
                      {response?.vendorTerms?.taxCode && (
                        <div className="space-y-1">
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">제안 세금조건</span>
                            <div className="flex items-center gap-2">
                              <span className="font-medium">{response.vendorTerms.taxCode}</span>
                              {response.vendorTerms.taxCode !== data.taxCode && (
                                <Badge variant="outline" className="text-xs">변경</Badge>
                              )}
                            </div>
                          </div>
                          {response.vendorTerms.taxCode !== data.taxCode &&
                           response?.changeReasons?.tax && (
                            <p className="text-xs text-muted-foreground text-right pl-4">
                              사유: {response.changeReasons.tax}
                            </p>
                          )}
                        </div>
                      )}
                    </div>
                    <div className="space-y-3">
                      <div className="space-y-1">
                        <div className="flex items-center justify-between">
                          <span className="text-sm text-muted-foreground">제안 납기일</span>
                          <div className="flex items-center gap-2">
                            <span className="text-sm">
                              {response?.vendorTerms?.deliveryDate
                                ? format(new Date(response.vendorTerms.deliveryDate), "yyyy-MM-dd")
                                : data.deliveryDate
                                ? format(new Date(data.deliveryDate), "yyyy-MM-dd")
                                : "-"}
                            </span>
                            {response?.vendorTerms?.deliveryDate && data.deliveryDate &&
                             new Date(response.vendorTerms.deliveryDate).getTime() !== new Date(data.deliveryDate).getTime() && (
                              <Badge variant="outline" className="text-xs">변경</Badge>
                            )}
                          </div>
                        </div>
                        {response?.vendorTerms?.deliveryDate && data.deliveryDate &&
                         new Date(response.vendorTerms.deliveryDate).getTime() !== new Date(data.deliveryDate).getTime() &&
                         response?.changeReasons?.deliveryDate && (
                          <p className="text-xs text-muted-foreground text-right pl-4">
                            사유: {response.changeReasons.deliveryDate}
                          </p>
                        )}
                      </div>
                      {response?.vendorTerms?.placeOfShipping && (
                        <div className="space-y-1">
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">제안 선적지</span>
                            <div className="flex items-center gap-2">
                              <span className="text-sm">{response.vendorTerms.placeOfShipping}</span>
                              {response.vendorTerms.placeOfShipping !== data.placeOfShipping && (
                                <Badge variant="outline" className="text-xs">변경</Badge>
                              )}
                            </div>
                          </div>
                          {response.vendorTerms.placeOfShipping !== data.placeOfShipping &&
                           response?.changeReasons?.shipping && (
                            <p className="text-xs text-muted-foreground text-right pl-4">
                              사유: {response.changeReasons.shipping}
                            </p>
                          )}
                        </div>
                      )}
                      {response?.vendorTerms?.placeOfDestination && (
                        <div className="space-y-1">
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">제안 하역지</span>
                            <div className="flex items-center gap-2">
                              <span className="text-sm">{response.vendorTerms.placeOfDestination}</span>
                              {response.vendorTerms.placeOfDestination !== data.placeOfDestination && (
                                <Badge variant="outline" className="text-xs">변경</Badge>
                              )}
                            </div>
                          </div>
                          {response.vendorTerms.placeOfDestination !== data.placeOfDestination &&
                           response?.changeReasons?.shipping && (
                            <p className="text-xs text-muted-foreground text-right pl-4">
                              사유: {response.changeReasons.shipping}
                            </p>
                          )}
                        </div>
                      )}
                      {response?.vendorTerms?.contractDuration && (
                        <div className="space-y-1">
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">제안 계약기간</span>
                            <div className="flex items-center gap-2">
                              <span className="text-sm">{response.vendorTerms.contractDuration}</span>
                              {response.vendorTerms.contractDuration !== data.contractDuration && (
                                <Badge variant="outline" className="text-xs">변경</Badge>
                              )}
                            </div>
                          </div>
                          {response.vendorTerms.contractDuration !== data.contractDuration && (
                            <p className="text-xs text-muted-foreground text-right pl-4">
                              {response?.changeReasons?.contractDuration 
                                ? `사유: ${response.changeReasons.contractDuration}`
                                : "변경 사유 없음"}
                            </p>
                          )}
                        </div>
                      )}
                      <div className="flex items-center justify-between">
                        <span className="text-sm text-muted-foreground">총 견적금액</span>
                        <span className="font-bold text-lg">
                          {response?.pricing?.totalAmount
                            ? new Intl.NumberFormat("ko-KR", {
                                style: "currency",
                                currency: response.pricing.vendorCurrency || data.currency,
                              }).format(response.pricing.totalAmount)
                            : "-"}
                        </span>
                      </div>
                    </div>
                  </div>

                  {/* 벤더 추가 응답 */}
                  {(response?.additionalRequirements?.firstArticle?.acceptance ||
                    response?.additionalRequirements?.sparePart?.acceptance) && (
                    <>
                      <Separator className="my-4" />
                      <div className="space-y-3">
                        {response?.additionalRequirements?.firstArticle?.acceptance && (
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">초도품 수용여부</span>
                            <Badge
                              variant={
                                response.additionalRequirements.firstArticle.acceptance === "수용"
                                  ? "default"
                                  : response.additionalRequirements.firstArticle.acceptance === "부분수용"
                                  ? "secondary"
                                  : "destructive"
                              }
                            >
                              {response.additionalRequirements.firstArticle.acceptance}
                            </Badge>
                          </div>
                        )}
                        {response?.additionalRequirements?.sparePart?.acceptance && (
                          <div className="flex items-center justify-between">
                            <span className="text-sm text-muted-foreground">스페어파트 수용여부</span>
                            <Badge
                              variant={
                                response.additionalRequirements.sparePart.acceptance === "수용"
                                  ? "default"
                                  : response.additionalRequirements.sparePart.acceptance === "부분수용"
                                  ? "secondary"
                                  : "destructive"
                              }
                            >
                              {response.additionalRequirements.sparePart.acceptance}
                            </Badge>
                          </div>
                        )}
                      </div>
                    </>
                  )}

                  {/* 벤더 비고 */}
                  {(response?.remarks?.general || response?.remarks?.technical) && (
                    <>
                      <Separator className="my-4" />
                      <div className="space-y-3">
                        {response?.remarks?.general && (
                          <div>
                            <span className="text-sm text-muted-foreground">일반 비고</span>
                            <p className="mt-1 text-sm">{response.remarks.general}</p>
                          </div>
                        )}
                        {response?.remarks?.technical && (
                          <div>
                            <span className="text-sm text-muted-foreground">기술 제안</span>
                            <p className="mt-1 text-sm">{response.remarks.technical}</p>
                          </div>
                        )}
                      </div>
                    </>
                  )}
                </CardContent>
              </Card>
            )}
          </TabsContent>

          {/* 품목상세 탭 */}
          <TabsContent value="items" className="space-y-4">
            {quotationItems.length > 0 ? (
              <Card>
                <CardHeader>
                  <CardTitle className="text-base">견적 품목 상세</CardTitle>
                  <CardDescription>
                    총 {quotationItems.length}개 품목
                  </CardDescription>
                </CardHeader>
                <CardContent>
                  <Table>
                    <TableHeader>
                      <TableRow>
                        <TableHead>PR No.</TableHead>
                        <TableHead>자재코드</TableHead>
                        <TableHead>자재명</TableHead>
                        <TableHead className="text-right">수량</TableHead>
                        <TableHead>단위</TableHead>
                        <TableHead className="text-right">단가</TableHead>
                        <TableHead className="text-right">금액</TableHead>
                        <TableHead>통화</TableHead>
                        <TableHead>PR납기 요청일</TableHead>
                      </TableRow>
                    </TableHeader>
                    <TableBody>
                      {quotationItems.map((item: any) => (
                        <TableRow key={item.id}>
                          <TableCell className="font-mono text-xs">{item.prNo}</TableCell>
                          <TableCell className="font-mono text-xs">{item.materialCode}</TableCell>
                          <TableCell className="text-xs">{item.materialDescription}</TableCell>
                          <TableCell className="text-right">{item.quantity}</TableCell>
                          <TableCell>{item.uom}</TableCell>
                          <TableCell className="text-right">
                            {new Intl.NumberFormat("ko-KR").format(item.unitPrice)}
                          </TableCell>
                          <TableCell className="text-right font-medium">
                            {new Intl.NumberFormat("ko-KR").format(item.totalPrice)}
                          </TableCell>
                          <TableCell>
                            {response?.pricing?.vendorCurrency || data.currency || item.currency}
                          </TableCell>
                          <TableCell>
                            {item.vendorDeliveryDate
                              ? format(new Date(item.vendorDeliveryDate), "MM-dd")
                              : "-"}
                          </TableCell>
                        </TableRow>
                      ))}
                    </TableBody>
                  </Table>
                </CardContent>
              </Card>
            ) : (
              <Card>
                <CardContent className="pt-6">
                  <div className="text-center text-muted-foreground">
                    아직 제출된 견적 품목이 없습니다.
                  </div>
                </CardContent>
              </Card>
            )}
          </TabsContent>

          {/* 제출 문서 탭 */}
          <TabsContent value="documents" className="space-y-4">
            <Card>
              <CardHeader>
                <CardTitle className="text-base">제출 문서</CardTitle>
                <CardDescription>
                  총 {attachments.length}개 파일
                </CardDescription>
              </CardHeader>
              <CardContent>
                {attachments.length > 0 ? (
                  <div className="space-y-2 max-h-[52vh] overflow-y-auto pr-1">
                    {attachments.map((file: any) => {
                      const fileLabel = file.originalFileName || file.fileName || "파일명 없음";
                      const canOpen = !!file.filePath;
                      return (
                        <div
                          key={`${file.id}-${file.filePath}-${file.originalFileName}`}
                          className="flex items-start justify-between gap-4 rounded-lg border p-3 hover:bg-accent/50"
                        >
                          <div className="space-y-1">
                            <div className="flex items-center gap-2 flex-wrap">
                              <Paperclip className="h-4 w-4 text-muted-foreground" />
                              <Badge variant="secondary">{file.attachmentType || "문서"}</Badge>
                              <span className="font-medium break-all">{fileLabel}</span>
                            </div>
                            <p className="text-xs text-muted-foreground space-x-2 flex flex-wrap">
                              {file.documentNo && <span>문서번호: {file.documentNo}</span>}
                              {file.description && <span>{file.description}</span>}
                              <span>{file.fileSize ? formatFileSize(file.fileSize) : "크기 정보 없음"}</span>
                              {file.uploadedAt && (
                                <span>
                                  업로드: {format(new Date(file.uploadedAt), "yyyy-MM-dd HH:mm", { locale: ko })}
                                </span>
                              )}
                              {file.uploadedByName && <span>작성: {file.uploadedByName}</span>}
                            </p>
                          </div>
                          <div className="flex items-center gap-2">
                            <Button
                              variant="ghost"
                              size="sm"
                              disabled={!canOpen}
                              onClick={() => smartFileAction(file.filePath, fileLabel)}
                              className="h-8"
                            >
                              <Eye className="h-4 w-4 mr-1" />
                              열기
                            </Button>
                            <Button
                              variant="outline"
                              size="sm"
                              disabled={!canOpen}
                              onClick={() => quickDownload(file.filePath, fileLabel)}
                              className="h-8"
                            >
                              <Download className="h-4 w-4 mr-1" />
                              다운로드
                            </Button>
                          </div>
                        </div>
                      );
                    })}
                  </div>
                ) : (
                  <div className="text-sm text-muted-foreground">
                    아직 제출된 첨부파일이 없습니다.
                  </div>
                )}
              </CardContent>
            </Card>
          </TabsContent>
        </Tabs>
              </div>

      </DialogContent>
    </Dialog>
  );
}