<?php
/**
 * 搜索功能短代码
 *
 * @package Chinese-Character-Dictionary
 */

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

/**
 * 注册搜索表单短代码
 */
function chinese_character_search_form_shortcode($atts) {
    $atts = shortcode_atts(array(
        'placeholder' => '请输入汉字、词语或成语...',
        'button_text' => '搜索',
        'show_options' => 'true',
        'show_recommendations' => 'true',
    ), $atts, 'character_search_form');

    ob_start();
    ?>
    
    
  <div class="professional-search-wrapper">
    <div class="chinese-character-search-wrapper">
        <form class="chinese-character-search-form" method="get" action="<?php echo esc_url(home_url('/')); ?>">
            <div class="search-input-group">
                <input 
                    type="text" 
                    name="s" 
                    placeholder="<?php echo esc_attr($atts['placeholder']); ?>" 
                    value="<?php echo get_search_query(); ?>"
                    autocomplete="off"
                    required
                >
                <button type="submit" class="search-submit-button">
                	<span class="search-icon">🔍</span>
                    <span class="search-text"><?php echo esc_html($atts['button_text']); ?></span>
                </button>
            </div>
        </form>
        
        <?php if ($atts['show_options'] === 'true') : ?>
        <div class="search-options">
            <p>或通过以下方式查找：</p>
            <div class="search-option-links">
                <a href="<?php echo home_url('/pinyin-index'); ?>">拼音检索</a>
                <a href="<?php echo home_url('/radical-index'); ?>">部首检索</a>
            </div>
        </div>
        <?php endif; ?>
        
        <?php 
        // 显示推荐内容
        if ($atts['show_recommendations'] === 'true') :
            $recommended_content = get_option('chinese_character_recommended_content', array(
                'characters' => array(),
                'words' => array(),
                'idioms' => array(),
                'ancient_texts' => array(),
                'calligraphies' => array(),
                'posts' => array(),
                'pages' => array()
            ));
            
            // 检查是否有任何推荐内容
            $has_recommended_content = false;
            foreach ($recommended_content as $content_type => $content_ids) {
                if (!empty($content_ids)) {
                    $has_recommended_content = true;
                    break;
                }
            }
            
            if ($has_recommended_content) : ?>
            <div class="recommended-content">
                <h3>推荐内容</h3>
                
                <?php if (!empty($recommended_content['characters'])) : ?>
                <div class="recommended-section">
                    <h4>推荐汉字</h4>
                    <div class="recommended-characters recommended-items">
                        <?php foreach ($recommended_content['characters'] as $char_id) : 
                            $char_post = get_post($char_id);
                            if ($char_post && $char_post->post_status === 'publish') :
                                $pinyin = get_post_meta($char_id, '_character_pinyin', true);
                        ?>
                            <a href="<?php echo get_permalink($char_id); ?>" class="recommended-character recommended-item">
                                <span class="char"><?php echo esc_html($char_post->post_title); ?></span>
                                <span class="pinyin"><?php echo esc_html($pinyin); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['words'])) : ?>
                <div class="recommended-section">
                    <h4>推荐词语</h4>
                    <div class="recommended-words recommended-items">
                        <?php foreach ($recommended_content['words'] as $word_id) : 
                            $word_post = get_post($word_id);
                            if ($word_post && $word_post->post_status === 'publish') :
                                $pinyin = get_post_meta($word_id, '_dictionary_pinyin', true);
                        ?>
                            <a href="<?php echo get_permalink($word_id); ?>" class="recommended-word recommended-item">
                                <span class="word"><?php echo esc_html($word_post->post_title); ?></span>
                                <span class="pinyin"><?php echo esc_html($pinyin); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['idioms'])) : ?>
                <div class="recommended-section">
                    <h4>推荐成语</h4>
                    <div class="recommended-idioms recommended-items">
                        <?php foreach ($recommended_content['idioms'] as $idiom_id) : 
                            $idiom_post = get_post($idiom_id);
                            if ($idiom_post && $idiom_post->post_status === 'publish') :
                                $pinyin = get_post_meta($idiom_id, '_idiom_pinyin', true);
                        ?>
                            <a href="<?php echo get_permalink($idiom_id); ?>" class="recommended-idiom recommended-item">
                                <span class="idiom"><?php echo esc_html($idiom_post->post_title); ?></span>
                                <span class="pinyin"><?php echo esc_html($pinyin); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['ancient_texts'])) : ?>
                <div class="recommended-section">
                    <h4>推荐古籍</h4>
                    <div class="recommended-ancient-texts recommended-items">
                        <?php foreach ($recommended_content['ancient_texts'] as $ancient_text_id) : 
                            $ancient_text_post = get_post($ancient_text_id);
                            if ($ancient_text_post && $ancient_text_post->post_status === 'publish') :
                        ?>
                            <a href="<?php echo get_permalink($ancient_text_id); ?>" class="recommended-ancient-text recommended-item">
                                <span class="ancient-text"><?php echo esc_html($ancient_text_post->post_title); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['calligraphies'])) : ?>
                <div class="recommended-section">
                    <h4>推荐书法作品</h4>
                    <div class="recommended-calligraphies recommended-items">
                        <?php foreach ($recommended_content['calligraphies'] as $calligraphy_id) : 
                            $calligraphy_post = get_post($calligraphy_id);
                            if ($calligraphy_post && $calligraphy_post->post_status === 'publish') :
                        ?>
                            <a href="<?php echo get_permalink($calligraphy_id); ?>" class="recommended-calligraphy recommended-item">
                                <span class="calligraphy"><?php echo esc_html($calligraphy_post->post_title); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['posts'])) : ?>
                <div class="recommended-section">
                    <h4>推荐文章</h4>
                    <div class="recommended-posts recommended-items">
                        <?php foreach ($recommended_content['posts'] as $post_id) : 
                            $post_item = get_post($post_id);
                            if ($post_item && $post_item->post_status === 'publish') :
                        ?>
                            <a href="<?php echo get_permalink($post_id); ?>" class="recommended-post recommended-item">
                                <span class="post"><?php echo esc_html($post_item->post_title); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <?php if (!empty($recommended_content['pages'])) : ?>
                <div class="recommended-section">
                    <h4>推荐页面</h4>
                    <div class="recommended-pages recommended-items">
                        <?php foreach ($recommended_content['pages'] as $page_id) : 
                            $page_item = get_post($page_id);
                            if ($page_item && $page_item->post_status === 'publish') :
                        ?>
                            <a href="<?php echo get_permalink($page_id); ?>" class="recommended-page recommended-item">
                                <span class="page"><?php echo esc_html($page_item->post_title); ?></span>
                            </a>
                        <?php endif; endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        <?php endif; ?>
        
         <div class="search-instructions">
            <h3>使用说明</h3>
            <ul>
                <li>输入单个汉字（如：一、二、三）可直接查看详细解释</li>
                <li>输入词语（如：风和日丽）可直接查看词语解释</li>
                <li>输入成语（如：一帆风顺）可直接查看成语解释</li>
                <li>选择不同的搜索类型可精确查找相应内容</li>
            </ul>
        </div>
        
    </div>
  </div>
    
    <style>
    
    .professional-search-wrapper {
    max-width: 1200px;
    margin: 40px auto;
    padding: 0 20px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
    
    .recommended-content {
        margin: 20px 0;
        padding: 15px;
        background-color: #f8f9fa;
        border-radius: 8px;
    }

    .recommended-content h3 {
        margin-top: 0;
        margin-bottom: 15px;
        color: #333;
        font-size: 1.1em;
    }

    .recommended-section {
        margin-bottom: 20px;
    }

    .recommended-section:last-child {
        margin-bottom: 0;
    }

    .recommended-section h4 {
        margin-top: 0;
        margin-bottom: 10px;
        color: #555;
        font-size: 1em;
    }

    .recommended-items {
        display: flex;
        flex-wrap: wrap;
        gap: 10px;
    }

    .recommended-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 10px;
        background: white;
        border-radius: 6px;
        text-decoration: none;
        color: inherit;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        transition: all 0.3s ease;
        min-width: 60px;
    }

    .recommended-item:hover {
        transform: translateY(-3px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    }

    .recommended-character .char,
    .recommended-word .word,
    .recommended-idiom .idiom,
    .recommended-ancient-text .ancient-text,
    .recommended-calligraphy .calligraphy {
        font-size: 1.5em;
        font-weight: bold;
        color: #3498db;
    }

    .recommended-item .pinyin {
        font-size: 0.8em;
        color: #666;
        margin-top: 5px;
    }
    </style>
    <?php
    return ob_get_clean();
}
add_shortcode('character_search_form', 'chinese_character_search_form_shortcode');

/**
 * 注册搜索结果短代码
 */
function chinese_character_search_results_shortcode($atts) {
    $atts = shortcode_atts(array(
        'posts_per_page' => 10,
    ), $atts, 'character_search_results');

    $search_query = get_search_query();
    
    if (empty($search_query)) {
        return '<p>请输入搜索关键词</p>';
    }

    $args = array(
        's' => $search_query,
        'post_type' => array('character', 'dictionary', 'idiom', 'ancient_text', 'post', 'page'),
        'posts_per_page' => intval($atts['posts_per_page']),
    );
    
    $search_results = new WP_Query($args);
    
    ob_start();
    
    if ($search_results->have_posts()) : ?>
        <div class="chinese-character-search-results">
            <h3>搜索结果 (共 <?php echo $search_results->found_posts; ?> 个结果)</h3>
            <div class="search-results-list">
                <?php while ($search_results->have_posts()) : $search_results->the_post(); ?>
                    <div class="search-result-item">
                        <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                        <div class="search-result-excerpt">
                            <?php the_excerpt(); ?>
                        </div>
                        <div class="search-result-meta">
                            <span class="result-type"><?php echo get_post_type_object(get_post_type())->labels->singular_name; ?></span>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
        <?php 
        wp_reset_postdata();
    else : ?>
        <div class="chinese-character-search-results">
            <p>未找到与 "<?php echo esc_html($search_query); ?>" 相关的结果</p>
        </div>
    <?php endif;
    
    return ob_get_clean();
}
add_shortcode('character_search_results', 'chinese_character_search_results_shortcode');