<?php
/**
 * 单个博物馆页面模板
 */

get_header();

// 获取当前博物馆信息
$museum = get_post();
$location = get_post_meta($museum->ID, '_museum_location', true);
$founded_year = get_post_meta($museum->ID, '_museum_founded_year', true);
$director = get_post_meta($museum->ID, '_museum_director', true);
$website = get_post_meta($museum->ID, '_museum_website', true);

// 获取关联的藏品并按类别分组
$collections = get_posts(array(
    'post_type' => 'chinese_collection',
    'post_status' => 'publish',
    'numberposts' => -1,
    'meta_query' => array(
        array(
            'key' => '_collection_related_museum',
            'value' => $museum->ID,
            'compare' => '='
        )
    )
));

// 按类别分组藏品
$collections_by_type = array();
$collection_types = array('书法', '绘画', '藏品', '其他');

foreach ($collections as $collection) {
    $type = get_post_meta($collection->ID, '_collection_type', true);
    if (empty($type)) {
        $type = '其他';
    }
    
    if (!isset($collections_by_type[$type])) {
        $collections_by_type[$type] = array();
    }
    
    $collections_by_type[$type][] = $collection;
}

// 获取与该博物馆关联的书法作品
$calligraphies = get_posts(array(
    'post_type' => 'chinese_calligraphy',
    'post_status' => 'publish',
    'numberposts' => -1,
    'meta_query' => array(
        array(
            'key' => '_calligraphy_collection',
            'value' => $museum->post_title,
            'compare' => '='
        )
    )
));

// 将书法作品添加到书法类别中
if (!empty($calligraphies)) {
    if (!isset($collections_by_type['书法'])) {
        $collections_by_type['书法'] = array();
    }
    
    // 将书法作品合并到书法类别中
    $collections_by_type['书法'] = array_merge($collections_by_type['书法'], $calligraphies);
}
?>

<div class="museum-container">
    <div class="museum-header">
        <?php if (has_post_thumbnail()): ?>
            <div class="museum-thumbnail">
                <?php the_post_thumbnail('large'); ?>
            </div>
        <?php endif; ?>
        
        <h1 class="museum-title"><?php the_title(); ?></h1>
        
        <div class="museum-meta">
            <?php if ($location): ?>
                <div class="museum-location">
                    <strong>位置：</strong><?php echo esc_html($location); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($founded_year): ?>
                <div class="museum-founded-year">
                    <strong>建立年份：</strong><?php echo esc_html($founded_year); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($director): ?>
                <div class="museum-director">
                    <strong>馆长：</strong><?php echo esc_html($director); ?>
                </div>
            <?php endif; ?>
            
            <?php if ($website): ?>
                <div class="museum-website">
                    <strong>官网：</strong><a href="<?php echo esc_url($website); ?>" target="_blank"><?php echo esc_html($website); ?></a>
                </div>
            <?php endif; ?>
        </div>
    </div>
    
    <div class="museum-content">
        <?php the_content(); ?>
    </div>
    
    <?php if (!empty($collections_by_type)): ?>
        <div class="museum-collections">
            <h2>馆藏精品</h2>
            
            <?php foreach ($collection_types as $type): ?>
                <?php if (isset($collections_by_type[$type]) && !empty($collections_by_type[$type])): ?>
                    <div class="collections-section">
                        <h3><?php echo esc_html($type); ?></h3>
                        <div class="collections-grid">
                            <?php foreach ($collections_by_type[$type] as $collection): 
                                // 检查是藏品还是书法作品
                                $is_calligraphy = ($collection->post_type === 'chinese_calligraphy');
                                
                                if ($is_calligraphy) {
                                    // 处理书法作品
                                    $dynasty = get_post_meta($collection->ID, '_calligraphy_dynasty', true);
                                    $style = get_post_meta($collection->ID, '_calligraphy_style', true);
                                    $character = get_post_meta($collection->ID, '_calligraphy_character', true);
                                    $character_images = get_post_meta($collection->ID, '_calligraphy_character_images', true);
                                    $full_work_images = get_post_meta($collection->ID, '_calligraphy_full_work', true);
                                    
                                    // 确定要显示的图片
                                    $display_image = null;
                                    if (!empty($character_images) && is_array($character_images)) {
                                        $display_image = $character_images[0]; // 使用第一张单字字形图片
                                    } elseif (!empty($full_work_images) && is_array($full_work_images)) {
                                        $display_image = $full_work_images[0]; // 使用第一张完整作品图片
                                    } elseif (has_post_thumbnail($collection->ID)) {
                                        $display_image = get_post_thumbnail_id($collection->ID); // 使用特色图片
                                    }
                                } else {
                                    // 处理藏品
                                    $dynasty = get_post_meta($collection->ID, '_collection_dynasty', true);
                                    $category = get_post_meta($collection->ID, '_collection_category', true);
                                    $display_image = has_post_thumbnail($collection->ID) ? get_post_thumbnail_id($collection->ID) : null;
                                }
                            ?>
                                <div class="collection-item">
                                    <a href="<?php echo get_permalink($collection->ID); ?>">
                                        <?php if ($display_image): ?>
                                            <?php echo wp_get_attachment_image($display_image, 'medium'); ?>
                                        <?php else: ?>
                                            <div class="no-image">暂无图片</div>
                                        <?php endif; ?>
                                        <h4><?php echo get_the_title($collection->ID); ?></h4>
                                        <?php if ($is_calligraphy): ?>
                                            <?php if ($dynasty): ?>
                                                <p class="collection-dynasty"><?php echo esc_html($dynasty); ?></p>
                                            <?php endif; ?>
                                            <?php if ($style): ?>
                                                <p class="collection-style"><?php echo esc_html($style); ?></p>
                                            <?php endif; ?>
                                            <?php if ($character): ?>
                                                <p class="collection-character">字: <?php echo esc_html($character); ?></p>
                                            <?php endif; ?>
                                        <?php else: ?>
                                            <?php if ($dynasty): ?>
                                                <p class="collection-dynasty"><?php echo esc_html($dynasty); ?></p>
                                            <?php endif; ?>
                                            <?php if ($category): ?>
                                                <p class="collection-category"><?php echo esc_html($category); ?></p>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </a>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endif; ?>
            <?php endforeach; ?>
        </div>
    <?php endif; ?>
</div>

<style>
.museum-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.museum-header {
    text-align: center;
    margin-bottom: 30px;
    border-bottom: 1px solid #eee;
    padding-bottom: 20px;
}

.museum-thumbnail img {
    max-width: 100%;
    height: auto;
    border-radius: 5px;
}

.museum-title {
    margin: 20px 0 15px;
    font-size: 2.5em;
    color: #333;
}

.museum-meta {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
}

.museum-meta > div {
    background: #f9f9f9;
    padding: 10px 15px;
    border-radius: 5px;
    font-size: 0.9em;
}

.museum-content {
    font-size: 1.1em;
    line-height: 1.6;
    margin-bottom: 40px;
}

.museum-collections h2 {
    text-align: center;
    margin-bottom: 30px;
    font-size: 2em;
    color: #333;
}

.collections-section {
    margin-bottom: 40px;
}

.collections-section h3 {
    font-size: 1.5em;
    color: #555;
    border-left: 4px solid #0073aa;
    padding-left: 10px;
    margin-bottom: 20px;
}

.collections-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 25px;
}

.collection-item {
    border: 1px solid #ddd;
    border-radius: 5px;
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

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

.collection-item a {
    text-decoration: none;
    color: inherit;
    display: block;
}

.collection-item img {
    width: 100%;
    height: 200px;
    object-fit: cover;
}

.collection-item .no-image {
    height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    color: #999;
}

.collection-item h4 {
    padding: 15px 10px 5px;
    margin: 0;
    font-size: 1.1em;
}

.collection-item p {
    padding: 5px 10px;
    margin: 0;
    font-size: 0.9em;
    color: #666;
}

.collection-dynasty:before {
    content: "朝代: ";
}

.collection-category:before {
    content: "分类: ";
}

.collection-style:before {
    content: "书体: ";
}

.collection-character:before {
    content: "汉字: ";
}

@media (max-width: 768px) {
    .museum-meta {
        flex-direction: column;
        align-items: center;
        gap: 10px;
    }
    
    .collections-grid {
        grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    }
}
</style>

<?php
get_footer();