<?php
/**
 * 股票信息区块模板
 * 
 * @package Nenghui_Energy_Theme
 */

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

// 获取区块属性（支持直接调用和短代码调用）
global $stock_info_attributes;

// 优先使用全局变量（短代码调用），其次使用直接传入的属性
$current_attributes = !empty($stock_info_attributes) ? $stock_info_attributes : (isset($attributes) ? $attributes : array());

$stock_code = isset($current_attributes['stockCode']) ? $current_attributes['stockCode'] : 'sz301046';
$block_id = isset($current_attributes['blockId']) ? $current_attributes['blockId'] : 'stock-info-' . uniqid();
$theme = isset($current_attributes['theme']) ? $current_attributes['theme'] : 'default';
$show_chart = isset($current_attributes['showChart']) ? $current_attributes['showChart'] : false;

// 加载样式和脚本
wp_enqueue_style('stock-info-style', get_template_directory_uri() . '/assets/css/stock-info.css', array(), '1.0.0');
wp_enqueue_script('stock-info-script', get_template_directory_uri() . '/assets/js/stock-info.js', array('jquery'), '1.0.0', true);

// 本地化脚本数据
wp_localize_script('stock-info-script', 'stockInfoAjax', array(
    'apiUrl' => 'http://sqt.gtimg.cn/q=',
    'nonce' => wp_create_nonce('stock_info_nonce')
));
?>

<div class="stock-info-block theme-<?php echo esc_attr($theme); ?>" id="<?php echo esc_attr($block_id); ?>" data-stock-code="<?php echo esc_attr($stock_code); ?>">
    <div class="stock-info-container">
        <!-- 股票基本信息头部 -->
        <div class="stock-header">
            <div class="stock-basic-info">
                <span class="stock-label">股票代码:</span>
                <span class="stock-code" id="stock-code-display"><?php echo esc_html($stock_code); ?></span>
            </div>
            <div class="update-time">
                <span class="update-label">截至</span>
                <span class="update-timestamp" id="update-time">加载中...</span>
            </div>
        </div>

        <!-- 主要股票数据 -->
        <div class="stock-main-data">
            <div class="stock-price-section">
                <div class="current-price">
                    <span class="price-value" id="current-price">--</span>
                    <div class="price-change">
                        <span class="change-value" id="price-change">--</span>
                        <span class="change-percent" id="change-percent">--</span>
                    </div>
                </div>
            </div>

            <div class="stock-volume-section">
                <div class="volume-data">
                    <span class="volume-value" id="volume-value">--</span>
                    <span class="volume-label">成交量 (手)</span>
                </div>
            </div>

            <div class="stock-amount-section">
                <div class="amount-data">
                    <span class="amount-value" id="amount-value">--</span>
                    <span class="amount-label">成交额 (万元)</span>
                </div>
            </div>
        </div>

        <!-- 详细数据 -->
        <div class="stock-details" style="display: none;">
            <div class="detail-row">
                <div class="detail-item">
                    <span class="detail-label">今开:</span>
                    <span class="detail-value" id="open-price">--</span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">昨收:</span>
                    <span class="detail-value" id="prev-close">--</span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">最高:</span>
                    <span class="detail-value" id="high-price">--</span>
                </div>
                <div class="detail-item">
                    <span class="detail-label">最低:</span>
                    <span class="detail-value" id="low-price">--</span>
                </div>
            </div>
        </div>

        <!-- 加载状态 -->
        <div class="loading-state" id="loading-indicator">
            <div class="loading-spinner"></div>
            <span class="loading-text">正在加载股票数据...</span>
        </div>

        <!-- 错误状态 -->
        <div class="error-state" id="error-indicator" style="display: none;">
            <div class="error-icon">⚠</div>
            <span class="error-text">数据加载失败，请稍后重试</span>
            <button class="retry-button" onclick="window.stockInfoManager.refreshData()">重新加载</button>
        </div>
    </div>
</div>