"use client"; import { useState, MouseEvent, useEffect, useRef } from "react"; import { Heart } from "lucide-react"; import { toast } from "sonner"; import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious, type CarouselApi, } from "@/components/ui/carousel"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; type ProductCardProps = { id: number; name: string; price: number | null; struct_price_text: string; discount?: number | null; discount_text?: string | null; images: string[]; is_favorite: boolean; labels?: { text: string; bg_color: string }[]; price_color?: string; height?: number; width?: number; button?: boolean; }; export default function ProductCard({ id, name, price, struct_price_text, discount, discount_text, images, is_favorite, labels = [], price_color = "#005bff", height = 360, width = 280, button = true, }: ProductCardProps) { const [favorite, setFavorite] = useState(is_favorite); const [api, setApi] = useState(); const [current, setCurrent] = useState(0); const autoplayRef = useRef(null); const hasMultipleImages = images.length > 1; // Track carousel current slide useEffect(() => { if (!api) return; setCurrent(api.selectedScrollSnap()); api.on("select", () => { setCurrent(api.selectedScrollSnap()); }); }, [api]); // Auto-play functionality - 3 seconds useEffect(() => { if (!api || !hasMultipleImages) return; const startAutoplay = () => { autoplayRef.current = setInterval(() => { if (api.canScrollNext()) { api.scrollNext(); } else { api.scrollTo(0); } }, 3000); }; const stopAutoplay = () => { if (autoplayRef.current) { clearInterval(autoplayRef.current); autoplayRef.current = null; } }; startAutoplay(); return () => stopAutoplay(); }, [api, hasMultipleImages]); const handleFavorite = async (e: MouseEvent) => { e.preventDefault(); e.stopPropagation(); const newFavoriteState = !favorite; setFavorite(newFavoriteState); if (newFavoriteState) { toast.success("Товар добавлен в избранное"); } else { toast.success("Товар удален из избранного"); } }; const handleCardClick = (e: MouseEvent) => { const target = e.target as HTMLElement; if ( target.closest('button') || target.closest('[data-carousel-control="true"]') ) { e.preventDefault(); } }; const handleNavClick = (e: MouseEvent, action: () => void) => { e.preventDefault(); e.stopPropagation(); action(); }; return ( {/* Image Section with Carousel */}
{images.map((image, index) => (
{`${name}
))}
{/* Navigation Arrows - Only show if multiple images */} {hasMultipleImages && ( <> handleNavClick(e, () => api?.scrollPrev())} /> handleNavClick(e, () => api?.scrollNext())} /> )}
{/* Favorite Button */} {/* Image Indicators */} {hasMultipleImages && (
{images.map((_, index) => (
)} {/* Labels */} {labels?.length > 0 && (
{labels.map((label, idx) => ( {label.text} ))}
)}
{/* Content */}

{struct_price_text}

{name}

); }