<?php
/**
 * 产品详情页模板
 * Template for displaying product content
 */

// 防止直接访问
if (!defined('ABSPATH')) {
    exit;
}
?>
<!-- 产品顶部Banner -->
<?php
// 获取产品Banner图片
$banner_url = get_post_meta(get_the_ID(), '_product_banner_url', true);
// 如果没有设置自定义Banner，使用默认图片
if (empty($banner_url)) {
    $banner_url = get_template_directory_uri() . '/assets/images/products-1.webp';
}
?>
<div class="product-banner" style="width: 100%; height: 600px; background-image: url('<?php echo esc_url($banner_url); ?>'); background-size: cover; background-position: center; background-repeat: no-repeat;">
    <!-- 产品标题 -->
    <div class="p-container" style="padding: 40px 0; display: flex; flex-direction: column; justify-content: center; width: 80%;margin: 0 auto;height: 100%;">
        <h2 class="product-title" style="font-size: 2.5rem; font-weight: 600; color: #24528d; margin: 0; line-height: 1.2;"><?php echo esc_html(get_the_title()); ?></h2>
        <?php 
        $product_description = get_post_meta(get_the_ID(), '_product_description', true);
        if (!empty($product_description)): 
        ?>
        <div class="product-description" style="font-size: 1.5rem;color: #24528d;"><?php echo wp_kses_post($product_description); ?></div>
        <?php endif; ?>
    </div>
</div>
<!-- 产品详情内容 -->
<div class="container mt-5" style="margin: 1rem auto; ">
    <?php
    // 获取产品特性显示开关
    $show_features = get_post_meta(get_the_ID(), '_show_product_features', true);
    if ($show_features === '') {
        $show_features = '1'; // 默认显示
    }
    ?>
    
    <?php if ($show_features === '1'): ?>
    <!-- 产品特性卡片 -->
    <div class="product-features">
        <div class="features-grid">
            <?php
            // 获取产品特性数据
            $product_features = get_post_meta(get_the_ID(), '_product_features', true);
            
            // 如果没有自定义数据，使用默认数据
            if (empty($product_features)) {
                $product_features = array(
                    array(
                        'title' => 'COMPACT<br>DESIGN',
                        'description' => 'Light footprint, easy frame<br>installation and field installation.',
                        'icon' => 'product-icon-1.webp',
                        'alt' => 'Compact Design'
                    ),
                    array(
                        'title' => 'HIGH<br>INTEGRATION',
                        'description' => '20kWh+ energy in one cabinet<br>with DC/AC power conversion<br>& thermal cooling.',
                        'icon' => 'product-icon-2.webp',
                        'alt' => 'High Integration'
                    ),
                    array(
                        'title' => 'EFFICIENT<br>COOLING',
                        'description' => 'Optimal air-duct design optimizes<br>airflow for improved cooling with low<br>energy consumption.',
                        'icon' => 'product-icon-3.webp',
                        'alt' => 'Efficient Cooling'
                    ),
                    array(
                        'title' => 'LONG<br>CYCLE LIFE',
                        'description' => 'Over 6,000 times cycle life, excellent<br>performance of battery system.',
                        'icon' => 'product-icon-4.webp',
                        'alt' => 'Long Cycle Life'
                    ),
                    array(
                        'title' => 'FLEXIBLE<br>EXPANSION',
                        'description' => 'Modular design, simplified panel<br>for expansion.',
                        'icon' => 'product-icon-5.webp',
                        'alt' => 'Flexible Expansion'
                    ),
                    array(
                        'title' => 'ULTIMATE<br>SAFETY',
                        'description' => 'Comprehensive safety protection<br>with NOVEC1230 thermal prevent<br>heat diffusion and spreading.',
                        'icon' => 'product-icon-6.webp',
                        'alt' => 'Ultimate Safety'
                    )
                );
            }
            
            // 循环输出产品特性卡片
            foreach ($product_features as $feature) :
                if (!empty($feature['title'])) :
            ?>
            <div class="feature-card">
                <div class="feature-icon">
                    <?php 
                    $icon_src = $feature['icon'];
                    if (filter_var($icon_src, FILTER_VALIDATE_URL)) {
                        $icon_url = $icon_src;
                    } else {
                        $icon_url = get_template_directory_uri() . '/assets/images/' . $icon_src;
                    }
                    ?>
                    <img src="<?php echo esc_url($icon_url); ?>" alt="<?php echo esc_attr($feature['alt']); ?>">
                </div>
                <h3 class="feature-title"><?php echo wp_kses_post($feature['title']); ?></h3>
                <p class="feature-description"><?php echo wp_kses_post($feature['description']); ?></p>
            </div>
            <?php
                endif;
            endforeach;
            ?>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    // 获取使用场景显示开关
    $show_usage_scenario = get_post_meta(get_the_ID(), '_show_usage_scenario', true);
    if ($show_usage_scenario === '') {
        $show_usage_scenario = '1'; // 默认显示
    }
    ?>
    
    <?php if ($show_usage_scenario === '1'): ?>
    <!-- 使用场景板块 -->
    <div class="usage-scenario-section">
        <?php
        // 获取使用场景数据
        $usage_scenario_data = get_post_meta(get_the_ID(), '_usage_scenario_data', true);
        
        // 如果没有自定义数据，使用默认数据
        if (empty($usage_scenario_data)) {
            $usage_scenario_data = array(
                'title' => 'USAGE SCENARIO',
                'description' => 'Off-grid PV, energy storage, diesel generation, and charging post',
                'image' => 'Usage scenario.webp',
                'bottom_text' => 'Nenghui all-in-one liquid-cooled ESS cabinet adopts advanced cabinet-level liquid cooling and temperature balancing strategy. The cell temperature difference is less than 3°C, which further improves the consistency of cell temperature and extends the battery life. The modular design makes the parallel solution more flexible and has a higher energy density, which significantly improves the economy, safety and construction convenience of ESS projects.'
            );
        }
        ?>
        
        <div class="usage-scenario-content">
            <div class="scenario-header">
                <h2 class="scenario-title"><?php echo wp_kses_post($usage_scenario_data['title']); ?></h2>
                <p class="scenario-description"><?php echo wp_kses_post($usage_scenario_data['description']); ?></p>
            </div>
            
            <div class="scenario-image">
                <?php 
                $image_src = $usage_scenario_data['image'];
                // 如果是完整URL，直接使用；否则拼接主题目录路径
                if (filter_var($image_src, FILTER_VALIDATE_URL)) {
                    $image_url = $image_src;
                } else {
                    $image_url = get_template_directory_uri() . '/assets/images/' . $image_src;
                }
                ?>
                <img src="<?php echo esc_url($image_url); ?>" alt="Usage Scenario">
            </div>
            
            <div class="scenario-footer">
                <p class="scenario-text" ><?php echo wp_kses_post($usage_scenario_data['bottom_text']); ?></p>
            </div>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    // 获取技术规格显示开关
    $show_technical_specs = get_post_meta(get_the_ID(), '_show_technical_specs', true);
    if ($show_technical_specs === '') {
        $show_technical_specs = '1'; // 默认显示
    }
    ?>
    
    <?php if ($show_technical_specs === '1'): ?>
    <!-- 技术规格板块 -->
    <div class="technical-specs-section">
        <?php
        // 获取技术规格数据
        $technical_specs_data = get_post_meta(get_the_ID(), '_technical_specs_data', true);
        
        // 如果没有自定义数据，使用默认数据
        if (empty($technical_specs_data)) {
            $technical_specs_data = array(
                array(
                    'title' => 'DC Side',
                    'content' => '<table><tr><td>Cell Type</td><td>LFP280Ah</td></tr><tr><td>PACK</td><td>46.592kWh/IP25S</td></tr><tr><td>Battery System</td><td>232.96kWh/IP260S</td></tr><tr><td>Rated Voltage</td><td>832Vdc</td></tr><tr><td>Voltage Range</td><td>728-936Vdc</td></tr><tr><td>Pack Ingress Rating</td><td>IP65</td></tr></table>',
                    'expanded' => true
                ),
                array(
                    'title' => 'AC Side',
                    'content' => '<table><tr><td>Rated Power</td><td>100kW</td></tr><tr><td>Max. Power</td><td>110kW</td></tr><tr><td>THDi</td><td>≤3%</td></tr><tr><td>DC Ratio</td><td>≤0.5%lon</td></tr><tr><td>Nominal Voltage</td><td>400Vac/3P+N+PE</td></tr><tr><td>Power Factor</td><td>-1lagging~1leading</td></tr><tr><td>Nominal Frequency</td><td>50Hz/60Hz</td></tr></table>',
                    'expanded' => false
                )
            );
        }
        ?>
        
        <div class="technical-specs-content">
            <div class="specs-header">
                <h2 class="specs-title">TECHNICAL SPECIFICATION</h2>
            </div>
            
            <div class="specs-accordion" role="tablist">
                <?php foreach ($technical_specs_data as $index => $spec): ?>
                    <?php $is_expanded = !empty($spec['expanded']); ?>
                <div class="accordion-item <?php echo $is_expanded ? 'is-expanded' : ''; ?>">
                    <button class="accordion-trigger" 
                            type="button"
                            aria-expanded="<?php echo $is_expanded ? 'true' : 'false'; ?>"
                            aria-controls="spec-panel-<?php echo $index; ?>"
                            id="spec-trigger-<?php echo $index; ?>">
                        <h3 class="accordion-title"><?php echo wp_kses_post($spec['title']); ?></h3>
                        <span class="accordion-icon" aria-hidden="true"></span>
                    </button>
                    <div class="accordion-panel" 
                         id="spec-panel-<?php echo $index; ?>"
                         role="region"
                         aria-labelledby="spec-trigger-<?php echo $index; ?>"
                         <?php echo $is_expanded ? '' : 'hidden'; ?>>
                        <div class="accordion-content">
                            <?php echo wp_kses_post($spec['content']); ?>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <?php endif; ?>
    
    <?php
    // 获取PDF下载显示开关
    $show_pdf_downloads = get_post_meta(get_the_ID(), '_show_pdf_downloads', true);
    if ($show_pdf_downloads === '') {
        $show_pdf_downloads = '1'; // 默认显示
    }
    ?>
    
    <?php if ($show_pdf_downloads === '1'): ?>
    <!-- PDF下载板块 -->
    <div class="pdf-downloads-section">
        <?php
        // 获取PDF下载数据
        $pdf_downloads_data = get_post_meta(get_the_ID(), '_pdf_downloads_data', true);
        
        // 如果没有自定义数据，使用默认数据
        if (empty($pdf_downloads_data)) {
            $pdf_downloads_data = array(
                array(
                    'title' => 'Nenghui\'s innovative 40',
                    'file_url' => '',
                    'file_size' => '',
                    'description' => ''
                )
            );
        }
        
        // 过滤掉空的PDF项目
        $pdf_downloads_data = array_filter($pdf_downloads_data, function($pdf) {
            return !empty($pdf['title']) || !empty($pdf['file_url']);
        });
        ?>
        
        <?php if (!empty($pdf_downloads_data)): ?>
        <div class="pdf-downloads-content">
            <div class="downloads-header">
                <h2 class="downloads-title">DOWNLOADS</h2>
            </div>
            
            <div class="downloads-grid">
                <?php foreach ($pdf_downloads_data as $index => $pdf): ?>
                <div class="download-item">
                    <div class="download-icon">
                        <img src="<?php echo get_template_directory_uri(); ?>/assets/images/PDF-ICON.svg" alt="PDF" class="pdf-icon">
                    </div>
                    <div class="download-info">
                        <h3 class="download-title"><?php echo esc_html($pdf['title']); ?></h3>
                        <?php if (!empty($pdf['description'])): ?>
                        <p class="download-description"><?php echo esc_html($pdf['description']); ?></p>
                        <?php endif; ?>
                        <?php if (!empty($pdf['file_size'])): ?>
                        <span class="download-size"><?php echo esc_html($pdf['file_size']); ?></span>
                        <?php endif; ?>
                    </div>
                    <?php if (!empty($pdf['file_url'])): ?>
                    <div class="download-action">
                        <a href="<?php echo esc_url($pdf['file_url']); ?>" class="download-button" download target="_blank">
                            <img src="<?php echo get_template_directory_uri(); ?>/assets/images/download-icon.svg" alt="Download" class="download-btn-icon">
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
    <?php endif; ?>
</div>

<?php
// 加载产品详情页样式和脚本（避免重复加载）
if (!wp_style_is('products-content-css', 'enqueued')) {
    wp_enqueue_style('products-content-css', get_template_directory_uri() . '/assets/css/products-content.css', array(), filemtime(get_template_directory() . '/assets/css/products-content.css'));
}
if (!wp_script_is('products-content-js', 'enqueued')) {
    wp_enqueue_script('products-content-js', get_template_directory_uri() . '/assets/js/products-content.js', array('jquery'), filemtime(get_template_directory() . '/assets/js/products-content.js'), true);
}
?>
