"use client"; import React, { useState, useEffect, useCallback, useRef } from "react"; import Link from "next/link"; import Image from "next/image"; import { usePathname, useRouter } from "next/navigation"; import { Home, ShoppingBag, ShoppingCart, Menu, Pill, ShieldCheck, HeartPulse, BookOpen, FileText, Video, Building2, FileHeart, User, Search, ChevronLeft, Dog, PhoneCall, Sparkles, ArrowRight } from "lucide-react"; import { useCartStore } from "../lib/store/cartStore"; import { useUserStore } from "../lib/store/userStore"; import { useUIStore } from "../lib/store/uiStore"; import { useSettingsStore } from "../lib/store/settingsStore"; import { useCatalogMode } from "../lib/useCatalogMode"; import { productService } from "../lib/services/productService"; import { Product } from "../lib/data/products"; import { toPersian } from "../lib/utils"; const SOLUTION_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 MobileBottomNav({ onPrescriptionOpen, }: { onPrescriptionOpen?: () => void; }) { const pathname = usePathname(); const router = useRouter(); const [activeSheet, setActiveSheet] = useState<"shop" | "menu" | null>(null); const [searchQuery, setSearchQuery] = useState(""); const [searchResults, setSearchResults] = useState([]); const [isSearching, setIsSearching] = useState(false); const [isMounted, setIsMounted] = useState(false); const isNavigatingBackRef = useRef(false); const { getTotalItems } = useCartStore(); const setCartOpen = useUIStore((state) => state.setCartOpen); const setB2BPortalOpen = useUIStore((state) => state.setB2BPortalOpen); const isB2BEnabled = useSettingsStore((s) => s.getBoolean('b2bRegistrationOpen', true) && s.getBoolean('b2b_enabled', true)); const isPrescriptionEnabled = useSettingsStore((s) => s.getText('PRESCRIPTION_UPLOAD_ENABLED', 'true') !== 'false'); const { isCatalogOnly, allowCart } = useCatalogMode(); const isCartDisabled = isCatalogOnly && !allowCart; const { isLoggedIn, setAuthModalOpen } = useUserStore(); useEffect(() => { setIsMounted(true); }, []); // Close sheet with history navigation support (Back button dismiss) const closeSheet = useCallback((shouldPopHistory: boolean | React.MouseEvent = true) => { if (!activeSheet) return; setActiveSheet(null); const doPop = typeof shouldPopHistory === "boolean" ? shouldPopHistory : true; if (doPop && typeof window !== "undefined" && window.location.hash === "#sheet") { isNavigatingBackRef.current = true; window.history.back(); } else if (!doPop && typeof window !== "undefined" && window.location.hash === "#sheet") { // Remove hash without triggering history.back() which cancels router navigation window.history.replaceState(null, "", window.location.pathname + window.location.search); } }, [activeSheet]); const navigateAndClose = useCallback((href: string) => { closeSheet(false); router.push(href); }, [closeSheet, router]); const openSheet = useCallback((sheet: "shop" | "menu") => { setActiveSheet(sheet); if (typeof window !== "undefined") { window.history.pushState({ mobileSheet: sheet }, "", "#sheet"); } }, []); // Handle hardware / browser back button popstate useEffect(() => { const handlePopState = () => { if (isNavigatingBackRef.current) { isNavigatingBackRef.current = false; return; } if (activeSheet) { setActiveSheet(null); } }; window.addEventListener("popstate", handlePopState); return () => window.removeEventListener("popstate", handlePopState); }, [activeSheet]); // Close sheet on route change useEffect(() => { setActiveSheet(null); setSearchQuery(""); setSearchResults([]); }, [pathname]); // Live search debounced query useEffect(() => { if (!searchQuery.trim()) { setSearchResults([]); setIsSearching(false); return; } const timer = setTimeout(async () => { setIsSearching(true); try { const res = await productService.getProducts({ query: searchQuery.trim(), limit: 6 }); setSearchResults(res.data || []); } catch (err) { console.error("Live search failed:", err); } finally { setIsSearching(false); } }, 250); return () => clearTimeout(timer); }, [searchQuery]); const handleSearchSubmit = (e: React.FormEvent) => { e.preventDefault(); if (searchQuery.trim()) { const q = searchQuery.trim(); closeSheet(false); router.push(`/shop?search=${encodeURIComponent(q)}`); } else { closeSheet(false); router.push("/shop"); } }; const handleAccountClick = () => { if (isLoggedIn) { router.push("/dashboard"); } else { setAuthModalOpen(true); } }; const totalCartItems = isMounted ? getTotalItems() : 0; const isHomeActive = pathname === "/"; const isShopActive = pathname.startsWith("/shop") || activeSheet === "shop"; const isAccountActive = pathname.startsWith("/dashboard"); return ( <> {/* 1. SHOP & SOLUTIONS BOTTOM SHEET (Modal for Shop Tab) */} {activeSheet === "shop" && (
e.stopPropagation()} dir="rtl" > {/* Grab handle */}
{/* Header with Direct Shop Link */}

فروشگاه و راهکارهای دارویی

جستجو در داروها، مکمل‌ها و علائم بالینی

{/* Search Input */}
setSearchQuery(e.target.value)} className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2.5 pr-10 pl-14 text-xs outline-none focus:border-canina-blue font-vazir text-medical-gray-900" autoFocus /> {/* Live Search Results (if typing) */} {searchQuery.trim().length > 0 && (
{isSearching ? "در حال جستجو..." : `نتایج زنده (${toPersian(searchResults.length)})`}
{searchResults.length === 0 && !isSearching ? (

دارویی با این نام یافت نشد

) : (
{searchResults.map((prod) => ( ))}
)}
)} {/* Direct Link to All Products */} {/* Prescription Upload Banner CTA if enabled */} {isPrescriptionEnabled && ( )} {/* 4 Major Clinical Categories with Symptoms */}
دسته‌بندی‌ها و راهکارهای درمانی:
{SOLUTION_ITEMS.map((item) => (
{item.solutions.map((sol, sIdx) => ( ))}
))}
)} {/* 2. MENU BOTTOM SHEET (Modal for Menu Tab) */} {activeSheet === "menu" && (
e.stopPropagation()} dir="rtl" > {/* Grab handle */}
{/* Header */}

منوی بخش‌های کنینا

{/* Navigation Links Grid */}
{isB2BEnabled && ( )}
{/* Contact & Support */}
)} {/* 3. FIXED 5-TAB APP BOTTOM NAVIGATION BAR */} ); }