'use client'; import { useEffect, useState } from 'react'; import { useRouter, usePathname } from 'next/navigation'; import Link from 'next/link'; import { useAuthStore } from '@/lib/store'; const menuItems = [ { href: '/admin', label: 'Dashboard', icon: 'π' }, { href: '/admin/visitor-stats', label: 'Visitor Stats', icon: 'ποΈ' }, { href: '/admin/cars', label: 'Cars', icon: 'π' }, { href: '/admin/vehicle-requests', label: 'Vehicle Requests', icon: 'π' }, { href: '/admin/purchased', label: 'Purchased Vehicles', icon: 'π¦' }, { href: '/admin/dealers', label: 'Dealers', icon: 'π€' }, { href: '/admin/payments', label: 'Payments', icon: 'π³' }, { href: '/admin/withdrawals', label: 'Withdrawals', icon: 'πΈ' }, { href: '/admin/notifications', label: 'Notifications', icon: 'π' }, { href: '/admin/translations', label: 'Translations', icon: 'π' }, { href: '/admin/dealer-translations', label: 'Dealer Descriptions', icon: 'π' }, { href: '/admin/users', label: 'Users', icon: 'π₯' }, { href: '/admin/inquiries', label: 'Inquiries', icon: 'π¬' }, { href: '/admin/settings', label: 'Settings', icon: 'βοΈ' }, ]; export default function AdminLayout({ children, }: { children: React.ReactNode; }) { const router = useRouter(); const pathname = usePathname(); const { user, token, logout, isLoading: authLoading } = useAuthStore(); const [sidebarOpen, setSidebarOpen] = useState(true); // λλ²κ·Έ λ‘κ·Έ useEffect(() => { console.log('Admin Layout Debug:', { pathname, token: token ? 'exists' : 'null', user: user ? { id: user.id, email: user.email, is_admin: user.is_admin } : null, authLoading }); }, [pathname, token, user, authLoading]); useEffect(() => { // λ‘κ·ΈμΈ νμ΄μ§λ μ²΄ν¬ νμ μμ if (pathname === '/admin/login') { return; } // μμ§ λ‘λ© μ€μ΄λ©΄ λκΈ° if (authLoading) { return; } // ν ν°μ΄ μμΌλ©΄ λ‘κ·ΈμΈ νμ΄μ§λ‘ if (!token) { router.push('/admin/login'); return; } // user μ λ³΄κ° μμΌλ©΄ λ‘κ·ΈμΈ νμ΄μ§λ‘ if (!user) { router.push('/admin/login'); return; } // κ΄λ¦¬μκ° μλλ©΄ νμΌλ‘ if (!user.is_admin) { console.log('User is not admin, redirecting to home'); router.push('/'); return; } }, [pathname, router, token, user, authLoading]); const handleLogout = () => { logout(); router.push('/admin/login'); }; // λ‘κ·ΈμΈ νμ΄μ§λ λ μ΄μμ μμ΄ λ λλ§ if (pathname === '/admin/login') { return <>{children}>; } // λ‘λ© μ€ if (authLoading) { return (