<?php
/**
 * 古籍章节详情页面模板
 */

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

get_header();

// 检查是否是阅读模式
$reading_mode = isset($_GET['mode']) && $_GET['mode'] === 'reading';

// 如果是阅读模式，加载专门的阅读模板
if ($reading_mode) {
    include(plugin_dir_path(__FILE__) . 'single-ancient-text-chapter-reading-mode.php');
    exit;
}

if (have_posts()) :
    while (have_posts()) : the_post();
        $ancient_text_id = get_post_meta(get_the_ID(), '_chapter_ancient_text', true);
        $volume = get_post_meta(get_the_ID(), '_chapter_volume', true);
        $chapter_number = get_post_meta(get_the_ID(), '_chapter_number', true);
        $ancient_text = get_post($ancient_text_id);
        ?>
        
        <div class="wrap ancient-text-chapter-single">
            <div class="chapter-header">
                <?php if ($ancient_text && $ancient_text->post_status === 'publish'): ?>
                    <div class="breadcrumb">
                        <a href="<?php echo get_post_type_archive_link('chinese_ancient_text'); ?>">古籍</a> > 
                        <a href="<?php echo get_permalink($ancient_text_id); ?>"><?php echo esc_html($ancient_text->post_title); ?></a> > 
                        <span><?php the_title(); ?></span>
                    </div>
                <?php endif; ?>
                
                <h1><?php the_title(); ?></h1>
                
                <div class="chapter-meta">
                    <table>
                        <?php if (!empty($volume)): ?>
                            <tr>
                                <th>卷数：</th>
                                <td><?php echo esc_html($volume); ?></td>
                            </tr>
                        <?php endif; ?>
                        
                        <?php if (!empty($chapter_number)): ?>
                            <tr>
                                <th>章节编号：</th>
                                <td><?php echo esc_html($chapter_number); ?></td>
                            </tr>
                        <?php endif; ?>
                        
                        <?php if ($ancient_text && $ancient_text->post_status === 'publish'): ?>
                            <tr>
                                <th>所属古籍：</th>
                                <td><a href="<?php echo get_permalink($ancient_text_id); ?>"><?php echo esc_html($ancient_text->post_title); ?></a></td>
                            </tr>
                        <?php endif; ?>
                        
                        <tr>
                            <th>阅读模式：</th>
                            <td>
                                <a href="?mode=reading" class="btn-enter-reading-mode">进入专业阅读模式</a>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            
            <div class="chapter-content">
                <?php the_content(); ?>
            </div>
            
            <div class="chapter-navigation">
                <?php
                // 获取同一古籍下的所有章节
                if ($ancient_text_id) {
                    $all_chapters = get_posts(array(
                        'post_type' => 'ancient_text_chapter',
                        'post_status' => 'publish',
                        'meta_query' => array(
                            array(
                                'key' => '_chapter_ancient_text',
                                'value' => $ancient_text_id,
                                'compare' => '='
                            )
                        ),
                        'orderby' => 'menu_order',
                        'order' => 'ASC',
                        'posts_per_page' => -1
                    ));
                    
                    $current_index = array_search(get_the_ID(), wp_list_pluck($all_chapters, 'ID'));
                    
                    if ($current_index !== false) {
                        $prev_chapter = isset($all_chapters[$current_index - 1]) ? $all_chapters[$current_index - 1] : null;
                        $next_chapter = isset($all_chapters[$current_index + 1]) ? $all_chapters[$current_index + 1] : null;
                        ?>
                        <div class="nav-links">
                            <?php if ($prev_chapter): ?>
                                <div class="nav-previous">
                                    <a href="<?php echo get_permalink($prev_chapter->ID); ?>">&larr; <?php echo esc_html($prev_chapter->post_title); ?></a>
                                </div>
                            <?php endif; ?>
                            
                            <?php if ($next_chapter): ?>
                                <div class="nav-next">
                                    <a href="<?php echo get_permalink($next_chapter->ID); ?>"><?php echo esc_html($next_chapter->post_title); ?> &rarr;</a>
                                </div>
                            <?php endif; ?>
                        </div>
                        <?php
                    }
                }
                ?>
            </div>
        </div>
        
        <style>
        .ancient-text-chapter-single {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .breadcrumb {
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .breadcrumb a {
            color: #0073aa;
            text-decoration: none;
        }
        
        .breadcrumb a:hover {
            text-decoration: underline;
        }
        
        .chapter-header h1 {
            text-align: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        
        .chapter-meta {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 5px;
            margin-bottom: 30px;
        }
        
        .chapter-meta table {
            width: 100%;
        }
        
        .chapter-meta th {
            text-align: right;
            padding-right: 15px;
            width: 100px;
            color: #333;
        }
        
        .chapter-meta td {
            padding: 5px 0;
        }
        
        .btn-enter-reading-mode {
            display: inline-block;
            padding: 8px 15px;
            background: #0073aa;
            color: white;
            text-decoration: none;
            border-radius: 3px;
            transition: background 0.3s ease;
            font-size: 14px;
        }
        
        .btn-enter-reading-mode:hover {
            background: #005a87;
        }
        
        .chapter-content {
            font-size: 16px;
            line-height: 1.8;
            margin-bottom: 40px;
        }
        
        .chapter-content p {
            margin-bottom: 20px;
            text-align: justify;
        }
        
        .chapter-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) {
            .nav-links {
                flex-direction: column;
                gap: 10px;
            }
            
            .nav-links > div {
                width: 100%;
            }
            
            .nav-links a {
                display: block;
                text-align: center;
            }
        }
        </style>
        
    <?php endwhile; ?>
<?php endif; ?>

<?php
get_footer();