"use client"

import { AdminNav } from "@/components/admin-nav"
import { ThemeToggle } from "@/components/theme-toggle"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import { useToast } from "@/components/ui/use-toast"
import { useAuth } from "@/context/auth-context"
import { BookOpen, Package, ShoppingBag, Users } from "lucide-react"
import Link from "next/link"
import { useRouter } from "next/navigation"
import { useEffect, useState } from "react"

interface Stats {
  totalBooks: number
  totalOrders: number
  totalUsers: number
  totalPublishers: number
}

export default function AdminDashboard() {
  const { user } = useAuth()
  const router = useRouter()
  const { toast } = useToast()
  const [stats, setStats] = useState<Stats>({
    totalBooks: 0,
    totalOrders: 0,
    totalUsers: 0,
    totalPublishers: 0,
  })

  useEffect(() => {
    // 检查用户是否登录且是管理员
    if (!user) {
      toast({
        title: "请先登录",
        description: "您需要登录后才能访问管理页面",
        variant: "destructive",
      })
      router.push("/login")
      return
    }

    if (!user.isAdmin) {
      toast({
        title: "权限不足",
        description: "您没有管理员权限",
        variant: "destructive",
      })
      router.push("/")
      return
    }

    // 模拟获取统计数据
    // 实际项目中应该从API获取
    const fetchStats = async () => {
      // 这里是模拟数据
      try {
        // 模拟API调用
        setTimeout(() => {
          setStats({
            totalBooks: 156,
            totalOrders: 42,
            totalUsers: 87,
            totalPublishers: 23,
          })
        }, 500)
      } catch (error) {
        console.error("获取统计数据失败", error)
      }
    }

    fetchStats()
  }, [user, router, toast])

  if (!user || !user.isAdmin) {
    return null
  }

  return (
    <div className="flex min-h-screen flex-col">
      <header className="sticky top-0 z-50 w-full border-b bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60">
        <div className="container flex h-16 items-center justify-between">
          <div className="flex items-center gap-2 font-semibold">
            <BookOpen className="h-6 w-6" />
            <span>图书管理系统 - 管理后台</span>
          </div>
          <div className="flex items-center gap-4">
            <ThemeToggle />
            <Button variant="ghost" onClick={() => router.push("/")}>
              返回前台
            </Button>
          </div>
        </div>
      </header>
      <div className="flex flex-1">
        <AdminNav />
        <main className="flex-1 p-6">
          <div className="space-y-6">
            <h1 className="text-3xl font-bold">管理员控制台</h1>
            <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-4">
              <Card>
                <CardHeader className="pb-2">
                  <CardTitle className="text-sm font-medium">总图书数</CardTitle>
                  <CardDescription>系统中的图书总数</CardDescription>
                </CardHeader>
                <CardContent>
                  <div className="flex items-center justify-between">
                    <div className="text-2xl font-bold">{stats.totalBooks}</div>
                    <BookOpen className="h-8 w-8 text-muted-foreground" />
                  </div>
                  <Link href="/admin/books">
                    <Button variant="ghost" className="mt-4 w-full">
                      管理图书
                    </Button>
                  </Link>
                </CardContent>
              </Card>
              <Card>
                <CardHeader className="pb-2">
                  <CardTitle className="text-sm font-medium">总订单数</CardTitle>
                  <CardDescription>系统中的订单总数</CardDescription>
                </CardHeader>
                <CardContent>
                  <div className="flex items-center justify-between">
                    <div className="text-2xl font-bold">{stats.totalOrders}</div>
                    <Package className="h-8 w-8 text-muted-foreground" />
                  </div>
                  <Link href="/admin/orders">
                    <Button variant="ghost" className="mt-4 w-full">
                      管理订单
                    </Button>
                  </Link>
                </CardContent>
              </Card>
              <Card>
                <CardHeader className="pb-2">
                  <CardTitle className="text-sm font-medium">总用户数</CardTitle>
                  <CardDescription>系统中的用户总数</CardDescription>
                </CardHeader>
                <CardContent>
                  <div className="flex items-center justify-between">
                    <div className="text-2xl font-bold">{stats.totalUsers}</div>
                    <Users className="h-8 w-8 text-muted-foreground" />
                  </div>
                  <Link href="/admin/users">
                    <Button variant="ghost" className="mt-4 w-full">
                      管理用户
                    </Button>
                  </Link>
                </CardContent>
              </Card>
              <Card>
                <CardHeader className="pb-2">
                  <CardTitle className="text-sm font-medium">总出版社数</CardTitle>
                  <CardDescription>系统中的出版社总数</CardDescription>
                </CardHeader>
                <CardContent>
                  <div className="flex items-center justify-between">
                    <div className="text-2xl font-bold">{stats.totalPublishers}</div>
                    <ShoppingBag className="h-8 w-8 text-muted-foreground" />
                  </div>
                  <Link href="/admin/publishers">
                    <Button variant="ghost" className="mt-4 w-full">
                      管理出版社
                    </Button>
                  </Link>
                </CardContent>
              </Card>
            </div>
            <div className="grid gap-6 md:grid-cols-2">
              <Card>
                <CardHeader>
                  <CardTitle>最近活动</CardTitle>
                </CardHeader>
                <CardContent>
                  <div className="space-y-4">
                    <div className="flex items-center gap-4">
                      <div className="rounded-full bg-primary/10 p-2">
                        <Package className="h-4 w-4 text-primary" />
                      </div>
                      <div className="flex-1">
                        <p className="text-sm font-medium">新订单 #1234</p>
                        <p className="text-xs text-muted-foreground">10分钟前</p>
                      </div>
                    </div>
                    <div className="flex items-center gap-4">
                      <div className="rounded-full bg-primary/10 p-2">
                        <Users className="h-4 w-4 text-primary" />
                      </div>
                      <div className="flex-1">
                        <p className="text-sm font-medium">新用户注册</p>
                        <p className="text-xs text-muted-foreground">30分钟前</p>
                      </div>
                    </div>
                    <div className="flex items-center gap-4">
                      <div className="rounded-full bg-primary/10 p-2">
                        <BookOpen className="h-4 w-4 text-primary" />
                      </div>
                      <div className="flex-1">
                        <p className="text-sm font-medium">新图书添加</p>
                        <p className="text-xs text-muted-foreground">1小时前</p>
                      </div>
                    </div>
                  </div>
                </CardContent>
              </Card>
              <Card>
                <CardHeader>
                  <CardTitle>快速操作</CardTitle>
                </CardHeader>
                <CardContent className="space-y-2">
                  <Link href="/admin/books/add">
                    <Button variant="outline" className="w-full justify-start">
                      <BookOpen className="mr-2 h-4 w-4" />
                      添加新图书
                    </Button>
                  </Link>
                  <Link href="/admin/publishers/add">
                    <Button variant="outline" className="w-full justify-start">
                      <ShoppingBag className="mr-2 h-4 w-4" />
                      添加新出版社
                    </Button>
                  </Link>
                  <Link href="/admin/users">
                    <Button variant="outline" className="w-full justify-start">
                      <Users className="mr-2 h-4 w-4" />
                      管理用户权限
                    </Button>
                  </Link>
                </CardContent>
              </Card>
            </div>
          </div>
        </main>
      </div>
    </div>
  )
}
