<?php
/**
 * News区块模板
 * 支持自定义器设置和短代码调用
 */

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

// 检查是否通过短代码调用
global $news_shortcode_atts;

// 确保 $news_shortcode_atts 是数组
if (!is_array($news_shortcode_atts)) {
    $news_shortcode_atts = array();
}

// 获取自定义器设置，如果有短代码参数则优先使用短代码参数
$news_title = !empty($news_shortcode_atts['title']) ? $news_shortcode_atts['title'] : get_theme_mod('news_title', 'NEWS');
$news_button_text = get_theme_mod('news_button_text', 'Learn More');
$news_button_url = get_theme_mod('news_button_url', '/news');
$news_category_id = !empty($news_shortcode_atts['category_id']) ? $news_shortcode_atts['category_id'] : get_theme_mod('news_category_id', '');
$news_order_by = !empty($news_shortcode_atts['order_by']) ? $news_shortcode_atts['order_by'] : get_theme_mod('news_order_by', 'date');
$news_order = !empty($news_shortcode_atts['order']) ? $news_shortcode_atts['order'] : get_theme_mod('news_order', 'DESC');
$news_posts_count = !empty($news_shortcode_atts['posts_count']) ? intval($news_shortcode_atts['posts_count']) : get_theme_mod('news_posts_count', 3);

// 设置容器ID和CSS类
$container_id = !empty($news_shortcode_atts['id']) ? $news_shortcode_atts['id'] : 'nenghui-news-block';
$container_class = !empty($news_shortcode_atts['class']) ? $news_shortcode_atts['class'] : '';

// 构建查询参数
$query_args = array(
    'post_type' => 'post',
    'posts_per_page' => $news_posts_count,
    'orderby' => $news_order_by,
    'order' => $news_order,
    'post_status' => 'publish',
    'meta_query' => array(
        array(
            'key' => '_thumbnail_id',
            'compare' => 'EXISTS'
        )
    )
);

// 如果指定了分类ID，添加分类过滤
if (!empty($news_category_id)) {
    $query_args['cat'] = $news_category_id;
}

// 查询文章
$news_query = new WP_Query($query_args);

// 获取随机特色图像和对应的文章链接
$featured_image_url = '';
$featured_image_link = '';
if ($news_query->have_posts()) {
    // 获取所有文章的特色图像和链接
    $featured_data = array();
    while ($news_query->have_posts()) {
        $news_query->the_post();
        if (has_post_thumbnail()) {
            $featured_data[] = array(
                'image' => get_the_post_thumbnail_url(get_the_ID(), 'full'),
                'link' => get_permalink()
            );
        }
    }
    // 随机选择一张图片和对应链接
    if (!empty($featured_data)) {
        $random_item = $featured_data[array_rand($featured_data)];
        $featured_image_url = $random_item['image'];
        $featured_image_link = $random_item['link'];
    }
    // 重置查询
    wp_reset_postdata();
}

// 如果没有找到特色图像，使用默认图片
if (empty($featured_image_url)) {
    $featured_image_url = 'http://nh.matepress.cn/wp-content/uploads/2025/06/logo.svg';
}
?>

<style>
    .image-section {
        flex: 1;
    }

    .image-link {
        display: block;
        text-decoration: none;
        transition: transform 0.3s ease;
    }

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

    .main-image {
        width: 100%;
        height: auto;
        border-radius: 15px;
        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        transition: box-shadow 0.3s ease;
    }

    .image-link:hover .main-image {
        box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
        filter: brightness(1.1);
        transition: all 0.3s ease;
    }
</style>

<div id="<?php echo esc_attr($container_id); ?>" class="news-container <?php echo esc_attr($container_class); ?>" data-aos="fade-up">
    <div class="news-header" data-aos="fade-up" data-aos-delay="200">
        <h1 class="news-title"><?php echo esc_html($news_title); ?></h1>
        <a href="<?php echo esc_url($news_button_url); ?>" class="learn-more-btn"><?php echo esc_html($news_button_text); ?></a>
    </div>

    <!-- 主要内容区域 -->
    <div class="news-content" data-aos="fade-up" data-aos-delay="400">
        <!-- 左侧图片区域 -->
        <div class="image-section" data-aos="fade-up" data-aos-delay="600">
            <?php if (!empty($featured_image_link)) : ?>
            <a href="<?php echo esc_url($featured_image_link); ?>" class="image-link">
                <img src="<?php echo esc_url($featured_image_url); ?>" alt="<?php echo esc_attr($news_title); ?>"
                    class="main-image">
            </a>
            <?php else : ?>
            <img src="<?php echo esc_url($featured_image_url); ?>" alt="<?php echo esc_attr($news_title); ?>"
                class="main-image">
            <?php endif; ?>
        </div>

        <!-- 右侧新闻列表 -->
        <div class="news-list" data-aos="fade-up" data-aos-delay="800">
            <?php
            // 重新执行查询显示文章列表
            $news_query = new WP_Query($query_args);
            if ($news_query->have_posts()) :
                while ($news_query->have_posts()) : $news_query->the_post();
                    // 获取文章分类
                    $categories = get_the_category();
                    $category_name = !empty($categories) ? $categories[0]->name : 'News';
            ?>
            <div class="news-item">
                <a href="<?php the_permalink(); ?>" class="news-item-link">
                    <div class="news-item-title"><?php the_title(); ?></div>
                    <div class="news-meta">
                        <span class="review-tag"><?php echo esc_html($category_name); ?></span>
                        <span class="news-date"><?php echo date('M j, Y', get_the_time('U')); ?></span>
                    </div>
                </a>
            </div>
            <?php
                endwhile;
                wp_reset_postdata();
            else :
            ?>
            <div class="news-item">
                <div class="news-item-title">暂无文章</div>
                <div class="news-meta">
                    <span class="review-tag">提示</span>
                    <span class="news-date"><?php echo date('M j, Y'); ?></span>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>