import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { DashboardShell } from "@/components/dashboard-shell"
import { DashboardHeader } from "@/components/dashboard-header"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { ArrowLeft, Edit, AlertTriangle, FileText } from "lucide-react"
import Link from "next/link"
import { AddPrintBatchDialog } from "@/components/outfits/add-print-batch-dialog"
import { ExportCardsDialog } from "@/components/outfits/export-cards-dialog"

// Mock data for the outfit details
const outfitData = {
  OFT001: {
    id: "OFT001",
    name: "经典原创服装",
    type: "常规",
    rarity: "稀有",
    description: "洛天依的经典原创服装，简约而不失时尚，适合各种场合穿着。",
    releaseDate: "2023-10-15",
    status: "已发布",
    activatedCount: 1245,
    printedCount: 2000,
    image: "/placeholder.svg?height=300&width=300",
    batches: [
      { id: "B001", date: "2023-09-01", quantity: 1000, startId: "OFT001-0001", endId: "OFT001-1000" },
      { id: "B002", date: "2023-12-15", quantity: 1000, startId: "OFT001-1001", endId: "OFT001-2000" },
    ],
  },
  OFT002: {
    id: "OFT002",
    name: "夏日泳装",
    type: "季节限定",
    rarity: "史诗",
    description: "专为夏季设计的清凉泳装，让洛天依在夏日活动中更加亮眼。",
    releaseDate: "2023-06-01",
    status: "已发布",
    activatedCount: 876,
    printedCount: 1500,
    image: "/placeholder.svg?height=300&width=300",
    batches: [{ id: "B003", date: "2023-05-10", quantity: 1500, startId: "OFT002-0001", endId: "OFT002-1500" }],
  },
  OFT003: {
    id: "OFT003",
    name: "冬日圣诞服",
    type: "节日限定",
    rarity: "史诗",
    description: "圣诞节特别设计的节日服装，温暖而喜庆，让洛天依陪你度过欢乐的圣诞。",
    releaseDate: "2023-12-01",
    status: "已发布",
    activatedCount: 1032,
    printedCount: 2000,
    image: "/placeholder.svg?height=300&width=300",
    batches: [{ id: "B004", date: "2023-11-15", quantity: 2000, startId: "OFT003-0001", endId: "OFT003-2000" }],
  },
  OFT004: {
    id: "OFT004",
    name: "校园制服",
    type: "常规",
    rarity: "稀有",
    description: "清新可爱的校园风格制服，展现洛天依青春活力的一面。",
    releaseDate: "2024-01-15",
    status: "已发布",
    activatedCount: 1567,
    printedCount: 3000,
    image: "/placeholder.svg?height=300&width=300",
    batches: [
      { id: "B005", date: "2024-01-05", quantity: 2000, startId: "OFT004-0001", endId: "OFT004-2000" },
      { id: "B006", date: "2024-02-10", quantity: 1000, startId: "OFT004-2001", endId: "OFT004-3000" },
    ],
  },
  OFT005: {
    id: "OFT005",
    name: "演唱会礼服",
    type: "特别版",
    rarity: "传说",
    description: "为重要演唱会设计的华丽礼服，尽显洛天依的优雅与魅力。",
    releaseDate: "",
    status: "未发布",
    activatedCount: 0,
    printedCount: 1000,
    image: "/placeholder.svg?height=300&width=300",
    batches: [{ id: "B007", date: "2024-03-20", quantity: 1000, startId: "OFT005-0001", endId: "OFT005-1000" }],
  },
}

export default function OutfitDetailPage({ params }: { params: { id: string } }) {
  const outfit = outfitData[params.id as keyof typeof outfitData]

  if (!outfit) {
    return (
      <DashboardShell>
        <div className="flex flex-col items-center justify-center h-[60vh]">
          <AlertTriangle className="h-16 w-16 text-red-500 mb-4" />
          <h1 className="text-2xl font-bold mb-2">服装不存在</h1>
          <p className="text-gray-500 mb-6">找不到ID为 {params.id} 的服装</p>
          <Button asChild>
            <Link href="/outfits">
              <ArrowLeft className="mr-2 h-4 w-4" />
              返回服装列表
            </Link>
          </Button>
        </div>
      </DashboardShell>
    )
  }

  const isPublished = outfit.status === "已发布"

  return (
    <DashboardShell>
      <div className="absolute top-0 right-0 w-1/2 h-48 bg-gradient-to-bl from-pink-200 via-purple-200 to-transparent opacity-20 rounded-bl-full" />

      <div className="flex items-center mb-6">
        <Button variant="ghost" size="sm" className="mr-4" asChild>
          <Link href="/outfits">
            <ArrowLeft className="mr-2 h-4 w-4" />
            返回列表
          </Link>
        </Button>
        <DashboardHeader heading={outfit.name} text={`服装ID: ${outfit.id}`}>
          <div className="flex space-x-2 ml-auto">
            {!isPublished && (
              <Button
                asChild
                className="bg-gradient-to-r from-pink-500 to-purple-600 hover:from-pink-600 hover:to-purple-700 transition-all duration-300 shadow-md hover:shadow-lg"
              >
                <Link href={`/outfits/edit/${params.id}`}>
                  <Edit className="mr-2 h-4 w-4" />
                  编辑服装
                </Link>
              </Button>
            )}
            <ExportCardsDialog outfitId={outfit.id} />
          </div>
        </DashboardHeader>
      </div>

      <Tabs defaultValue="details" className="space-y-4">
        <TabsList>
          <TabsTrigger value="details">服装详情</TabsTrigger>
          <TabsTrigger value="batches">批次管理</TabsTrigger>
          <TabsTrigger value="analytics">数据分析</TabsTrigger>
        </TabsList>

        <TabsContent value="details" className="space-y-4">
          <div className="grid gap-6 md:grid-cols-3">
            <Card className="md:col-span-1 border-none shadow-lg bg-white">
              <CardHeader>
                <CardTitle className="text-lg font-bold">服装预览</CardTitle>
              </CardHeader>
              <CardContent className="flex justify-center">
                <div className="relative w-full aspect-square max-w-[300px] rounded-lg overflow-hidden bg-gray-100 flex items-center justify-center">
                  <img src={outfit.image || "/placeholder.svg"} alt={outfit.name} className="object-cover" />
                  <div className="absolute bottom-0 left-0 right-0 bg-gradient-to-t from-black/70 to-transparent p-3">
                    <Badge className={`${isPublished ? "bg-green-500" : "bg-gray-500"}`}>{outfit.status}</Badge>
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card className="md:col-span-2 border-none shadow-lg bg-gradient-to-br from-white to-purple-50">
              <CardHeader>
                <CardTitle className="text-lg font-bold">服装详情</CardTitle>
              </CardHeader>
              <CardContent>
                <div className="grid grid-cols-2 gap-4">
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">服装类型</p>
                    <p className="font-medium">{outfit.type}</p>
                  </div>
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">稀有度</p>
                    <p className="font-medium">{outfit.rarity}</p>
                  </div>
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">发布日期</p>
                    <p className="font-medium">{outfit.releaseDate || "尚未发布"}</p>
                  </div>
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">激活数量</p>
                    <p className="font-medium">{outfit.activatedCount}</p>
                  </div>
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">印刷总数</p>
                    <p className="font-medium">{outfit.printedCount}</p>
                  </div>
                  <div className="space-y-1">
                    <p className="text-sm font-medium text-gray-500">剩余库存</p>
                    <p className="font-medium">{outfit.printedCount - outfit.activatedCount}</p>
                  </div>
                  <div className="col-span-2 space-y-1">
                    <p className="text-sm font-medium text-gray-500">服装描述</p>
                    <p className="text-gray-700">{outfit.description}</p>
                  </div>
                </div>

                {isPublished && (
                  <div className="mt-6 p-3 bg-amber-50 border border-amber-200 rounded-lg flex items-start">
                    <AlertTriangle className="h-5 w-5 text-amber-500 mr-2 flex-shrink-0 mt-0.5" />
                    <p className="text-sm text-amber-700">该服装已发布，基本属性不可修改。您仍可以增加印刷数量。</p>
                  </div>
                )}
              </CardContent>
            </Card>
          </div>
        </TabsContent>

        <TabsContent value="batches" className="space-y-4">
          <Card className="border-none shadow-lg bg-gradient-to-br from-white to-blue-50">
            <CardHeader className="flex flex-row items-center justify-between">
              <div>
                <CardTitle className="text-lg font-bold">印刷批次</CardTitle>
                <CardDescription>管理服装卡牌的印刷批次和卡牌ID</CardDescription>
              </div>
              <AddPrintBatchDialog outfitId={outfit.id} isPublished={isPublished} />
            </CardHeader>
            <CardContent>
              <div className="rounded-md border">
                <table className="w-full">
                  <thead>
                    <tr className="bg-gray-50 border-b">
                      <th className="py-3 px-4 text-left text-sm font-medium text-gray-500">批次ID</th>
                      <th className="py-3 px-4 text-left text-sm font-medium text-gray-500">创建日期</th>
                      <th className="py-3 px-4 text-left text-sm font-medium text-gray-500">数量</th>
                      <th className="py-3 px-4 text-left text-sm font-medium text-gray-500">起始ID</th>
                      <th className="py-3 px-4 text-left text-sm font-medium text-gray-500">结束ID</th>
                      <th className="py-3 px-4 text-right text-sm font-medium text-gray-500">操作</th>
                    </tr>
                  </thead>
                  <tbody>
                    {outfit.batches.map((batch) => (
                      <tr key={batch.id} className="border-b hover:bg-gray-50">
                        <td className="py-3 px-4 text-sm font-medium">{batch.id}</td>
                        <td className="py-3 px-4 text-sm">{batch.date}</td>
                        <td className="py-3 px-4 text-sm">{batch.quantity}</td>
                        <td className="py-3 px-4 text-sm font-mono text-xs">{batch.startId}</td>
                        <td className="py-3 px-4 text-sm font-mono text-xs">{batch.endId}</td>
                        <td className="py-3 px-4 text-right">
                          <Button variant="ghost" size="sm" className="h-8 hover:bg-blue-50 hover:text-blue-600">
                            <FileText className="h-4 w-4 mr-1" />
                            导出ID
                          </Button>
                        </td>
                      </tr>
                    ))}
                  </tbody>
                </table>
              </div>
            </CardContent>
          </Card>
        </TabsContent>

        <TabsContent value="analytics" className="space-y-4">
          <Card className="border-none shadow-lg bg-gradient-to-br from-white to-green-50">
            <CardHeader>
              <CardTitle className="text-lg font-bold">数据分析</CardTitle>
              <CardDescription>查看服装卡牌的激活数据和使用情况</CardDescription>
            </CardHeader>
            <CardContent>
              <div className="grid gap-6 md:grid-cols-2">
                <div className="h-[300px] bg-gray-50 rounded-lg flex items-center justify-center">
                  <p className="text-gray-500">激活数据图表</p>
                </div>
                <div className="h-[300px] bg-gray-50 rounded-lg flex items-center justify-center">
                  <p className="text-gray-500">地区分布图表</p>
                </div>
                <div className="md:col-span-2 h-[300px] bg-gray-50 rounded-lg flex items-center justify-center">
                  <p className="text-gray-500">时间趋势图表</p>
                </div>
              </div>
            </CardContent>
          </Card>
        </TabsContent>
      </Tabs>
    </DashboardShell>
  )
}
