<?php
/**
 * 古籍归档模板
 * <h1>古籍文献</h1>
 */

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

get_header();

// 获取搜索关键词和筛选参数
$search_query = get_search_query();
$category_filter = isset($_GET['category']) ? sanitize_text_field($_GET['category']) : '';
$dynasty_filter = isset($_GET['dynasty']) ? sanitize_text_field($_GET['dynasty']) : '';
$author_filter = isset($_GET['author']) ? sanitize_text_field($_GET['author']) : '';

// 获取所有古籍
$args = array(
    'post_type' => 'chinese_ancient_text',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC'
);

// 如果有搜索关键词，则添加搜索条件
if (!empty($search_query)) {
    $args['s'] = $search_query;
}

// 构建元查询条件
$meta_query = array();

// 如果有经史子集分类筛选，则添加元查询条件
if (!empty($category_filter)) {
    $meta_query[] = array(
        'key' => '_ancient_text_category',
        'value' => $category_filter,
        'compare' => '='
    );
}

// 如果有朝代筛选，则添加元查询条件
if (!empty($dynasty_filter)) {
    $meta_query[] = array(
        'key' => '_ancient_text_dynasty',
        'value' => $dynasty_filter,
        'compare' => '='
    );
}

// 如果有作者筛选，则添加元查询条件
if (!empty($author_filter)) {
    $meta_query[] = array(
        'key' => '_ancient_text_author',
        'value' => $author_filter,
        'compare' => '='
    );
}

// 如果有元查询条件，则添加到查询参数中
if (!empty($meta_query)) {
    // 如果有多个条件，需要使用AND关系
    if (count($meta_query) > 1) {
        $meta_query['relation'] = 'AND';
    }
    $args['meta_query'] = $meta_query;
}

$ancient_texts = new WP_Query($args);

// 获取所有朝代、作者和经史子集分类
$dynasties = array();
$authors = array();
$categories = array();

if ($ancient_texts->have_posts()) {
    while ($ancient_texts->have_posts()) {
        $ancient_texts->the_post();
        $dynasty = get_post_meta(get_the_ID(), '_ancient_text_dynasty', true);
        $author = get_post_meta(get_the_ID(), '_ancient_text_author', true);
        $category = get_post_meta(get_the_ID(), '_ancient_text_category', true);
        
        if (!empty($dynasty) && !in_array($dynasty, $dynasties)) {
            $dynasties[] = $dynasty;
        }
        
        if (!empty($author) && !in_array($author, $authors)) {
            $authors[] = $author;
        }
        
        if (!empty($category) && !in_array($category, $categories)) {
            $categories[] = $category;
        }
    }
    rewind_posts();
}
?>

<div class="wrap ancient-text-archive">
    <!-- <h1>古籍文献</h1> -->
    
   <!-- <?php //if (current_user_can('edit_posts')): ?>
        <div class="ancient-text-admin-notice">
            <p><strong>管理提示：</strong>您可以为每部古籍创建章节内容。前往 <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>。</p>
        </div>
    <?php //endif; ?> -->
    
   <div class="wrap ancient-text-archive">
   	<table>
   		<tr>
   			<td>
   				
    <?php if (!empty($dynasties) || !empty($authors) || !empty($categories)): ?>
    <div class="ancient-text-filters">
        <form method="get" action="<?php echo esc_url(get_post_type_archive_link('chinese_ancient_text')); ?>">
            <?php if (!empty($dynasties)): ?>
            <div class="filter-group">
                <label for="filter-dynasty">朝代：</label>
                <select id="filter-dynasty" name="dynasty">
                    <option value="">全部朝代</option>
                    <?php foreach ($dynasties as $dynasty): ?>
                        <option value="<?php echo esc_attr($dynasty); ?>" <?php selected($dynasty_filter, $dynasty); ?>><?php echo esc_html($dynasty); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($authors)): ?>
            <div class="filter-group">
                <label for="filter-author">作者：</label>
                <select id="filter-author" name="author">
                    <option value="">全部作者</option>
                    <?php foreach ($authors as $author): ?>
                        <option value="<?php echo esc_attr($author); ?>" <?php selected($author_filter, $author); ?>><?php echo esc_html($author); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($categories)): ?>
            <div class="filter-group">
                <label>经史子集：</label>
                <div class="category-filters">
                    <?php 
                    // 构建"全部"分类的链接，保留朝代和作者筛选条件
                    $all_category_url = esc_url(remove_query_arg('category'));
                    if (!empty($dynasty_filter)) {
                        $all_category_url = add_query_arg('dynasty', $dynasty_filter, $all_category_url);
                    }
                    if (!empty($author_filter)) {
                        $all_category_url = add_query_arg('author', $author_filter, $all_category_url);
                    }
                    ?>
                    <a href="<?php echo $all_category_url; ?>" class="category-filter <?php echo empty($category_filter) ? 'active' : ''; ?>">全部</a>
                    <?php foreach ($categories as $category): 
                        // 构建分类链接，保留朝代和作者筛选条件
                        $category_url = esc_url(add_query_arg('category', $category));
                        if (!empty($dynasty_filter)) {
                            $category_url = add_query_arg('dynasty', $dynasty_filter, $category_url);
                        }
                        if (!empty($author_filter)) {
                            $category_url = add_query_arg('author', $author_filter, $category_url);
                        }
                        ?>
                        <a href="<?php echo $category_url; ?>" class="category-filter <?php echo ($category_filter === $category) ? 'active' : ''; ?>"><?php echo esc_html($category); ?></a>
                    <?php endforeach; ?>
                </div>
            </div>
            <?php endif; ?>
            
            <button type="submit">筛选</button>
            
            <?php 
            // 保留搜索关键词
            if (!empty($search_query)) : ?>
                <input type="hidden" name="s" value="<?php echo esc_attr($search_query); ?>">
            <?php endif; ?>
        </form>
    </div>
    <?php endif; ?>
    
   			</td>
   			<td>
             <!-- 添加搜索框 -->
    <div class="ancient-text-search">
        <form method="get" action="<?php echo esc_url(get_post_type_archive_link('chinese_ancient_text')); ?>">
            <!--  <label for="ancient-text-search-input">搜索古籍：</label>  -->
            <input type="text" id="ancient-text-search-input" name="s" value="<?php echo esc_attr($search_query); ?>" placeholder="输入古籍名称...">
            <button type="submit">搜索</button>
            <?php if (!empty($search_query)): ?>
                <a href="<?php echo esc_url(get_post_type_archive_link('chinese_ancient_text')); ?>">显示全部</a>
            <?php endif; ?>
        </form>
        
        <?php if (!empty($search_query)): ?>
            <p>搜索"<?php echo esc_html($search_query); ?>"的结果：共找到 <?php echo $ancient_texts->found_posts; ?> 部古籍</p>
        <?php endif; ?>
    </div>
    </td>
   		</tr>
    </table>
    </div>
    
    <div class="ancient-texts-list">
        <?php if ($ancient_texts->have_posts()): ?>
            <div class="ancient-texts-grid">
                <?php while ($ancient_texts->have_posts()): $ancient_texts->the_post(); ?>
                    <div class="ancient-text-item">
                        <div class="ancient-text-thumbnail">
                            <?php if (has_post_thumbnail()): ?>
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_post_thumbnail('medium'); ?>
                                </a>
                            <?php else: ?>
                                <a href="<?php the_permalink(); ?>">
                                    <div class="no-thumbnail">古籍封面</div>
                                </a>
                            <?php endif; ?>
                        </div>
                        <div class="ancient-text-info">
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                            <?php
                            $dynasty = get_post_meta(get_the_ID(), '_ancient_text_dynasty', true);
                            $author = get_post_meta(get_the_ID(), '_ancient_text_author', true);
                            $volume = get_post_meta(get_the_ID(), '_ancient_text_volume', true);
                            ?>
                            <?php if (!empty($dynasty)): ?>
                                <p class="dynasty">朝代：<?php echo esc_html($dynasty); ?></p>
                            <?php endif; ?>
                            
                            <?php if (!empty($author)): ?>
                                <p class="author">作者：<?php echo esc_html($author); ?></p>
                            <?php endif; ?>
                            
                            <?php if (!empty($volume)): ?>
                                <p class="volume">卷数：<?php echo esc_html($volume); ?></p>
                            <?php endif; ?>
                            
                            <div class="excerpt">
                                <?php the_excerpt(); ?>
                            </div>
                        </div>
                    </div>
                <?php endwhile; ?>
            </div>
        <?php else: ?>
            <p>暂无古籍文献。</p>
            <?php if (!empty($search_query)): ?>
                <p><a href="<?php echo esc_url(get_post_type_archive_link('chinese_ancient_text')); ?>">查看所有古籍</a></p>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

<style>
.ancient-text-archive {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.ancient-text-admin-notice {
    background: #fff8e5;
    border: 1px solid #ffb900;
    border-radius: 4px;
    padding: 12px 16px;
    margin-bottom: 20px;
}

.ancient-text-admin-notice a {
    color: #0073aa;
    text-decoration: underline;
}

.ancient-text-search {
    background: #f9f9f9;
    padding: 20px;
    margin-bottom: 30px;
    border-radius: 5px;
}

.ancient-text-search form {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.ancient-text-search label {
    font-weight: bold;
}

.ancient-text-search input[type="text"] {
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    min-width: 300px;
}

.ancient-text-search button {
    padding: 8px 15px;
    background: #0073aa;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.ancient-text-search button:hover {
    background: #005a87;
}

.ancient-text-search a {
    color: #0073aa;
    text-decoration: underline;
}

.ancient-text-toc {
    background: #f9f9f9;
    padding: 20px;
    margin-bottom: 30px;
    border-radius: 5px;
    border: 1px solid #ddd;
}

.ancient-text-toc h2 {
    margin-top: 0;
    border-bottom: 1px solid #ddd;
    padding-bottom: 10px;
}

.toc-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.toc-item {
    padding: 8px 0;
    border-bottom: 1px solid #eee;
}

.toc-item:last-child {
    border-bottom: none;
}

.toc-link {
    text-decoration: none;
    color: #333;
    display: block;
    transition: color 0.3s ease;
}

.toc-link:hover {
    color: #0073aa;
}

.toc-meta {
    font-size: 0.9em;
    color: #666;
    margin-left: 5px;
}

.ancient-text-filters {
    background: #f9f9f9;
    padding: 20px;
    margin-bottom: 30px;
    border-radius: 5px;
}

.ancient-text-filters h2 {
    margin-top: 0;
}

.filter-group {
    display: inline-block;
    margin-right: 20px;
    margin-bottom: 10px;
}

.filter-group label {
    font-weight: bold;
    margin-right: 5px;
}

.ancient-texts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 30px;
}

.ancient-text-item {
    border: 1px solid #ddd;
    border-radius: 5px;
    overflow: hidden;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.ancient-text-item:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.ancient-text-thumbnail {
    height: 200px;
    overflow: hidden;
    background: #f0f0f0;
}

.ancient-text-thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.ancient-text-thumbnail .no-thumbnail {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    background: #e0e0e0;
    color: #666;
    font-size: 18px;
}

.ancient-text-info {
    padding: 15px;
}

.ancient-text-info h3 {
    margin-top: 0;
    margin-bottom: 10px;
}

.ancient-text-info h3 a {
    text-decoration: none;
    color: #333;
}

.ancient-text-info h3 a:hover {
    color: #0073aa;
}

.ancient-text-info p {
    margin: 5px 0;
    font-size: 14px;
}

.ancient-text-info .dynasty,
.ancient-text-info .author,
.ancient-text-info .volume {
    color: #666;
}

.excerpt {
    margin-top: 10px;
    color: #555;
    font-size: 14px;
}

@media (max-width: 768px) {
    .ancient-texts-grid {
        grid-template-columns: repeat(auto-fill, minmax(100%, 1fr));
    }
    
    .filter-group {
        display: block;
        margin-bottom: 15px;
    }
    
    .ancient-text-search input[type="text"] {
        min-width: 200px;
    }
}

/* 经史子集筛选按钮样式 */
.category-filters {
    display: inline-block;
}

.category-filter {
    display: inline-block;
    padding: 5px 10px;
    margin-right: 5px;
    margin-bottom: 5px;
    text-decoration: none;
    color: #333;
    background: #f0f0f0;
    border: 1px solid #ddd;
    border-radius: 3px;
    transition: all 0.3s ease;
}

.category-filter:hover {
    background: #0073aa;
    color: white;
    border-color: #0073aa;
}

.category-filter.active {
    background: #0073aa;
    color: white;
    border-color: #0073aa;
}
</style>

<?php
wp_reset_postdata();
get_footer();