<?php
/**
 * 资源加载配置文件
 * 管理前端脚本和样式的加载，包含jQuery管理和性能优化
 */

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

/**
 * 前端脚本和样式加载
 * 正确管理jQuery版本和主题资源文件的加载顺序
 */
function nenghui_enqueue_scripts() {
    // 确保不在管理后台执行jQuery替换
    if (is_admin()) {
        return;
    }
    
    // 获取文件路径和版本号
    $jquery_file = get_template_directory() . '/assets/js/jquery.min.js';
    $main_js_file = get_template_directory() . '/assets/js/index.js';
    $main_css_file = get_template_directory() . '/assets/css/index.css';
    
    // 检查文件是否存在
    if (!file_exists($jquery_file) || !file_exists($main_js_file) || !file_exists($main_css_file)) {
        return;
    }
    
    $jquery_version = filemtime($jquery_file);
    $main_js_version = filemtime($main_js_file);
    $main_css_version = filemtime($main_css_file);
    
    // 安全地移除和重新注册jQuery
    if (wp_script_is('jquery', 'registered')) {
        wp_deregister_script('jquery');
    }
    
    // 注册并加载jQuery（本地化）
    wp_register_script('jquery', get_template_directory_uri() . '/assets/js/jquery.min.js', array(), $jquery_version, true);
    wp_enqueue_script('jquery');
    
    // 添加jQuery fallback机制
    wp_add_inline_script('jquery', 'window.jQuery || document.write(\'<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"><\/script>\');');
        
    // 加载主题自定义JS（Banner轮播现在使用原生JavaScript实现）
    wp_enqueue_script('nenghui-main-js', get_template_directory_uri() . '/assets/js/index.js', array('jquery'), $main_js_version, true);
    
    // 按正确的依赖顺序加载样式文件，避免CSS阻塞渲染
    // 只加载必要的CSS文件
    wp_enqueue_style('nenghui-main-css', get_template_directory_uri() . '/assets/css/index.css', array(), $main_css_version);
    
    // 添加滚动动画样式和平滑滚动
    $scroll_animation_css = '
    /* 平滑滚动 */
    html {
        scroll-behavior: smooth;
    }
    
    /* 滚动动画基础样式 */
    .fade-in-block {
        opacity: 0;
        transform: translateY(20px);
        transition: all 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    }
    
    .fade-in-block.animate {
        opacity: 1;
        transform: translateY(0);
    }
    
    /* 为所有区块添加动画类 */
    .about-company-section,
    .banner-section,
    .cases-section,
    .certification-section,
    .contact-map-section,
    .development-history-section,
    .download-center-section,
    .ess-scenarios-section,
    .faq-section,
    .futures-section,
    .home-news-section,
    .map-section,
    .news-section,
    .overseas-services-section,
    .tabs-section,
    .technical-service-section,
    .warehousing-distribution-section,
    .about-info-section,
    .products-banner-section,
    .about-nav-section,
    .black-about-company-banner {
        opacity: 0;
        transform: translateY(20px);
        transition: all 0.8s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    }
    
    .about-company-section.animate,
    .banner-section.animate,
    .cases-section.animate,
    .certification-section.animate,
    .contact-map-section.animate,
    .development-history-section.animate,
    .download-center-section.animate,
    .ess-scenarios-section.animate,
    .faq-section.animate,
    .futures-section.animate,
    .home-news-section.animate,
    .map-section.animate,
    .news-section.animate,
    .overseas-services-section.animate,
    .tabs-section.animate,
    .technical-service-section.animate,
    .warehousing-distribution-section.animate,
    .about-info-section.animate,
    .products-banner-section.animate,
    .about-nav-section.animate,
    .black-about-company-banner.animate {
        opacity: 1;
        transform: translateY(0);
    }
    
    /* 延迟动画效果 */
    .fade-in-block:nth-child(1) { transition-delay: 0.1s; }
    .fade-in-block:nth-child(2) { transition-delay: 0.2s; }
    .fade-in-block:nth-child(3) { transition-delay: 0.3s; }
    .fade-in-block:nth-child(4) { transition-delay: 0.4s; }
    .fade-in-block:nth-child(5) { transition-delay: 0.5s; }
    
    /* 响应式优化 */
    @media (prefers-reduced-motion: reduce) {
        html { scroll-behavior: auto; }
        .fade-in-block,
        .about-company-section,
        .banner-section,
        .cases-section,
        .certification-section,
        .contact-map-section,
        .development-history-section,
        .download-center-section,
        .ess-scenarios-section,
        .faq-section,
        .futures-section,
        .home-news-section,
        .map-section,
        .news-section,
        .overseas-services-section,
        .tabs-section,
        .technical-service-section,
        .warehousing-distribution-section,
        .about-info-section,
        .products-banner-section,
        .about-nav-section,
        .black-about-company-banner {
            opacity: 1;
            transform: none;
            transition: none;
        }
    }
    ';
    wp_add_inline_style('nenghui-main-css', $scroll_animation_css);
    
    // 添加滚动动画JavaScript
    $scroll_animation_js = '
    document.addEventListener("DOMContentLoaded", function() {
        // 创建Intersection Observer
        const observerOptions = {
            root: null,
            rootMargin: "0px 0px -100px 0px",
            threshold: 0.1
        };
        
        const observer = new IntersectionObserver(function(entries) {
            entries.forEach(function(entry) {
                if (entry.isIntersecting) {
                    entry.target.classList.add("animate");
                    // 一旦动画触发，就停止观察该元素
                    observer.unobserve(entry.target);
                }
            });
        }, observerOptions);
        
        // 选择所有需要动画的区块
        const animatedBlocks = document.querySelectorAll(
            ".about-company-section, .banner-section, " +
            ".cases-section, .certification-section, .contact-map-section, " +
            ".development-history-section, .download-center-section, .ess-scenarios-section, " +
            ".faq-section, .futures-section, .home-news-section, " +
            ".map-section, .news-section, .overseas-services-section, " +
            ".tabs-section, .technical-service-section, .warehousing-distribution-section, " +
            ".about-info-section, .products-banner-section, .about-nav-section, " +
            ".black-about-company-banner, .fade-in-block"
        );
        
        // 开始观察所有区块
        animatedBlocks.forEach(function(block) {
            observer.observe(block);
        });
        
        // 优化滚动性能
        let ticking = false;
        function updateScrollPosition() {
            // 这里可以添加额外的滚动优化逻辑
            ticking = false;
        }
        
        window.addEventListener("scroll", function() {
            if (!ticking) {
                requestAnimationFrame(updateScrollPosition);
                ticking = true;
            }
        }, { passive: true });
    });
    ';
    wp_add_inline_script('nenghui-main-js', $scroll_animation_js);
    
    // 只在文章详情页和案例详情页加载post.css
    if (is_single() || is_singular('cases')) {
        $post_css_file = get_template_directory() . '/assets/css/post.css';
        if (file_exists($post_css_file)) {
            $post_css_version = filemtime($post_css_file);
            wp_enqueue_style('nenghui-post-css', get_template_directory_uri() . '/assets/css/post.css', array('nenghui-main-css'), $post_css_version);
        }
    }
    
    // 加载black-about-company-banner样式
    $black_about_banner_css_file = get_template_directory() . '/assets/css/black-about-company-banner.css';
    if (file_exists($black_about_banner_css_file)) {
        $black_about_banner_css_version = filemtime($black_about_banner_css_file);
        wp_enqueue_style('black-about-company-banner-style', get_template_directory_uri() . '/assets/css/black-about-company-banner.css', array('nenghui-main-css'), $black_about_banner_css_version);
    }
    
    // 加载black-about-company-banner轮播JavaScript
    $black_about_banner_js_file = get_template_directory() . '/assets/js/black-about-company-banner.js';
    if (file_exists($black_about_banner_js_file)) {
        $black_about_banner_js_version = filemtime($black_about_banner_js_file);
        wp_enqueue_script('black-about-company-banner-js', get_template_directory_uri() . '/assets/js/black-about-company-banner.js', array('jquery'), $black_about_banner_js_version, true);
    }

}
add_action('wp_enqueue_scripts', 'nenghui_enqueue_scripts');

?>