<?php
/**
 * 产品Banner卡片轮播区块 - 基于Swiper
 * 显示产品卡片轮播，支持自定义器配置
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}

// 获取短代码参数（如果通过短代码调用）
global $products_banner_shortcode_atts;
$shortcode_atts = isset($products_banner_shortcode_atts) ? $products_banner_shortcode_atts : array();

// 设置默认参数
$block_id = !empty($shortcode_atts['id']) ? $shortcode_atts['id'] : 'nenghui-products-banner-' . uniqid();
$block_class = !empty($shortcode_atts['class']) ? $shortcode_atts['class'] : '';
$show_animation = !empty($shortcode_atts['show_animation']) ? $shortcode_atts['show_animation'] === 'true' : true;
$autoplay = !empty($shortcode_atts['autoplay']) ? $shortcode_atts['autoplay'] === 'true' : get_theme_mod('products_banner_autoplay', true);
$autoplay_delay = !empty($shortcode_atts['autoplay_delay']) ? intval($shortcode_atts['autoplay_delay']) : get_theme_mod('products_banner_autoplay_delay', 5000);

// 获取产品数量
$products_count = get_theme_mod('products_banner_count', 6);

// 收集有效的产品数据
$products = array();
for ($i = 1; $i <= 7; $i++) {
    $image = get_theme_mod('products_banner_image_' . $i, '');
    $title = get_theme_mod('products_banner_title_' . $i, '');
    $description = get_theme_mod('products_banner_description_' . $i, '');
    $link = get_theme_mod('products_banner_link_' . $i, '#');
    
    // 只有设置了图片和标题的产品才显示
    if (!empty($image) && !empty($title)) {
        $products[] = array(
            'image' => $image,
            'title' => $title,
            'description' => $description,
            'link' => $link
        );
    }
    
    // 限制显示数量
    if (count($products) >= $products_count) {
        break;
    }
}

// 如果没有产品数据，不显示区块
if (empty($products)) {
    return;
}
?>

<!-- Swiper CSS -->
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/assets/css/swiper-bundle.min.css" />

<section id="<?php echo esc_attr($block_id); ?>" class="products-banner-section nenghui-products-banner <?php echo esc_attr($block_class); ?>" <?php if ($show_animation): ?>data-aos="fade-up"<?php endif; ?>>
    <div class="products-banner-container">
        <div class="products-banner-wrapper">
            <!-- 产品标题 -->
            <div class="section-header">
                <h2 class="section-title">What We Offer</h2>
            </div>
            
            <!-- Swiper 轮播容器 -->
            <div class="swiper products-swiper" 
                 data-autoplay="<?php echo $autoplay ? 'true' : 'false'; ?>" 
                 data-delay="<?php echo esc_attr($autoplay_delay); ?>">
                <div class="swiper-wrapper">
                    <?php foreach ($products as $index => $product): ?>
                    <div class="swiper-slide">
                        <div class="product-card">
                            <div class="product-content">
                                <h3 class="product-title"><?php echo esc_html($product['title']); ?></h3>
                                <?php if (!empty($product['description'])): ?>
                                <p class="product-description"><?php echo esc_html($product['description']); ?></p>
                                <?php endif; ?>
                                <a href="<?php echo esc_url($product['link']); ?>" class="product-link" aria-label="了解更多关于<?php echo esc_attr($product['title']); ?>">
                                    <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path d="M5 12H19M19 12L12 5M19 12L12 19" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                    </svg>
                                </a>
                            </div>
                            <div class="product-image">
                                <img src="<?php echo esc_url($product['image']); ?>" 
                                     alt="<?php echo esc_attr($product['title']); ?>" 
                                     loading="lazy">
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                
                <!-- 分页器 -->
                <div class="swiper-pagination"></div>
                
                <!-- 导航按钮 -->
                <div class="swiper-button-next"></div>
                <div class="swiper-button-prev"></div>
            </div>
        </div>
    </div>
</section>

<style>
.nenghui-products-banner {
    padding: 80px 0;
    position: relative;
    overflow: hidden;
    background: #f8f9fa;
}

.nenghui-products-banner .products-banner-container {
    width: 100%;
    margin: 0;
    padding: 0;
}

.products-banner-wrapper {
    position: relative;
}

.section-header {
    text-align: center;
    margin-bottom: 60px;
}

.section-title {
    font-size: 2.5rem;
    font-weight: 700;
    color: #333;
    margin: 0;
    line-height: 1.2;
}

/* Swiper 容器样式 */
.products-swiper {
    width: 100%;
    height: 400px;
    padding: 20px 0 60px;
}

.products-swiper .swiper-slide {
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease;
}

/* 产品卡片样式 */
.product-card {
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    transition: all 0.3s ease;
    height: 320px;
    width: 100%;
    max-width: 600px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    position: relative;
}

.product-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 16px 48px rgba(0, 0, 0, 0.15);
    transition: all 0.3s ease;
}

/* 产品内容区域 */
.product-content {
    padding: 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 20px;
    background: transparent;
    color: #333;
}

.product-title {
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1.3;
    margin: 0;
    color: #333;
}

.product-description {
    font-size: 15px;
    line-height: 1.6;
    color: #666;
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.product-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 48px;
    height: 48px;
    background: #eee;
    border-radius: 50%;
    color: #333;
    text-decoration: none;
    transition: all 0.3s ease;
}

.product-link:hover {
    background: #3eb396;
    color: white;
    transform: scale(1.1);
    transition: all 0.3s ease;
}

/* 产品图片区域 */
.product-image {
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fff;
}

.product-image img {
    max-width: 80%;
    max-height: 80%;
    width: auto;
    height: auto;
    object-fit: contain;
    transition: transform 0.3s ease;
}

.product-card:hover .product-image img {
    transform: scale(1.05);
    transition: all 0.3s ease;
}

/* Swiper 导航样式 */
.products-swiper .swiper-button-next,
.products-swiper .swiper-button-prev {
    width: 50px;
    height: 50px;
    margin-top: -25px;
    background: #fff;
    border-radius: 50%;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    color: #333;
    transition: all 0.3s ease;
}

.products-swiper .swiper-button-next:hover,
.products-swiper .swiper-button-prev:hover {
    background: #667eea;
    color: white;
    transform: scale(1.1);
    box-shadow: 0 6px 24px rgba(102, 126, 234, 0.3);
}

.products-swiper .swiper-button-next::after,
.products-swiper .swiper-button-prev::after {
    font-size: 18px;
    font-weight: 600;
}

/* Swiper 分页器样式 */
.products-swiper .swiper-pagination {
    bottom: 20px;
}

.products-swiper .swiper-pagination-bullet {
    width: 50px;
    height: 4px;
    background: #ddd;
    opacity: 1;
    transition: all 0.3s ease;
    border-radius: 0;
    margin: 0 4px;
}

.products-swiper .swiper-pagination-bullet-active {
    background: #3eb396;
    transform: none;
}

/* 中间卡片放大效果 */
.products-swiper .swiper-slide-active .product-card {
    transform: scale(1.1);
    z-index: 2;
}

.products-swiper .swiper-slide-active .product-card:hover {
    transform: scale(1.1) translateY(-8px);
}

/* 侧边卡片缩小效果 */
.products-swiper .swiper-slide-prev .product-card,
.products-swiper .swiper-slide-next .product-card {
    transform: scale(0.9);
    opacity: 0.7;
}

.products-swiper .swiper-slide-prev .product-card:hover,
.products-swiper .swiper-slide-next .product-card:hover {
    transform: scale(0.9) translateY(-4px);
    opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 1024px) {
    .nenghui-products-banner .products-banner-container {
        width: 100%;
        padding: 0;
    }
    
    .section-title {
        font-size: 2rem;
    }
    
    .products-swiper {
        height: 350px;
    }
    
    .product-card {
        height: 280px;
        max-width: 550px;
    }
    
    .product-content {
        padding: 25px;
    }
    
    .product-title {
        font-size: 1.3rem;
    }
    
    .product-description {
        font-size: 0.9rem;
        -webkit-line-clamp: 2;
    }
}

@media (max-width: 768px) {
    .nenghui-products-banner {
        padding: 60px 0;
    }
    
    .section-header {
        margin-bottom: 40px;
    }
    
    .section-title {
        font-size: 1.8rem;
    }
    
    .products-swiper {
        height: auto;
        min-height: 450px;
        padding: 20px 0 60px;
    }
    
    .product-card {
        height: auto;
        min-height: 400px;
        max-width: 90%;
        grid-template-columns: 1fr;
        grid-template-rows: auto auto;
        gap: 0;
    }
    
    .product-content {
        padding: 25px;
        gap: 15px;
        order: 1;
    }
    
    .product-image {
        padding: 20px;
        order: 2;
        height: 200px;
        background: #fff;
    }

    .product-image img {
        max-width: 90%;
        max-height: 180px;
    }
    
    .product-title {
        font-size: 1.25rem;
    }
    
    .product-description {
        font-size: 0.9rem;
        -webkit-line-clamp: 4;
    }
    
    .product-link {
        width: 44px;
        height: 44px;
    }

    /* 隐藏导航按钮 */
    .products-swiper .swiper-button-next,
    .products-swiper .swiper-button-prev {
        display: none !important;
    }
    
    /* 移动端取消缩放效果 */
    .products-swiper .swiper-slide-active .product-card,
    .products-swiper .swiper-slide-prev .product-card,
    .products-swiper .swiper-slide-next .product-card {
        transform: none;
        opacity: 1;
    }
    
    .products-swiper .swiper-slide-active .product-card:hover,
    .products-swiper .swiper-slide-prev .product-card:hover,
    .products-swiper .swiper-slide-next .product-card:hover {
        transform: translateY(-4px);
    }
}

@media (max-width: 480px) {
    .nenghui-products-banner .products-banner-container {
        padding: 0;
    }
    
    .section-title {
        font-size: 1.5rem;
    }
    
    .products-swiper {
        min-height: 420px;
    }
    
    .product-card {
        min-height: 380px;
    }
    
    .product-image {
        height: 180px;
    }

    .product-image img {
        max-height: 160px;
    }
    
    .product-content {
        padding: 20px;
        gap: 12px;
    }
    
    .product-title {
        font-size: 1.15rem;
    }
    
    .product-description {
        font-size: 0.85rem;
    }
}
</style>

<!-- Swiper JS -->
<script src="<?php echo get_template_directory_uri(); ?>/assets/js/swiper-bundle.min.js"></script>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const swiperElement = document.querySelector('.products-swiper');
    if (!swiperElement) return;
    
    const autoplay = swiperElement.dataset.autoplay === 'true';
    const delay = parseInt(swiperElement.dataset.delay) || 5000;
    
    // 初始化 Swiper
    const swiper = new Swiper('.products-swiper', {
        // 基本配置
        slidesPerView: 'auto',
        centeredSlides: true,
        spaceBetween: 30,
        loop: true,
        
        // 自动播放
        autoplay: autoplay ? {
            delay: delay,
            disableOnInteraction: false,
            pauseOnMouseEnter: true
        } : false,
        
        // 响应式断点
        breakpoints: {
            320: {
                slidesPerView: 1,
                spaceBetween: 20,
                centeredSlides: true
            },
            768: {
                slidesPerView: 1.5,
                spaceBetween: 25,
                centeredSlides: true
            },
            1024: {
                slidesPerView: 2.5,
                spaceBetween: 30,
                centeredSlides: true
            },
            1200: {
                slidesPerView: 3,
                spaceBetween: 30,
                centeredSlides: true
            }
        },
        
        // 导航
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        
        // 分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true,
            dynamicBullets: true
        },
        
        // 效果
        effect: 'slide',
        
        // 过渡效果
        speed: 600,
        
        // 触摸设置
        touchRatio: 1,
        touchAngle: 45,
        grabCursor: true,
        
        // 键盘控制
        keyboard: {
            enabled: true,
            onlyInViewport: true
        },
        
        // 鼠标滚轮
        mousewheel: {
            invert: false,
            forceToAxis: false,
            sensitivity: 1,
            releaseOnEdges: false
        },
        
        // 事件回调
        on: {
            init: function() {
                // Swiper initialized
            },
            slideChange: function() {
                // 可以在这里添加自定义的滑动事件处理
            }
        }
    });
    
    // 鼠标悬停暂停自动播放
    if (autoplay) {
        swiperElement.addEventListener('mouseenter', () => {
            swiper.autoplay.stop();
        });
        
        swiperElement.addEventListener('mouseleave', () => {
            swiper.autoplay.start();
        });
    }
});
</script>