'use client' import { useState, useEffect } from 'react'; interface TimeLeft { days: number; hours: number; minutes: number; seconds: number; } interface SaleCountdownProps { hasActiveSale: boolean; maxSalePercentage?: number; saleEndDate?: string | null; } export function SaleCountdown({ hasActiveSale, maxSalePercentage = 5, saleEndDate }: SaleCountdownProps) { const [timeLeft, setTimeLeft] = useState({ days: 0, hours: 0, minutes: 0, seconds: 0 }); const [mounted, setMounted] = useState(false); useEffect(() => { setMounted(true); }, []); useEffect(() => { if (!mounted) return; const calculateTimeLeft = (): TimeLeft => { const now = new Date(); let targetDate: Date; if (saleEndDate) { // Use the sale end date from admin try { const saleDate = new Date(saleEndDate); // Always set to end of day for proper countdown display targetDate = new Date(saleDate.getFullYear(), saleDate.getMonth(), saleDate.getDate(), 23, 59, 59, 999); } catch (error) { console.error('Error parsing sale end date:', error); // Fallback to next Sunday if date parsing fails const nextSunday = new Date(); const daysUntilSunday = (7 - now.getDay()) % 7; const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday; nextSunday.setDate(now.getDate() + targetDay); nextSunday.setHours(23, 59, 59, 999); targetDate = nextSunday; } } else { // Fallback to next Sunday at 23:59:59 const nextSunday = new Date(); const daysUntilSunday = (7 - now.getDay()) % 7; const targetDay = daysUntilSunday === 0 ? 7 : daysUntilSunday; nextSunday.setDate(now.getDate() + targetDay); nextSunday.setHours(23, 59, 59, 999); targetDate = nextSunday; } const difference = targetDate.getTime() - now.getTime(); if (difference > 0) { return { days: Math.floor(difference / (1000 * 60 * 60 * 24)), hours: Math.floor((difference / (1000 * 60 * 60)) % 24), minutes: Math.floor((difference / 1000 / 60) % 60), seconds: Math.floor((difference / 1000) % 60) }; } return { days: 0, hours: 0, minutes: 0, seconds: 0 }; }; const timer = setInterval(() => { setTimeLeft(calculateTimeLeft()); }, 1000); // Initial calculation setTimeLeft(calculateTimeLeft()); return () => clearInterval(timer); }, [mounted]); if (!mounted) { return
; // Placeholder to prevent hydration mismatch } // Only show countdown if there are active sales if (!hasActiveSale) return null; const isActive = timeLeft.days > 0 || timeLeft.hours > 0 || timeLeft.minutes > 0 || timeLeft.seconds > 0; if (!isActive) return null; return (

AKCIJA SE ZAVRŠAVA!

Popust od {maxSalePercentage}% važi još:

{timeLeft.days}
dana
{timeLeft.hours.toString().padStart(2, '0')}
sati
{timeLeft.minutes.toString().padStart(2, '0')}
min
{timeLeft.seconds.toString().padStart(2, '0')}
sek
); }