"use client"; import React, { useState, useRef, useEffect } from "react"; import { Search, ChevronDown, Menu, X, Pill, ShieldCheck, HeartPulse, Sparkles, ShoppingBag, User, Check, Building2, LogIn, LogOut, FileHeart, Dog, BookOpen, FileText, Video, Award, PhoneCall, Activity } 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 AuthModal from "./AuthModal"; import PrescriptionUploadModal from "./PrescriptionUploadModal"; import TickerBanner from "./TickerBanner"; import { cn } from "../lib/utils"; import { productService } from "../lib/services/productService"; const MENU_ICONS: Record = { joints: , immune: , energy: , "special-care": , }; import { NavigationTarget } from "../lib/types"; export default function Header({ onCartOpen = () => {}, onSearch = () => {}, onB2BOpen = () => {} }: { onNavigate?: (v: NavigationTarget) => void; onShopNavigate?: (c?: string, s?: string) => void; currentView?: string; onCartOpen?: () => void; onSearch?: (q: string) => void; onB2BOpen?: () => void; }) { const [isMegaMenuOpen, setIsMegaMenuOpen] = useState(false); const [activeDropdown, setActiveDropdown] = useState(null); const [isAuthModalOpen, setIsAuthModalOpen] = useState(false); const [isPrescriptionModalOpen, setIsPrescriptionModalOpen] = useState(false); const [isProfileMenuOpen, setIsProfileMenuOpen] = useState(false); const userProfileRef = useRef(null); const [searchQuery, setSearchQuery] = useState(""); const [isMobileMenuOpen, setIsMobileMenuOpen] = 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 [isMounted, setIsMounted] = useState(false); useEffect(() => { Promise.resolve().then(() => setIsMounted(true)); }, []); const { getTotalItems } = useCartStore(); const { pets, activePetId, setActivePet } = usePetStore(); const { isLoggedIn, logout, profile } = useUserStore(); const getText = useSettingsStore(state => state.getText); useEffect(() => { const loadNavFilters = async () => { try { const data = await productService.getNavigationFilters(); if (data && data.length > 0) { setMenuItems(data.map(item => ({ id: item.slug, title: item.name, icon: MENU_ICONS[item.slug] || , solutions: item.symptoms || [] }))); } } catch (err) { console.error('Failed to load navigation filters:', err); } }; loadNavFilters(); }, []); useEffect(() => { function handleClickOutside(event: MouseEvent) { if (userProfileRef.current && !userProfileRef.current.contains(event.target as Node)) { setIsProfileMenuOpen(false); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const handleSearch = (e: React.FormEvent) => { e.preventDefault(); if (searchQuery.trim()) { onSearch(searchQuery); } }; return ( <> setIsAuthModalOpen(false)} /> setIsPrescriptionModalOpen(false)} />
{/* Sliding Announcement Ticker Banner */}
{/* TOP ROW: Logo + Search + Primary Actions */}
{/* Mobile Menu Toggle & Logo */}
{(getText('BRAND_LOGO_URL', '') || getText('site_logo', '')) ? ( {getText('brand_name_fa', ) : ( <>
C
Canina {getText('brand_name_fa', "ایران")} نماینده رسمی CANINA PHARMA GMBH GERMANY
)}
{/* Center Search Input (Desktop) */}
setSearchQuery(e.target.value)} className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-2xl py-2.5 pr-10 pl-4 text-xs font-bold text-medical-gray-900 focus:outline-none focus:ring-2 focus:ring-canina-blue/20 focus:bg-white transition-all" />
{/* Left Action Buttons */}
{/* Prescription Upload Button */} {/* User Account Button with Dropdown & Pets List */} {isLoggedIn ? (
{isProfileMenuOpen && ( {/* User info header */}

{profile.firstName ? `${profile.firstName} ${profile.lastName || ''}` : 'کاربر عزیز'}

{profile.mobile || profile.email}

{/* Pets List Section */}
پت‌های من ({pets.length}) setIsProfileMenuOpen(false)} className="text-canina-blue hover:underline text-[10px]"> مدیریت
{pets.length > 0 ? (
{pets.map((p) => ( ))}
) : (

پتی ثبت نشده است

)}
{/* Direct Navigation Links */} setIsProfileMenuOpen(false)} className="flex items-center gap-2 px-3 py-2 hover:bg-medical-gray-50 text-medical-gray-700 rounded-xl font-bold transition-colors" > داشبورد کاربری )}
) : ( )} {/* Shopping Cart Button */}
{/* BOTTOM ROW: Clean Organized Dropdown Navigation Bar (Desktop) */}
{/* Dropdown 1: Category Mega-Menu */}
setIsMegaMenuOpen(true)} onMouseLeave={() => setIsMegaMenuOpen(false)} > {isMegaMenuOpen && ( {menuItems.map((item, idx) => (
setIsMegaMenuOpen(false)} className="flex items-center gap-3 mb-3 cursor-pointer" >
{item.icon}

{item.title}

{item.solutions.slice(0, 6).map((sol, sIdx) => ( setIsMegaMenuOpen(false)} > {sol} ))}
))}
)}
{/* Dropdown 2: Products & Catalog */}
setActiveDropdown('products')} onMouseLeave={() => setActiveDropdown(null)} > {activeDropdown === 'products' && (
کاتالوگ جامع محصولات کاتالوگ آنلاین و دوز مصرفی
)}
{/* Dropdown 3: Science & Academy */}
setActiveDropdown('science')} onMouseLeave={() => setActiveDropdown(null)} > {activeDropdown === 'science' && (
دانشنامه علمی کنینا مجله سلامت پت (وبلاگ)
)}
{/* Dropdown 4: Smart Tools & Pet Profile */}
setActiveDropdown('tools')} onMouseLeave={() => setActiveDropdown(null)} > {activeDropdown === 'tools' && (
شناسنامه و سوابق سلامت پت پایش هوشمند مصرف مکمل‌ها
)}
{/* Dropdown 5: Company & B2B */}
setActiveDropdown('company')} onMouseLeave={() => setActiveDropdown(null)} > {activeDropdown === 'company' && (
درباره کمپانی کنینا آلمان نمادهای اعتماد و مجوزهای رسمی تماس با مرکز پشتیبانی
)}
{/* Mobile Navigation Drawer & Backdrop */} {isMobileMenuOpen && ( <> setIsMobileMenuOpen(false)} className="lg:hidden fixed inset-0 bg-black/50 z-40 backdrop-blur-xs" />
setSearchQuery(e.target.value)} className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-3 pr-10 pl-4 text-xs font-bold" />
setIsMobileMenuOpen(false)} className="block p-3 rounded-xl bg-medical-gray-50 text-medical-gray-900"> 🛒 محصولات تخصصی کنینا setIsMobileMenuOpen(false)} className="block p-3 rounded-xl hover:bg-medical-gray-50 text-medical-gray-800"> 📑 کاتالوگ آنلاین و دوز مصرفی setIsMobileMenuOpen(false)} className="block p-3 rounded-xl hover:bg-medical-gray-50 text-medical-gray-800"> 🧬 دانشنامه علمی setIsMobileMenuOpen(false)} className="block p-3 rounded-xl hover:bg-medical-gray-50 text-medical-gray-800"> 📰 مجله سلامت پت setIsMobileMenuOpen(false)} className="block p-3 rounded-xl hover:bg-medical-gray-50 text-medical-gray-800"> 🎥 آکادمی ویدئویی و مشاوره دامپزشک setIsMobileMenuOpen(false)} className="block p-3 rounded-xl hover:bg-medical-gray-50 text-medical-gray-800"> 🐾 شناسنامه و سوابق سلامت پت
)}
); }