<?php
/**
 * 主题简码功能
 */

// 视频Banner区块简码
function nenghui_video_banner_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
        'bg_image' => '',
        'height' => '800',
        'video_bg_enabled' => 'true',
        'video_bg_url' => '',
        'video_bg_opacity' => '1.0',
        'video_bg_lazy_load' => 'false'
    ), $atts, 'video_banner');

    // 转换字符串布尔值
    $video_bg_enabled = filter_var($atts['video_bg_enabled'], FILTER_VALIDATE_BOOLEAN);
    $video_bg_lazy_load = filter_var($atts['video_bg_lazy_load'], FILTER_VALIDATE_BOOLEAN);

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'video-banner-' . uniqid(),
        'className' => $atts['class']
    );

    // 如果没有提供视频地址，则从customizer获取
    $video_bg_url = !empty($atts['video_bg_url']) ? $atts['video_bg_url'] : get_video_banner_background_url();
    $bg_image = !empty($atts['bg_image']) ? $atts['bg_image'] : get_video_banner_bg_image();

    // 设置自定义器值（模拟）
    $customizer_values = array(
        'video_banner_bg_image' => $bg_image,
        'video_banner_height' => intval($atts['height']),
        'video_banner_video_bg_enabled' => $video_bg_enabled,
        'video_banner_video_bg_url' => $video_bg_url,
        'video_banner_video_bg_opacity' => floatval($atts['video_bg_opacity']),
        'video_banner_video_bg_lazy_load' => $video_bg_lazy_load,
        'video_banner_block_id' => $block['id'],
        'video_banner_block_class' => $block['className']
    );

    // 临时设置全局变量以供模板使用
    global $video_banner_shortcode_values;
    $video_banner_shortcode_values = $customizer_values;

    // 确保样式和脚本已加载
    if (!wp_style_is('video-banner-css', 'enqueued')) {
        wp_enqueue_style('video-banner-css', get_template_directory_uri() . '/assets/css/video-banner.css', array(), '1.0.0');
    }

    if (!wp_script_is('video-banner-js', 'enqueued')) {
        // 检查GSAP是否已加载
        if (!wp_script_is('gsap', 'enqueued')) {
            wp_enqueue_script('gsap', get_template_directory_uri() . '/assets/js/gsap.min.js', array(), '3.12.2', true);
        }
        
        // 检查video-banner.js文件是否存在
        $video_banner_js_path = get_template_directory() . '/assets/js/video-banner.js';
        if (file_exists($video_banner_js_path)) {
            wp_enqueue_script('video-banner-js', get_template_directory_uri() . '/assets/js/video-banner.js', array('jquery', 'gsap'), '1.0.0', true);
        } else {
            error_log('Video banner JS file not found: ' . $video_banner_js_path);
        }
    }

    ob_start();
    include get_template_directory() . '/template-parts/block/block-video-banner.php';
    $output = ob_get_clean();

    // 清理全局变量
    unset($video_banner_shortcode_values);

    return $output;
}
add_shortcode('video_banner', 'nenghui_video_banner_shortcode');

// 产品服务区块简码
function nenghui_products_services_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
    ), $atts, 'products_services');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'products-services-' . uniqid(),
        'className' => $atts['class']
    );

    ob_start();
    include get_template_directory() . '/template-parts/block/block-products-services.php';
    return ob_get_clean();
}
add_shortcode('products_services', 'nenghui_products_services_shortcode');

// 特性展示区块简码
function nenghui_features_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
    ), $atts, 'features');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'features-' . uniqid(),
        'className' => $atts['class']
    );

    ob_start();
    include get_template_directory() . '/template-parts/block/block-features.php';
    return ob_get_clean();
}
add_shortcode('features', 'nenghui_features_shortcode');

// 解决方案区块简码
function nenghui_solution_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
    ), $atts, 'solution');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'solution-' . uniqid(),
        'className' => $atts['class']
    );

    ob_start();
    include get_template_directory() . '/template-parts/block/block-solution.php';
    return ob_get_clean();
}
add_shortcode('solution', 'nenghui_solution_shortcode');

// 页脚图片区块简码
function nenghui_footer_img_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
    ), $atts, 'footer_img');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'footer-img-' . uniqid(),
        'className' => $atts['class']
    );

    ob_start();
    include get_template_directory() . '/template-parts/block/block-footer-img.php';
    return ob_get_clean();
}
add_shortcode('footer_img', 'nenghui_footer_img_shortcode');

// 新闻区块简码
function nenghui_news_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'category' => '',
        'posts_count' => 4,
        'title' => '新闻资讯',
        'subtitle' => 'NEWS',
        'class' => '',
        'id' => '',
    ), $atts, 'news');

    // 准备传递给模板的参数
    $args = array(
        'category' => $atts['category'],
        'posts_count' => intval($atts['posts_count']),
        'title' => $atts['title'],
        'subtitle' => $atts['subtitle']
    );

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'news-' . uniqid(),
        'className' => $atts['class']
    );

    // 开始输出缓冲
    ob_start();
    
    // 确保变量在模板中可用
    extract($args);
    
    // 包含模板文件
    include get_template_directory() . '/template-parts/block/block-news.php';
    
    // 获取输出内容
    $output = ob_get_clean();
    
    // 如果输出为空，返回调试信息
    if (empty(trim($output))) {
        $debug_info = array(
            '分类参数' => $atts['category'],
            '文章数量' => $atts['posts_count'],
            '标题' => $atts['title']
        );
        
        $output = '<div class="news-debug">';
        $output .= '<h4>简码调试信息</h4>';
        $output .= '<p>模板输出为空，可能的原因：</p>';
        $output .= '<ul>';
        $output .= '<li>指定的分类中没有文章</li>';
        $output .= '<li>所有文章都没有特色图片</li>';
        $output .= '<li>分类ID或别名不正确</li>';
        $output .= '</ul>';
        $output .= '<p><strong>当前参数：</strong></p>';
        $output .= '<pre>' . print_r($debug_info, true) . '</pre>';
        $output .= '</div>';
    }
    
    return $output;
}
add_shortcode('news', 'nenghui_news_shortcode');

// 社会责任轮播简码
function nenghui_social_activities_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'category_id' => 8,
        'posts_count' => 6,
        'title' => '社会责任',
        'slides_per_view' => 3,
        'class' => '',
        'id' => '',
    ), $atts, 'social_activities');

    // 准备传递给模板的参数
    $args = array(
        'category_id' => intval($atts['category_id']),
        'posts_count' => intval($atts['posts_count']),
        'title' => $atts['title'],
        'slides_per_view' => intval($atts['slides_per_view'])
    );

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'social-activities-' . uniqid(),
        'className' => $atts['class']
    );

    // 开始输出缓冲
    ob_start();
    
    // 确保变量在模板中可用
    extract($args);
    
    // 包含模板文件
    include get_template_directory() . '/template-parts/block/black-social-activities.php';
    
    // 获取输出内容
    $output = ob_get_clean();
    
    // 动态加载社会活动轮播相关资源 - 使用 Swiper
    $theme_url = get_template_directory_uri();
    $theme_path = get_template_directory();
    
    // 加载 Swiper CSS
    if (!wp_style_is('swiper-bundle-css', 'enqueued')) {
        $swiper_css_file = '/assets/css/swiper-bundle.min.css';
        if (file_exists($theme_path . $swiper_css_file)) {
            wp_enqueue_style('swiper-bundle-css', $theme_url . $swiper_css_file, array(), '8.4.5');
        } else {
            error_log('Missing Swiper CSS file: ' . $theme_path . $swiper_css_file);
        }
    }
    
    // 加载社会活动样式
    if (!wp_style_is('social-activities-css', 'enqueued')) {
        $css_file = '/assets/css/social-activities.css';
        if (file_exists($theme_path . $css_file)) {
            wp_enqueue_style('social-activities-css', $theme_url . $css_file, array('swiper-bundle-css'), '1.0.0');
        } else {
            error_log('Missing CSS file: ' . $theme_path . $css_file);
        }
    }
    
    // 加载 Swiper JavaScript
    if (!wp_script_is('swiper-bundle-js', 'enqueued')) {
        $swiper_js_file = '/assets/js/swiper-bundle.min.js';
        if (file_exists($theme_path . $swiper_js_file)) {
            wp_enqueue_script('swiper-bundle-js', $theme_url . $swiper_js_file, array(), '8.4.5', true);
        } else {
            error_log('Missing Swiper JS file: ' . $theme_path . $swiper_js_file);
        }
    }
    
    // 加载社会活动 Swiper 初始化脚本
    if (!wp_script_is('social-activities-swiper-js', 'enqueued')) {
        $js_file = '/assets/js/social-activities-swiper.js';
        if (file_exists($theme_path . $js_file)) {
            wp_enqueue_script('social-activities-swiper-js', $theme_url . $js_file, array('jquery', 'swiper-bundle-js'), '1.0.0', true);
        } else {
            error_log('Missing JS file: ' . $theme_path . $js_file);
        }
    }
    
    // 如果输出为空，返回调试信息
    if (empty(trim($output))) {
        $debug_info = array(
            '分类ID' => $atts['category_id'],
            '文章数量' => $atts['posts_count'],
            '标题' => $atts['title'],
            '每页显示' => $atts['slides_per_view']
        );
        
        $output = '<div class="social-activities-debug">';
        $output .= '<h4>社会活动轮播简码调试信息</h4>';
        $output .= '<p>模板输出为空，可能的原因：</p>';
        $output .= '<ul>';
        $output .= '<li>指定的分类中没有文章</li>';
        $output .= '<li>所有文章都没有特色图片</li>';
        $output .= '<li>分类ID不正确</li>';
        $output .= '</ul>';
        $output .= '<p><strong>当前参数：</strong></p>';
        $output .= '<pre>' . print_r($debug_info, true) . '</pre>';
        $output .= '</div>';
    }
    
    return $output;
}
add_shortcode('social_activities', 'nenghui_social_activities_shortcode');

// 投资者关系区块简码
function nenghui_investor_relations_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'title' => '投资者关系',
        'class' => '',
        'id' => '',
    ), $atts, 'investor_relations');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'investor-relations-' . uniqid(),
        'className' => $atts['class']
    );

    // 准备传递给模板的参数
    $args = array(
        'title' => $atts['title']
    );

    // 开始输出缓冲
    ob_start();
    
    // 确保变量在模板中可用
    extract($args);
    
    // 包含模板文件
    include get_template_directory() . '/template-parts/block/black-investor-relations.php';
    
    // 获取输出内容
    $output = ob_get_clean();
    
    return $output;
}
add_shortcode('investor_relations', 'nenghui_investor_relations_shortcode');

// 导航菜单简码
function nenghui_navigation_menu_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'menu_id'     => 'primary',
        'menu_class'  => '',
        'theme'       => 'default',
        'show_border' => 'true',
        'align'       => 'left',
        'class'       => '',
        'id'          => '',
    ), $atts, 'navigation_menu');

    // 转换字符串布尔值
    $show_border = filter_var($atts['show_border'], FILTER_VALIDATE_BOOLEAN);

    // 准备传递给模板的参数
    $args = array(
        'menu_id'     => $atts['menu_id'],
        'menu_class'  => $atts['menu_class'],
        'theme'       => $atts['theme'],
        'show_border' => $show_border,
        'align'       => $atts['align']
    );

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'navigation-menu-' . uniqid(),
        'className' => $atts['class']
    );

    // 开始输出缓冲
    ob_start();
    
    // 包含模板文件
    include get_template_directory() . '/template-parts/block/block-navigation-menu.php';
    
    // 动态加载navigation-menu相关资源
    if (!wp_style_is('navigation-menu-css', 'enqueued')) {
        $theme_url = get_template_directory_uri();
        $theme_path = get_template_directory();
        $css_file = '/assets/css/navigation-menu.css';
        
        if (file_exists($theme_path . $css_file)) {
            wp_enqueue_style('navigation-menu-css', $theme_url . $css_file, array(), '1.0.0');
        } else {
            // 如果CSS文件不存在，记录日志但不阻止功能
            error_log('Navigation menu CSS file not found: ' . $theme_path . $css_file);
        }
    }
    
    if (!wp_script_is('navigation-menu-js', 'enqueued')) {
        $theme_url = get_template_directory_uri();
        $theme_path = get_template_directory();
        $js_file = '/assets/js/navigation-menu.js';
        
        if (file_exists($theme_path . $js_file)) {
            wp_enqueue_script('navigation-menu-js', $theme_url . $js_file, array('jquery'), '1.0.0', true);
        } else {
            // 如果JS文件不存在，记录日志但不阻止功能
            error_log('Navigation menu JS file not found: ' . $theme_path . $js_file);
        }
    }
    
    // 获取输出内容
    $output = ob_get_clean();
    
    return $output;
}
add_shortcode('navigation_menu', 'nenghui_navigation_menu_shortcode');

// 业务流程区块简码
function nenghui_business_process_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
        'title' => '',
        'subtitle' => '',
        'tab_01_title' => '',
        'tab_01_description' => '',
        'tab_01_image' => '',
        'tab_02_title' => '',
        'tab_02_description' => '',
        'tab_02_image' => '',
        'tab_03_title' => '',
        'tab_03_description' => '',
        'tab_03_image' => '',
        'tab_04_title' => '',
        'tab_04_description' => '',
        'tab_04_image' => '',
    ), $atts, 'business_process');

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'business-process-' . uniqid(),
        'className' => $atts['class']
    );

    // 设置自定义器值（模拟）
    $customizer_values = array(
        'business_process_show' => true,
        'business_process_title' => !empty($atts['title']) ? $atts['title'] : get_theme_mod('business_process_title', '设计业务流程'),
        'business_process_subtitle' => !empty($atts['subtitle']) ? $atts['subtitle'] : get_theme_mod('business_process_subtitle', 'DESIGN BUSINESS PROCESS'),
        'business_process_tab_01_title' => !empty($atts['tab_01_title']) ? $atts['tab_01_title'] : get_theme_mod('business_process_tab1_title', '可行性研究'),
        'business_process_tab_01_description' => !empty($atts['tab_01_description']) ? $atts['tab_01_description'] : get_theme_mod('business_process_tab1_description', '前期规划及可行性分析'),
        'business_process_tab_01_image' => !empty($atts['tab_01_image']) ? $atts['tab_01_image'] : get_theme_mod('business_process_tab1_image', get_template_directory_uri() . '/assets/images/FWas.jpg'),
        'business_process_tab_02_title' => !empty($atts['tab_02_title']) ? $atts['tab_02_title'] : get_theme_mod('business_process_tab2_title', '初步设计'),
        'business_process_tab_02_description' => !empty($atts['tab_02_description']) ? $atts['tab_02_description'] : get_theme_mod('business_process_tab2_description', '方案设计及技术论证'),
        'business_process_tab_02_image' => !empty($atts['tab_02_image']) ? $atts['tab_02_image'] : get_theme_mod('business_process_tab2_image', get_template_directory_uri() . '/assets/images/FWas.jpg'),
        'business_process_tab_03_title' => !empty($atts['tab_03_title']) ? $atts['tab_03_title'] : get_theme_mod('business_process_tab3_title', '施工图设计'),
        'business_process_tab_03_description' => !empty($atts['tab_03_description']) ? $atts['tab_03_description'] : get_theme_mod('business_process_tab3_description', '详细设计及施工图纸'),
        'business_process_tab_03_image' => !empty($atts['tab_03_image']) ? $atts['tab_03_image'] : get_theme_mod('business_process_tab3_image', get_template_directory_uri() . '/assets/images/FWas.jpg'),
        'business_process_tab_04_title' => !empty($atts['tab_04_title']) ? $atts['tab_04_title'] : get_theme_mod('business_process_tab4_title', '竣工图设计'),
        'business_process_tab_04_description' => !empty($atts['tab_04_description']) ? $atts['tab_04_description'] : get_theme_mod('business_process_tab4_description', '竣工验收及图纸归档'),
        'business_process_tab_04_image' => !empty($atts['tab_04_image']) ? $atts['tab_04_image'] : get_theme_mod('business_process_tab4_image', get_template_directory_uri() . '/assets/images/FWas.jpg'),
    );

    // 临时设置全局变量以供模板使用
    global $business_process_shortcode_values;
    $business_process_shortcode_values = $customizer_values;

    // 确保样式和脚本已加载
    if (!wp_style_is('business-process-css', 'enqueued')) {
        wp_enqueue_style('business-process-css', get_template_directory_uri() . '/assets/css/business-process.css', array(), '1.0.0');
    }

    if (!wp_script_is('business-process-js', 'enqueued')) {
        wp_enqueue_script('business-process-js', get_template_directory_uri() . '/assets/js/business-process.js', array('jquery'), '1.0.0', true);
    }

    ob_start();
    include get_template_directory() . '/template-parts/block/block-process.php';
    $output = ob_get_clean();

    // 清理全局变量
    unset($business_process_shortcode_values);

    return $output;
}
add_shortcode('business_process', 'nenghui_business_process_shortcode');

// 户用轮播图区块简码
function nenghui_household_carousel_shortcode($atts) {
    // 设置默认属性
    $atts = shortcode_atts(array(
        'class' => '',
        'id' => '',
        'title' => '户用解决方案',
        'subtitle' => 'HOUSEHOLD SOLUTIONS',
        'show' => 'true',
        'autoplay' => 'true',
        'autoplay_delay' => '5000'
    ), $atts, 'household_carousel');

    // 转换字符串布尔值
    $show_household = filter_var($atts['show'], FILTER_VALIDATE_BOOLEAN);
    $autoplay = filter_var($atts['autoplay'], FILTER_VALIDATE_BOOLEAN);

    // 如果不显示该区块，直接返回
    if (!$show_household) {
        return '';
    }

    // 模拟区块数据
    $block = array(
        'id' => !empty($atts['id']) ? $atts['id'] : 'household-carousel-' . uniqid(),
        'className' => $atts['class']
    );

    // 设置自定义器值（模拟）
    $customizer_values = array(
        'household_title' => $atts['title'],
        'household_subtitle' => $atts['subtitle'],
        'household_show' => $show_household,
        'household_autoplay' => $autoplay,
        'household_autoplay_delay' => intval($atts['autoplay_delay']),
        'household_block_id' => $block['id'],
        'household_block_class' => $block['className']
    );

    // 临时设置全局变量以供模板使用
    global $household_shortcode_values;
    $household_shortcode_values = $customizer_values;

    // 确保样式和脚本已加载
    if (!wp_style_is('household-carousel-css', 'enqueued')) {
        wp_enqueue_style('household-carousel-css', get_template_directory_uri() . '/assets/css/household-carousel.css', array(), '1.0.0');
    }

    if (!wp_script_is('household-carousel-js', 'enqueued')) {
        wp_enqueue_script('household-carousel-js', get_template_directory_uri() . '/assets/js/household-carousel.js', array(), '1.0.0', true);
    }

    ob_start();
    include get_template_directory() . '/template-parts/block/block-household.php';
    $output = ob_get_clean();

    // 清理全局变量
    unset($household_shortcode_values);

    return $output;
}
add_shortcode('household_carousel', 'nenghui_household_carousel_shortcode');

?>