// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 页面加载动画 - 已移除
    // initPageLoader();
    
    // 初始化导航栏滚动效果
    initNavbarScroll();
    
    // 初始化滚动动画
    initScrollAnimation();
    
    // 初始化表单验证
    initFormValidation();
    
    // 初始化数据统计动画
    initStatsCounter();
    
    // 图片懒加载
    initLazyLoading();
    
    // 性能优化
    initPerformanceOptimizations();
});

// 页面加载动画
function initPageLoader() {
    // 创建加载器
    const loader = document.createElement('div');
    loader.className = 'page-loader';
    loader.innerHTML = `
        <div class="loader-content">
            <div class="spinner"></div>
            <div class="loader-text">宁夏智慧养殖监管平台</div>
            <div class="loader-subtitle">正在加载中...</div>
        </div>
    `;
    
    document.body.appendChild(loader);
    
    // 页面加载完成后隐藏加载器
    window.addEventListener('load', function() {
        setTimeout(() => {
            loader.classList.add('fade-out');
            setTimeout(() => {
                if (loader.parentNode) {
                    loader.parentNode.removeChild(loader);
                }
            }, 500);
        }, 800);
    });
}

// 图片懒加载
function initLazyLoading() {
    const images = document.querySelectorAll('img[data-src]');
    
    const imageObserver = new IntersectionObserver((entries, observer) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const img = entry.target;
                img.src = img.dataset.src;
                img.classList.add('lazy-image');
                
                img.onload = () => {
                    img.classList.add('loaded');
                };
                
                observer.unobserve(img);
            }
        });
    });
    
    images.forEach(img => imageObserver.observe(img));
}

// 性能优化
function initPerformanceOptimizations() {
    // 导航栏滚动优化
    let ticking = false;
    
    function updateNavbar() {
        const navbar = document.querySelector('.navbar');
        if (window.scrollY > 50) {
            navbar.classList.add('scrolled');
        } else {
            navbar.classList.remove('scrolled');
        }
        ticking = false;
    }
    
    window.addEventListener('scroll', () => {
        if (!ticking) {
            requestAnimationFrame(updateNavbar);
            ticking = true;
        }
    });
    
    // 平滑滚动到锚点
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function(e) {
            e.preventDefault();
            const target = document.querySelector(this.getAttribute('href'));
            if (target) {
                target.scrollIntoView({
                    behavior: 'smooth',
                    block: 'start'
                });
            }
        });
    });
    
    // 预加载关键资源
    const criticalImages = [
        'assets/images/hero-bg.svg',
        'assets/images/logo.svg'
    ];
    
    criticalImages.forEach(src => {
        const link = document.createElement('link');
        link.rel = 'preload';
        link.as = 'image';
        link.href = src;
        document.head.appendChild(link);
    });
}

// 导航栏滚动效果
function initNavbarScroll() {
    const navbar = document.querySelector('.navbar');
    
    window.addEventListener('scroll', function() {
        if (window.scrollY > 100) {
            navbar.classList.add('navbar-scrolled');
        } else {
            navbar.classList.remove('navbar-scrolled');
        }
    });
}

// 滚动动画效果
function initScrollAnimation() {
    const observerOptions = {
        threshold: 0.1,
        rootMargin: '0px 0px -50px 0px'
    };

    const observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting) {
                entry.target.classList.add('animate__animated', 'animate__fadeInUp');
            }
        });
    }, observerOptions);

    // 观察需要动画的元素
    const animatedElements = document.querySelectorAll('.feature-card, .case-card, .news-card, .tech-item');
    animatedElements.forEach(function(el) {
        observer.observe(el);
    });
}

// 表单验证
function initFormValidation() {
    const contactForm = document.querySelector('.contact-form form');
    
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            if (validateForm(this)) {
                // 模拟表单提交
                showFormLoading();
                
                setTimeout(function() {
                    hideFormLoading();
                    showFormSuccess();
                    contactForm.reset();
                }, 2000);
            }
        });
    }
}

// 表单验证函数
function validateForm(form) {
    let isValid = true;
    const inputs = form.querySelectorAll('input, textarea');
    
    inputs.forEach(function(input) {
        if (input.hasAttribute('required') && !input.value.trim()) {
            showInputError(input, '此字段为必填项');
            isValid = false;
        } else if (input.type === 'email' && input.value && !isValidEmail(input.value)) {
            showInputError(input, '请输入有效的邮箱地址');
            isValid = false;
        } else if (input.type === 'tel' && input.value && !isValidPhone(input.value)) {
            showInputError(input, '请输入有效的手机号码');
            isValid = false;
        } else {
            clearInputError(input);
        }
    });
    
    return isValid;
}

// 手机号验证
function isValidPhone(phone) {
    const phoneRegex = /^1[3-9]\d{9}$/;
    return phoneRegex.test(phone);
}

// 提交联系表单
function submitContactForm(event) {
    event.preventDefault();
    
    const form = event.target;
    if (!validateForm(form)) {
        return;
    }
    
    const formData = new FormData(form);
    const data = {
        name: formData.get('name'),
        email: formData.get('email'),
        phone: formData.get('phone'),
        message: formData.get('message'),
        timestamp: new Date().toISOString()
    };
    
    // 显示提交中状态
    const submitBtn = form.querySelector('button[type="submit"]');
    const originalText = submitBtn.innerHTML;
    submitBtn.innerHTML = '<i class="bi bi-hourglass-split me-2"></i>提交中...';
    submitBtn.disabled = true;
    
    // 模拟提交到后端（实际项目中替换为真实API）
    setTimeout(() => {
        // 这里应该是实际的API调用
        console.log('表单数据:', data);
        
        // 显示成功消息
        showNotification('消息发送成功！我们会尽快回复您。', 'success');
        
        // 重置表单
        form.reset();
        
        // 恢复按钮状态
        submitBtn.innerHTML = originalText;
        submitBtn.disabled = false;
        
        // 存储到本地（演示用）
        saveContactToLocal(data);
        
    }, 2000);
}

// 显示通知消息
function showNotification(message, type = 'info') {
    const notification = document.createElement('div');
    notification.className = `alert alert-${type === 'success' ? 'success' : 'info'} alert-dismissible fade show position-fixed`;
    notification.style.cssText = 'top: 20px; right: 20px; z-index: 9999; min-width: 300px;';
    notification.innerHTML = `
        ${message}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    `;
    
    document.body.appendChild(notification);
    
    // 自动移除通知
    setTimeout(() => {
        if (notification.parentNode) {
            notification.parentNode.removeChild(notification);
        }
    }, 5000);
}

// 保存联系信息到本地存储（演示用）
function saveContactToLocal(formData) {
    let contacts = JSON.parse(localStorage.getItem('contacts') || '[]');
    contacts.push(formData);
    localStorage.setItem('contacts', JSON.stringify(contacts));
}

// 邮箱验证
function isValidEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}

// 显示输入错误
function showInputError(input, message) {
    clearInputError(input);
    
    const errorDiv = document.createElement('div');
    errorDiv.className = 'invalid-feedback';
    errorDiv.textContent = message;
    
    input.classList.add('is-invalid');
    input.parentNode.appendChild(errorDiv);
}

// 清除输入错误
function clearInputError(input) {
    input.classList.remove('is-invalid');
    const errorDiv = input.parentNode.querySelector('.invalid-feedback');
    if (errorDiv) {
        errorDiv.remove();
    }
}

// 显示表单加载状态
function showFormLoading() {
    const submitBtn = document.querySelector('.contact-form button[type="submit"]');
    const originalText = submitBtn.textContent;
    
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<span class="loading"></span> 发送中...';
    submitBtn.setAttribute('data-original-text', originalText);
}

// 隐藏表单加载状态
function hideFormLoading() {
    const submitBtn = document.querySelector('.contact-form button[type="submit"]');
    const originalText = submitBtn.getAttribute('data-original-text');
    
    submitBtn.disabled = false;
    submitBtn.textContent = originalText;
}

// 显示表单成功消息
function showFormSuccess() {
    // 创建成功消息提示
    const successAlert = document.createElement('div');
    successAlert.className = 'alert alert-success mt-3';
    successAlert.innerHTML = '\
        <i class="bi bi-check-circle-fill me-2"></i>\
        消息发送成功！我们会尽快回复您。\
    ';
    
    const form = document.querySelector('.contact-form form');
    form.appendChild(successAlert);
    
    // 3秒后自动隐藏
    setTimeout(function() {
        successAlert.remove();
    }, 3000);
}

// 数据统计计数器动画
function initStatsCounter() {
    const statsSection = document.querySelector('.stats-section');
    const statNumbers = document.querySelectorAll('.stat-item h3');
    let hasAnimated = false;
    
    const observer = new IntersectionObserver(function(entries) {
        entries.forEach(function(entry) {
            if (entry.isIntersecting && !hasAnimated) {
                hasAnimated = true;
                animateStats();
            }
        });
    }, { threshold: 0.5 });
    
    if (statsSection) {
        observer.observe(statsSection);
    }
}

// 数字动画效果
function animateStats() {
    const statNumbers = document.querySelectorAll('.stat-item h3');
    
    statNumbers.forEach(function(stat) {
        const target = parseInt(stat.textContent.replace(/[^0-9]/g, ''));
        const duration = 2000; // 动画时长2秒
        const frameDuration = 1000 / 60; // 60fps
        const totalFrames = Math.round(duration / frameDuration);
        let frame = 0;
        
        const originalText = stat.textContent;
        
        const counter = setInterval(function() {
            frame++;
            
            // 缓动函数
            const progress = frame / totalFrames;
            const easeOutQuart = 1 - Math.pow(1 - progress, 4);
            const current = Math.round(target * easeOutQuart);
            
            stat.textContent = originalText.replace(/\d+/g, current.toLocaleString());
            
            if (frame === totalFrames) {
                clearInterval(counter);
                stat.textContent = originalText; // 恢复原始文本（包含符号）
            }
        }, frameDuration);
    });
}

// 平滑滚动到锚点
function smoothScrollTo(targetId) {
    const targetElement = document.querySelector(targetId);
    if (targetElement) {
        const offsetTop = targetElement.offsetTop - 80; // 考虑固定导航栏的高度
        
        window.scrollTo({
            top: offsetTop,
            behavior: 'smooth'
        });
    }
}

// 导航链接点击事件
document.querySelectorAll('a[href^="#"]').forEach(function(anchor) {
    anchor.addEventListener('click', function(e) {
        e.preventDefault();
        
        const targetId = this.getAttribute('href');
        if (targetId !== '#') {
            smoothScrollTo(targetId);
        }
    });
});

// 响应式导航菜单关闭
function closeMobileMenu() {
    const navbarToggler = document.querySelector('.navbar-toggler');
    const navbarCollapse = document.querySelector('.navbar-collapse');
    
    if (navbarToggler && navbarCollapse.classList.contains('show')) {
        navbarToggler.click();
    }
}

// 页面性能监控（可选）
function monitorPerformance() {
    if ('performance' in window) {
        window.addEventListener('load', function() {
            const navigationTiming = performance.getEntriesByType('navigation')[0];
            console.log('页面加载时间:', navigationTiming.loadEventEnd - navigationTiming.navigationStart, 'ms');
        });
    }
}

// 错误处理
window.addEventListener('error', function(e) {
    console.error('页面错误:', e.error);
});

// 初始化性能监控
monitorPerformance();