<?php
/**
 * 古籍单页模板
 */

// 防止直接访问
use function PHPSTORM_META\type;

if (!defined('ABSPATH')) {
    exit;
}

get_header();

if (have_posts()) :
    while (have_posts()) : the_post();
        $dynasty = get_post_meta(get_the_ID(), '_ancient_text_dynasty', true);
        $author = get_post_meta(get_the_ID(), '_ancient_text_author', true);
        $publisher = get_post_meta(get_the_ID(), '_ancient_text_publisher', true);
        $publication_year = get_post_meta(get_the_ID(), '_ancient_text_publication_year', true);
        $volume = get_post_meta(get_the_ID(), '_ancient_text_volume', true);
        $chapter = get_post_meta(get_the_ID(), '_ancient_text_chapter', true);
        $category = get_post_meta(get_the_ID(), '_ancient_text_category', true); // 经史子集分类
        $content = get_post_meta(get_the_ID(), '_ancient_text_content', true);
        
        // 检查是否是阅读模式
        $reading_mode = isset($_GET['mode']) && $_GET['mode'] === 'reading';
        ?>
        
        <div class="wrap ancient-text-single">
        	
        	<div id="primary" class="content-area">
	        	<main id="main" class="site-main">

            <?php if ($reading_mode): ?>
                <!-- 阅读模式 -->
                <div class="reading-mode-header">
                    <h1><?php the_title(); ?></h1>
                    <div class="reading-mode-controls">
                        <a href="<?php the_permalink(); ?>" class="exit-reading-mode">退出阅读模式</a>
                    </div>
                </div>
                <div class="reading-mode-content">
                    <?php if (!empty($content)): ?>
                        <?php echo wpautop($content); ?>
                    <?php else: ?>
                        <?php the_content(); ?>
                    <?php endif; ?>
                </div>
            <?php else: ?>
                <!-- 普通模式 -->
                <div class="ancient-text-header">
                    <h1><?php the_title(); ?></h1>
                    
                    <div class="ancient-text-meta">
                        <table>
                            <?php if (!empty($dynasty)): ?>
                                <tr>
                                    <th>朝代：</th>
                                    <td><?php echo esc_html($dynasty); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($author)): ?>
                                <tr>
                                    <th>作者：</th>
                                    <td><?php echo esc_html($author); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($publisher)): ?>
                                <tr>
                                    <th>出版者：</th>
                                    <td><?php echo esc_html($publisher); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($publication_year)): ?>
                                <tr>
                                    <th>出版年份：</th>
                                    <td><?php echo esc_html($publication_year); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($volume)): ?>
                                <tr>
                                    <th>卷数：</th>
                                    <td><?php echo esc_html($volume); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($chapter)): ?>
                                <tr>
                                    <th>章节：</th>
                                    <td><?php echo esc_html($chapter); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (!empty($category)): ?>
                                <tr>
                                    <th>分类：</th>
                                    <td><?php echo esc_html($category); ?></td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php 
                            // 显示关联的汉字
                            $related_characters = get_post_meta(get_the_ID(), '_ancient_text_related_characters', true);
                            if ($related_characters && is_array($related_characters) && !empty($related_characters)):
                            ?>
                                <tr>
                                    <th>相关汉字：</th>
                                    <td>
                                        <?php 
                                        $character_links = array();
                                        foreach ($related_characters as $character_id) {
                                            $character = get_post($character_id);
                                            if ($character && $character->post_status === 'publish') {
                                                $character_links[] = '<a href="' . get_permalink($character_id) . '">' . esc_html($character->post_title) . '</a>';
                                            }
                                        }
                                        echo implode(', ', $character_links);
                                        ?>
                                    </td>
                                </tr>
                            <?php endif; ?>
                            
                            <?php if (current_user_can('edit_posts')): ?>
                                <tr>
                                    <th>管理：</th>
                                    <td>
                                        <a href="<?php echo admin_url('edit.php?post_type=ancient_text_chapter'); ?>">章节管理</a> | 
                                        <a href="<?php echo admin_url('post-new.php?post_type=ancient_text_chapter'); ?>">添加章节</a>
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </table>
                    </div>
                </div>
                
                <?php
                // 获取当前古籍的章节目录
                $chapters = get_posts(array(
                    'post_type' => 'ancient_text_chapter',
                    'post_status' => array('publish', 'private'),
                    'posts_per_page' => -1,
                    'meta_key' => '_chapter_ancient_text',
                    'meta_value' => get_the_ID(),
                    'orderby' => 'menu_order',
                    'order' => 'ASC'
                ));
                
                // 如果存在章节，则显示目录
                if (!empty($chapters)):
                    // 将章节分为三栏
                    $total_chapters = count($chapters);
                    $chapters_per_column = ceil($total_chapters / 3);
                    $columns = array_chunk($chapters, $chapters_per_column);
                ?>
                <div class="ancient-text-toc">
                    <h2>目录</h2>
                    <div class="toc-columns-container">
                        <?php foreach ($columns as $column_index => $column_chapters): ?>
                        <div class="toc-column">
                            <ul class="toc-list">
                                <?php foreach ($column_chapters as $chapter_post): 
                                    $chapter_volume = get_post_meta($chapter_post->ID, '_chapter_volume', true);
                                    $chapter_number = get_post_meta($chapter_post->ID, '_chapter_number', true);
                                    $chapter_status = get_post_status($chapter_post->ID);
                                    $status_class = '';
                                    $status_text = '';
                                    
                                    if ($chapter_status === 'draft') {
                                        $status_class = 'status-draft';
                                        $status_text = ' (草稿)';
                                    } elseif ($chapter_status === 'private') {
                                        $status_class = 'status-private';
                                        $status_text = ' (私有)';
                                    }
                                ?>
                                <li class="toc-item <?php echo $status_class; ?>">
                                    <a href="<?php echo get_permalink($chapter_post->ID); ?>" class="toc-link">
                                        <?php if (!empty($chapter_volume)): ?>
                                            <span class="chapter-volume"><?php echo esc_html($chapter_volume); ?></span>
                                        <?php endif; ?>
                                        <?php if (!empty($chapter_number)): ?>
                                            <span class="chapter-number"><?php echo esc_html($chapter_number); ?></span>
                                        <?php endif; ?>
                                        <span class="chapter-title"><?php echo esc_html($chapter_post->post_title); ?></span>
                                        <?php if (!empty($status_text)): ?>
                                            <span class="chapter-status"><?php echo $status_text; ?></span>
                                        <?php endif; ?>
                                    </a>
                                </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endif; ?>
              
                <div class="ancient-text-content">
                    <h2>古籍内容</h2>
                    <?php if (!empty($content)): ?>
                        <div class="content-text">
                            <?php echo wpautop($content); ?>
                        </div>
                    <?php endif; ?>
                    
                    <?php 
                    // 显示默认编辑器内容
                    $default_content = get_the_content();
                    if (!empty($default_content)): 
                    ?>
                        <div class="default-content-text">
                            <?php 
                            // 如果有自定义内容且默认内容，则添加分隔线
                            if (!empty($content)) {
                                echo '<hr>';
                            }
                            the_content(); 
                            ?>
                        </div>
                    <?php endif; ?>
                </div>
                
                <div class="ancient-text-navigation">
                    <div class="nav-links">
                        <div class="nav-previous">
                            <?php previous_post_link('%link', '&larr; 上一部古籍'); ?>
                        </div>
                        <div class="nav-next">
                            <?php next_post_link('%link', '下一部古籍 &rarr;'); ?>
                        </div>
                    </div>
                </div>
                
                <?php if (current_user_can('edit_posts')): ?>
                <div class="reading-mode-link">
                    <a href="<?php echo add_query_arg('mode', 'reading', get_permalink()); ?>" class="enter-reading-mode">进入阅读模式</a>
                </div>
                <?php endif; ?>
            <?php endif; ?>
            
           </main><!-- .site-main -->
	         </div><!-- .content-area -->
	        <?php get_sidebar(); ?>

        </div>
        
        <style>
        .ancient-text-single {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .ancient-text-header h1 {
            text-align: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        
        .ancient-text-meta {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 30px;
        }
        
        .ancient-text-meta table {
            width: 100%;
        }
        
        .ancient-text-meta th {
            text-align: right;
            padding-right: 15px;
            width: 100px;
            color: #333;
        }
        
        .ancient-text-meta td {
            padding: 5px 0;
        }
        
        /* 目录样式 */
        .ancient-text-toc {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 30px;
            border: 1px solid #ddd;
        }
        
        .ancient-text-toc h2 {
            margin-top: 0;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
        }
        
        .toc-columns-container {
            display: flex;
            gap: 20px;
        }
        
        .toc-column {
            flex: 1;
        }
        
        .toc-list {
            list-style: none;
            padding: 0;
            margin: 0 0 20px 0;
        }
        
        .toc-item {
            padding: 6px 0;
            border-bottom: 1px solid #eee;
        }
        
        .toc-item.status-draft .chapter-title {
            color: #d54e21;
        }
        
        .toc-item.status-private .chapter-title {
            color: #999;
        }
        
        .toc-item:last-child {
            border-bottom: none;
        }
        
        .toc-item.level-3 {
            padding-left: 20px;
            font-size: 0.9em;
        }
        
        .toc-link {
            text-decoration: none;
            color: #333;
            display: block;
            transition: color 0.3s ease;
        }
        
        .toc-link:hover {
            color: #0073aa;
        }
        
        .chapter-volume,
        .chapter-number {
            margin-right: 5px;
            font-weight: bold;
        }
        
        .chapter-status {
            font-size: 0.8em;
            color: #999;
        }
        
        .reading-mode-link {
            text-align: center;
            margin-top: 20px;
        }
        
        .enter-reading-mode, .exit-reading-mode {
            display: inline-block;
            padding: 10px 20px;
            background: #0073aa;
            color: white;
            text-decoration: none;
            border-radius: 3px;
            transition: background 0.3s ease;
        }
        
        .enter-reading-mode:hover, .exit-reading-mode:hover {
            background: #005a87;
        }
        
        /* 阅读模式样式 */
        .reading-mode-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid #ddd;
        }
        
        .reading-mode-header h1 {
            margin: 0;
        }
        
        .reading-mode-content {
            font-size: 18px;
            line-height: 1.8;
        }
        
        .reading-mode-content h2, 
        .reading-mode-content h3 {
            margin: 30px 0 15px 0;
            padding-bottom: 10px;
            border-bottom: 1px solid #eee;
        }
        
        .reading-mode-content p {
            margin-bottom: 20px;
            text-align: justify;
        }
        
        .ancient-text-content {
            margin-bottom: 40px;
        }
        
        .ancient-text-content h2 {
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }
        
        .content-text {
            font-size: 16px;
            line-height: 1.8;
        }
        
        .content-text p {
            margin-bottom: 20px;
            text-align: justify;
        }
        
        .default-content-text {
            font-size: 16px;
            line-height: 1.8;
            margin-top: 20px;
        }
        
        .default-content-text p {
            margin-bottom: 20px;
            text-align: justify;
        }
        
        .ancient-text-navigation {
            border-top: 1px solid #ddd;
            padding-top: 20px;
        }
        
        .nav-links {
            display: flex;
            justify-content: space-between;
        }
        
        .nav-links a {
            text-decoration: none;
            padding: 10px 15px;
            background: #0073aa;
            color: white;
            border-radius: 3px;
        }
        
        .nav-links a:hover {
            background: #005a87;
        }
        
        @media (max-width: 768px) {
            .toc-columns-container {
                flex-direction: column;
                gap: 0;
            }
            
            .reading-mode-header {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }
            
            .nav-links {
                flex-direction: column;
                gap: 10px;
            }
            
            .nav-links a {
                text-align: center;
            }
        }
        </style>
        
    <?php endwhile; ?>
<?php endif; ?>

<?php
get_footer();