"use client" import { useState, useEffect, use } from "react" 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, Loader2 } from "lucide-react" import Link from "next/link" import { AddPrintBatchDialog } from "@/components/food/add-print-batch-dialog" import { ExportCardsDialog } from "@/components/food/export-cards-dialog" import { useToast } from "@/components/ui/use-toast" import { isSuperUser } from "@/lib/api/auth" import { getFood } from "@/lib/api/food" import type { Food } from "@/components/food/food-detail-dialog" // 扩展Food类型以包含批次相关信息 type FoodWithBatches = Food & { printedCount?: number batches?: Array<{ id: string date: string quantity: number startId: string endId: string activatedCount: number }> } export default function FoodDetailPage({ params }: { params: Promise<{ id: string }> }) { const { id } = use(params) const { toast } = useToast() const [food, setFood] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) // 获取食物详情 const fetchFoodDetail = async () => { try { setLoading(true) setError(null) const response = await getFood(id) if (response.success && response.data) { // 为演示目的,添加一些模拟的批次数据 const foodWithBatches: FoodWithBatches = { ...response.data, printedCount: response.data.activatedCount + Math.floor(Math.random() * 1000), batches: [ { id: "B001", date: "2023-08-01", quantity: 2000, startId: `${response.data.id}-0001`, endId: `${response.data.id}-2000`, activatedCount: Math.floor(response.data.activatedCount * 0.6), }, { id: "B002", date: "2023-11-15", quantity: 1000, startId: `${response.data.id}-2001`, endId: `${response.data.id}-3000`, activatedCount: Math.floor(response.data.activatedCount * 0.4), }, ] } setFood(foodWithBatches) } else { setError("食物不存在或获取失败") } } catch (error) { console.error("获取食物详情失败:", error) setError(error instanceof Error ? error.message : "获取食物详情失败") toast({ title: "获取数据失败", description: error instanceof Error ? error.message : "无法获取食物详情", variant: "destructive", }) } finally { setLoading(false) } } useEffect(() => { fetchFoodDetail() }, [id]) if (loading) { return (
加载中...
) } if (error || !food) { return (

食物不存在

{error || `找不到ID为 ${id} 的食物`}

) } const isPublished = food.status === "published" || food.status === "已发布" // 显示名称映射 const foodTypeDisplayMap: Record = { fruit: "水果", vegetable: "蔬菜", meat: "肉类", seafood: "海鲜", dairy: "乳制品", grain: "谷物", snack: "零食", drink: "饮品", dessert: "甜点", spice: "调味品", other: "其他", } const rarityDisplayMap: Record = { common: "普通", uncommon: "非常见", rare: "稀有", epic: "史诗", legendary: "传说", mythic: "神话", } const statusDisplayMap: Record = { draft: "草稿", published: "已发布", archived: "已归档", } return (
{(!isPublished || isSuperUser()) && ( )}
食物详情 批次管理 数据分析
食物预览
{food.image && !food.image.includes("placeholder") ? ( {food.name} ) : (
暂无图片
)}
{statusDisplayMap[food.status] || food.status}
食物详情

类型

{foodTypeDisplayMap[food.food_type] || food.food_type || "-"}

稀有度

{rarityDisplayMap[food.rarity] || food.rarity || "-"}

发布日期

{food.releaseDate || "尚未发布"}

状态

{statusDisplayMap[food.status] || food.status}

激活数量

{food.activatedCount || 0}

印刷总数

{food.printedCount || 0}

描述

{food.description || "暂无描述"}

{isPublished && (

{isSuperUser() ? "该食物已发布,您以超级管理员身份仍可编辑和删除。请谨慎操作。" : "该食物已发布,基本属性不可修改。您仍可以增加印刷数量。"}

)}
印刷批次 管理食物卡牌的印刷批次和卡牌ID
{food.batches?.map((batch) => ( ))} {(!food.batches || food.batches.length === 0) && ( )}
批次ID 创建日期 数量 起始ID 结束ID 激活数量 操作
{batch.id} {batch.date} {batch.quantity} {batch.startId} {batch.endId} {batch.activatedCount}
暂无批次数据
数据分析 查看食物卡牌的激活数据和使用情况

激活数据图表

地区分布图表

时间趋势图表

) }