import { useState, useEffect, useCallback } from 'react'; import { Play, Info, ChevronLeft, ChevronRight } from 'lucide-react'; import { formatImageUrl } from '../lib/image'; export default function HeroBanner({ movies = [] }) { const [active, setActive] = useState(0); const [progress, setProgress] = useState(0); const [favorites, setFavorites] = useState([]); useEffect(() => { fetch('/api/user/favorite') .then(res => res.json()) .then(data => { if (data && data.favorites) { setFavorites(data.favorites.map(f => f.slug)); } }) .catch(() => {}); }, []); const handleFavoriteClick = async (m) => { try { const res = await fetch('/api/user/favorite', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ movieSlug: m.slug, movieName: m.name, movieThumb: m.thumb_url || m.poster_url }) }); const data = await res.json(); if (res.ok) { if (window.txatoast) { window.txatoast.success(data.message); } if (data.action === 'added') { setFavorites(prev => [...prev, m.slug]); } else { setFavorites(prev => prev.filter(slug => slug !== m.slug)); } } else { if (window.txatoast) { window.txatoast.error(data.error || 'Vui lòng đăng nhập!'); } } } catch (e) { if (window.txatoast) { window.txatoast.error('Không thể thực hiện lúc này!'); } } }; const heroMovies = movies.slice(0, 5); const next = useCallback(() => { setActive((prev) => (prev + 1) % heroMovies.length); setProgress(0); }, [heroMovies.length]); const prev = useCallback(() => { setActive((prev) => (prev - 1 + heroMovies.length) % heroMovies.length); setProgress(0); }, [heroMovies.length]); useEffect(() => { if (heroMovies.length <= 1) return; const interval = 100; // 0.1s const step = 100 / (6000 / interval); // 6s per slide const timer = setInterval(() => { setProgress((p) => { if (p >= 100) { next(); return 0; } return p + step; }); }, interval); return () => clearInterval(timer); }, [heroMovies.length, next]); if (!heroMovies.length) return null; const movie = heroMovies[active]; const bg = formatImageUrl(movie.poster_url || movie.thumb_url); const year = movie.year || (movie.modified?.time ? new Date(movie.modified.time).getFullYear() : ''); return (
{/* Background Slides with Stylized Text */} {heroMovies.map((m, i) => (
{/* Main Background Image */}
{/* Stylized Background Text Layer (The "Thu Hút Mãnh Liệt" style in bg) */}

{m.name}

))} {/* Gradients for cinematic feel */}

{movie.name}

{movie.origin_name && (

{movie.origin_name}

)} {/* Badges Row */}
{movie.tmdb?.vote_average || (8.5 + (movie.slug.length % 15) / 10).toFixed(1)}
{movie.quality || 'Full HD'} {year} {movie.tmdb?.season && ( Phần {movie.tmdb.season} )} {movie.episode_current?.toLowerCase().includes('hoàn tất') ? 'Hoàn tất' : `Tập ${movie.episode_current?.match(/\d+/)?.[0] || '??'}`} {movie.episode_total && movie.episode_total !== '1' && ` / ${movie.episode_total}`}
{/* Categories Row */}
{movie.category?.slice(0, 4).map(cat => ( {cat.name} ))}

{movie.content?.replace(/<[^>]*>/g, '') || movie.description || ''}

{/* Thumbnails Navigation (Right side row) */}
{heroMovies.map((m, i) => (
{ setActive(i); setProgress(0); }} className={`group relative h-16 w-24 cursor-pointer overflow-hidden rounded-xl border-2 transition-all duration-500 ${ i === active ? 'border-primary scale-110 shadow-[0_0_20px_rgba(229,9,20,0.5)]' : 'border-white/10 opacity-50 hover:opacity-100' }`} > nav {i === active && (
)}
))}
{/* Mobile Navigation */}
{heroMovies.map((_, i) => (
{ setActive(i); setProgress(0); }} className={`h-1.5 rounded-full transition-all duration-500 ${i === active ? 'w-8 bg-primary' : 'w-2 bg-white/20'}`} /> ))}
); }