"use client"; import React, { useState, useRef, useEffect } from "react"; import { Search, ChevronDown, Menu, X, Pill, ShieldCheck, HeartPulse, Sparkles, ShoppingBag, User, PlusCircle, Check, Building2, LogIn, Wallet, LogOut, MapPin, FileHeart, Dog, Cat } from "lucide-react"; import { motion, AnimatePresence } from "motion/react"; import Link from "next/link"; import { useCartStore } from "../lib/store/cartStore"; import { usePetStore } from "../lib/store/usePetStore"; import { useUserStore } from "../lib/store/userStore"; import { useSettingsStore } from "../lib/store/settingsStore"; import { toast } from "sonner"; import HeaderButton from "./HeaderButton"; import AuthModal from "./AuthModal"; import { cn } from "../lib/utils"; import { productService } from "../lib/services/productService"; const MENU_ICONS: Record = { joints: , immune: , energy: , "special-care": , }; export default function Header({ onNavigate, onShopNavigate, currentView, onCartOpen, onSearch, onB2BOpen }: { onNavigate: (v: any) => void, onShopNavigate: (c?: string, s?: string) => void, currentView: string, onCartOpen: () => void, onSearch: (q: string) => void, onB2BOpen: () => void }) { const [isMegaMenuOpen, setIsMegaMenuOpen] = useState(false); const [isSearchFocused, setIsSearchFocused] = useState(false); const [isAuthModalOpen, setIsAuthModalOpen] = useState(false); const [isPetSwitcherOpen, setIsPetSwitcherOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(""); const [isMobileMenuOpen, setIsMobileMenuOpen] = useState(false); const [isMobileSolutionsOpen, setIsMobileSolutionsOpen] = useState(false); const [menuItems, setMenuItems] = useState<{ id: string; title: string; icon: React.ReactNode; solutions: string[]; }[]>([ { id: "joints", title: "مفاصل و استخوان", icon: MENU_ICONS["joints"], solutions: ["لنگیدن", "سختی در بلند شدن", "درد مفاصل", "رشد سریع توله‌سگ", "پاهای پرانتزی", "ضعف تاندون"] }, { id: "immune", title: "تقویت سیستم ایمنی و گوارش", icon: MENU_ICONS["immune"], solutions: ["ضعف بعد از بیماری", "بی‌اشتهایی", "بعد از زایمان", "اسهال", "مشکلات گوارشی"] }, { id: "energy", title: "ویتامین‌ها و انرژی‌بخش‌ها", icon: MENU_ICONS["energy"], solutions: ["نفس‌نفس زدن", "خستگی", "سن بالا", "بی‌حالی"] }, { id: "special-care", title: "مراقبت‌های ویژه (پوست، دندان و چشم)", icon: MENU_ICONS["special-care"], solutions: ["ریزش مو", "خشکی پوست", "خارش", "جرم دندان", "سوزش چشم"] } ]); const petMenuRef = useRef(null); const { getTotalItems } = useCartStore(); const { pets, activePetId, setActivePet, getActivePet } = usePetStore(); const { role, isLoggedIn, logout, profile } = useUserStore(); const activePet = getActivePet(); const getText = useSettingsStore(state => state.getText); useEffect(() => { const loadNavFilters = async () => { try { const data = await productService.getNavigationFilters(); if (data && data.length > 0) { const mapped = data.map(item => ({ id: item.slug, title: item.name, icon: MENU_ICONS[item.slug] || , solutions: item.symptoms || [] })); setMenuItems(mapped); } } catch (e) { console.error("Failed to load navigation filters:", e); } }; loadNavFilters(); }, []); // Click Outside logic useEffect(() => { function handleClickOutside(event: MouseEvent) { if (petMenuRef.current && !petMenuRef.current.contains(event.target as Node)) { setIsPetSwitcherOpen(false); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const handleSearch = (e: React.FormEvent) => { e.preventDefault(); if (searchQuery.trim()) { onSearch(searchQuery); setIsSearchFocused(false); } }; return ( <> setIsAuthModalOpen(false)} />
🚚 {getText('shipping_notice', "ارسال رایگان برای سفارش‌های بالای ۱۵۰ هزار تومان")}
{/* Logo & Mobile Toggle Area */}
{/* Mobile Menu Toggle (visible below 800px) */} {/* Logo Section */} setIsMobileMenuOpen(false)} >
C
Canina {getText('brand_name_fa', "ایران")} {getText('brand_subtitle', "نماینده رسمی Canina Pharma GmbH آلمان")}
{/* Navigation Items: Centered on XL screens (1224px+) */} {/* Action Area */}
{/* Elastic Search */}
{isSearchFocused && (
setSearchQuery(e.target.value)} onBlur={() => !searchQuery && setIsSearchFocused(false)} placeholder="جستجو…" dir="rtl" className="w-full bg-transparent py-2 pr-2 text-[12px] font-black outline-none font-vazir text-medical-gray-900 text-right placeholder:text-medical-gray-400 placeholder:text-right" />
)}
{ if (isSearchFocused && searchQuery.trim()) { onSearch(searchQuery); setIsSearchFocused(false); } else { setIsSearchFocused(!isSearchFocused); } }} >
{isLoggedIn ? (
{/* User Name (Direct link to dashboard) */}
onNavigate("user-dashboard")} > {profile.firstName}
{/* Separator Line */}
{/* Pet Name & Switcher */}
setIsPetSwitcherOpen(!isPetSwitcherOpen)} > {activePet?.type === "گربه" ? ( ) : ( )} {activePet?.name || "بدون پت"}
{isPetSwitcherOpen && (
{getText('nav_pet_switcher_title', "تغییر پت یا مدیریت حساب")}
{pets.map(pet => (
{ setActivePet(pet.id); setIsPetSwitcherOpen(false); toast.success(`پت فعال به ${pet.name} تغییر یافت`); }} >
{pet.name[0]}
{pet.name}
{activePetId === pet.id && }
))}
)}
) : ( setIsAuthModalOpen(true)} icon={} > {getText('nav_login', "ورود / ثبت‌نام")} ورود )} } >
{getText('nav_cart', "سبد خرید")}
{/* Medium Screen Sub-Navbar (Visible only between 800px and 1224px) */}
{isMegaMenuOpen && ( {menuItems.map((item, idx) => (
setIsMegaMenuOpen(false)} className="flex items-center gap-3 mb-3 cursor-pointer block" >
{item.icon}

{item.title}

{item.solutions.length > 0 && (
{item.solutions.slice(0, 6).map((sol, sIdx) => ( setIsMegaMenuOpen(false)} > {sol} ))}
)}
))}
)}
{[ { id: 'shop', label: getText('nav_products', 'محصولات تخصصی'), href: '/shop' }, { id: 'wiki', label: getText('nav_wiki', 'دانشنامه علمی'), href: '/wiki' }, { id: 'blog', label: getText('nav_blog', 'مجله سلامت پت'), href: '/blog' }, { id: 'profile', label: getText('nav_pet_profiles', 'شناسنامه پت'), href: '/profile' } ].map((item) => ( {item.label} ))}
{/* Mobile Drawer Menu */} {isMobileMenuOpen && ( <> {/* Backdrop */} setIsMobileMenuOpen(false)} className="fixed inset-0 bg-black z-40 min-[800px]:hidden" /> {/* Drawer */}
setIsMobileMenuOpen(false)}>
C
Canina
{/* Navigation Links */}
)}
); }