"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"; const MENU_ITEMS = [ { id: "joints", title: "مفاصل و استخوان", icon: , solutions: ["آرتروز سگهای پیر", "رشد استخوانی تولهسگ", "تقویت رباط و تاندون"] }, { id: "immune", title: "تقویت سیستم ایمنی و گوارش", icon: , solutions: ["پیشگیری از بیماری", "رفع اسهال و یبوست", "پروبیوتیکها"] }, { id: "energy", title: "ویتامینها و انرژیبخشها", icon: , solutions: ["مکملهای مولتیویتامین", "افزایش اشتها", "رشد و بلوغ"] }, { id: "special-care", title: "مراقبتهای ویژه (پوست، دندان و چشم)", icon: , solutions: ["سلامت پوست و مفاصل", "رفع جرم دندان", "شستشوی چشم"] } ]; 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 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); // 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', "ارسال رایگان برای سفارشهای بالای ۱۵۰ هزار تومان")} {/* Mobile Menu Toggle */} setIsMobileMenuOpen(!isMobileMenuOpen)} className="lg:hidden p-2 rounded-xl text-medical-gray-600 hover:bg-medical-gray-50 transition-all" aria-label="منو" > {isMobileMenuOpen ? : } {/* Logo Section */} setIsMobileMenuOpen(false)} > C Canina {getText('brand_name_fa', "ایران")} {getText('brand_subtitle', "نماینده رسمی Canina Pharma GmbH آلمان")} {/* Navigation Items */} setIsMegaMenuOpen(true)} onMouseLeave={() => setIsMegaMenuOpen(false)} > {getText('nav_solutions', "دسته بندی درمانی")} {isMegaMenuOpen && ( {MENU_ITEMS.map((item, idx) => ( setIsMegaMenuOpen(false)} className="flex items-center gap-4 mb-4 cursor-pointer block" > {item.icon} {item.title} {item.solutions.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} ))} {/* Action Area */} {/* Elastic Search */} {isSearchFocused && ( setSearchQuery(e.target.value)} onBlur={() => !searchQuery && setIsSearchFocused(false)} placeholder="جستجوی محصولات، مقالات سلامت و غیره…" dir="rtl" className="w-full bg-transparent py-2 pr-4 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 && } { e.stopPropagation(); setActivePet(pet.id); onNavigate({ view: "profile", subview: "detail" }); setIsPetSwitcherOpen(false); }} className="p-1.5 rounded-lg hover:bg-canina-blue hover:text-white transition-all text-medical-gray-300 hover:shadow-lg" title="مشاهده شناسنامه سلامت" > ))} { onNavigate({ view: "profile", subview: "add" }); setIsPetSwitcherOpen(false); }} className="w-full flex items-center gap-3 p-3 text-canina-blue hover:bg-canina-blue/5 rounded-xl transition-all text-[12px] font-black font-vazir" > {getText('nav_register_pet', "ثبت همدم (Pet) جدید")} { onNavigate({ view: "user-dashboard" }); setIsPetSwitcherOpen(false); }} className="w-full flex items-center gap-3 p-3 text-medical-gray-600 hover:bg-medical-gray-50 rounded-xl transition-all text-[12px] font-bold font-vazir" > {getText('nav_wallet_orders', "کیف پول و سفارشات")} { logout(); setIsPetSwitcherOpen(false); }} className="w-full flex items-center gap-3 p-3 text-red-500 hover:bg-red-50 rounded-xl transition-all text-[12px] font-bold font-vazir mt-1" > {getText('nav_logout', "خروج از حساب کانینا")} )} ) : ( setIsAuthModalOpen(true)} icon={} > {getText('nav_login', "ورود / ثبتنام")} )} } > {getText('nav_cart', "سبد خرید")} {/* Mobile Drawer Menu */} {isMobileMenuOpen && ( <> {/* Backdrop */} setIsMobileMenuOpen(false)} className="fixed inset-0 bg-black z-40 lg:hidden" /> {/* Drawer */} setIsMobileMenuOpen(false)}> C Canina setIsMobileMenuOpen(false)} className="p-2 rounded-xl hover:bg-medical-gray-50 text-medical-gray-600 transition-all" > {/* Navigation Links */} {/* Treatment Solutions accordion */} setIsMobileSolutionsOpen(!isMobileSolutionsOpen)} className="w-full flex items-center justify-between px-4 py-3 rounded-xl text-sm font-black text-medical-gray-600 hover:bg-medical-gray-50 transition-all font-vazir" > {getText('nav_solutions', "دسته بندی درمانی")} {isMobileSolutionsOpen && ( {MENU_ITEMS.map((item, idx) => ( setIsMobileMenuOpen(false)} className="text-[13px] font-bold text-medical-gray-800 font-vazir hover:text-canina-blue cursor-pointer flex items-center gap-2 block" > {item.icon} {item.title} {item.solutions.map((sol, sIdx) => ( setIsMobileMenuOpen(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) => ( setIsMobileMenuOpen(false)} className={`w-full block text-right px-4 py-3 rounded-xl text-sm font-black transition-all font-vazir ${currentView === item.id ? 'bg-canina-blue/5 text-canina-blue' : 'text-medical-gray-600 hover:bg-medical-gray-50' }`} > {item.label} ))} > )} > ); }