"use client"
import type React from "react"
import { useState, useEffect } from "react"
import { useAuth } from "@/contexts/auth-context"
import {
LayoutDashboard,
Users,
Bot,
Trophy,
BarChart3,
Settings,
Menu,
X,
Bell,
Search,
LogOut,
User,
UserPlus,
FileText,
AlertTriangle,
Award,
Info,
} from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Badge } from "@/components/ui/badge"
import { Card, CardContent } from "@/components/ui/card"
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/components/ui/dialog"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
interface AdminLayoutProps {
children: React.ReactNode
currentPage: string
onPageChange: (page: string) => void
}
interface Notification {
id: string
type: "user_registration" | "app_submission" | "competition_update" | "system_alert" | "review_completed"
title: string
message: string
timestamp: string
read: boolean
}
interface SearchResult {
id: string
type: "user" | "app" | "competition"
title: string
subtitle: string
avatar?: string
}
const menuItems = [
{ id: "dashboard", name: "儀表板", icon: LayoutDashboard },
{ id: "users", name: "用戶管理", icon: Users },
{ id: "apps", name: "應用管理", icon: Bot },
{ id: "competitions", name: "競賽管理", icon: Trophy },
{ id: "analytics", name: "數據分析", icon: BarChart3 },
{ id: "settings", name: "系統設定", icon: Settings },
]
// Notifications data - empty for production
const mockNotifications: Notification[] = []
// Search data - empty for production
const mockSearchData: SearchResult[] = []
export function AdminLayout({ children, currentPage, onPageChange }: AdminLayoutProps) {
const { user, logout, isLoading } = useAuth()
const [sidebarOpen, setSidebarOpen] = useState(true)
// 認證檢查
if (isLoading) {
return (
)
}
if (!user) {
return (
需要登入
請先登入才能訪問管理員頁面
)
}
if (user.role !== 'admin') {
return (
權限不足
您沒有訪問管理員頁面的權限
)
}
// Search state
const [searchQuery, setSearchQuery] = useState("")
const [searchResults, setSearchResults] = useState([])
const [showSearchResults, setShowSearchResults] = useState(false)
// Notification state
const [notifications, setNotifications] = useState(mockNotifications)
const [showNotifications, setShowNotifications] = useState(false)
// Logout confirmation state
const [showLogoutDialog, setShowLogoutDialog] = useState(false)
// Handle search
useEffect(() => {
if (searchQuery.trim()) {
const filtered = mockSearchData.filter(
(item) =>
item.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
item.subtitle.toLowerCase().includes(searchQuery.toLowerCase()),
)
setSearchResults(filtered.slice(0, 8)) // Limit to 8 results
setShowSearchResults(true)
} else {
setSearchResults([])
setShowSearchResults(false)
}
}, [searchQuery])
// Get unread notification count
const unreadCount = notifications.filter((n) => !n.read).length
// Format timestamp
const formatTimestamp = (timestamp: string) => {
const now = new Date()
const time = new Date(timestamp)
const diffInMinutes = Math.floor((now.getTime() - time.getTime()) / (1000 * 60))
if (diffInMinutes < 1) return "剛剛"
if (diffInMinutes < 60) return `${diffInMinutes} 分鐘前`
if (diffInMinutes < 1440) return `${Math.floor(diffInMinutes / 60)} 小時前`
return `${Math.floor(diffInMinutes / 1440)} 天前`
}
// Get notification icon and color
const getNotificationIcon = (type: string) => {
switch (type) {
case "user_registration":
return
case "app_submission":
return
case "competition_update":
return
case "system_alert":
return
case "review_completed":
return
default:
return
}
}
// Get search result icon
const getSearchIcon = (type: string) => {
switch (type) {
case "user":
return
case "app":
return
case "competition":
return
default:
return
}
}
// Mark notification as read
const markAsRead = (notificationId: string) => {
setNotifications((prev) => prev.map((n) => (n.id === notificationId ? { ...n, read: true } : n)))
}
// Mark all notifications as read
const markAllAsRead = () => {
setNotifications((prev) => prev.map((n) => ({ ...n, read: true })))
}
// Handle logout with improved UX
const handleLogout = () => {
logout()
setShowLogoutDialog(false)
// Check if this is a popup/new tab opened from main site
if (typeof window !== 'undefined' && window.opener && !window.opener.closed) {
// If opened from another window, close this tab and focus parent
window.opener.focus()
window.close()
} else {
// If this is the main window or standalone, redirect to homepage
window.location.href = "/"
}
}
// Handle search result click
const handleSearchResultClick = (result: SearchResult) => {
setSearchQuery("")
setShowSearchResults(false)
// Navigate based on result type
switch (result.type) {
case "user":
onPageChange("users")
break
case "app":
onPageChange("apps")
break
case "competition":
onPageChange("competitions")
break
}
}
if (!user || user.role !== "admin") {
return (
存取被拒
您沒有管理員權限訪問此頁面
{typeof window !== 'undefined' && window.opener && !window.opener.closed && (
)}
)
}
return (
{/* Sidebar */}
{/* Logo */}
{/* Navigation */}
{/* User Info */}
{user.name.charAt(0)}
{sidebarOpen && (
)}
{/* Main Content */}
{/* Top Bar */}
{/* Page Content */}
{children}
{/* Logout Confirmation Dialog */}
{/* Click outside to close search results */}
{showSearchResults &&
setShowSearchResults(false)} />}
)
}