<?php
/**
 * Template part for displaying post cards in grid layout
 *
 * @package Blog_Over
 */

$image_type = get_theme_mod('blog_over_post_image_type', 'fixed');
$elements = blog_over_get_blog_entry_elements();
$show_thumbnail = get_theme_mod('Blog_Over_show_thumbnail', true);
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('blog-over-grid-item'); ?>>
    <div class="blog-over-grid-card">
        <?php
        // Featured Image - fixed at top, controlled by toggle
        if ($show_thumbnail && has_post_thumbnail()):
            ?>
            <div
                class="blog-over-grid-thumbnail <?php echo esc_attr($image_type === 'fixed' ? 'fixed-height' : 'auto-height'); ?>">
                <a href="<?php the_permalink(); ?>">
                    <?php the_post_thumbnail('blog-over-medium'); ?>
                </a>
            </div>
            <?php
        endif;
        ?>

        <div class="blog-over-grid-content">
            <?php
            // Render elements in saved order
            foreach ($elements as $element):
                switch ($element):
                    case 'category':
                        // Category only shows when thumbnail is enabled
                        if ($show_thumbnail):
                            $categories = get_the_category();
                            if (!empty($categories)):
                                ?>
                                <div class="blog-over-grid-category">
                                    <a href="<?php echo esc_url(get_category_link($categories[0]->term_id)); ?>">
                                        <?php echo esc_html($categories[0]->name); ?>
                                    </a>
                                </div>
                                <?php
                            endif;
                        endif;
                        break;

                    case 'header':
                        ?>
                        <?php the_title('<h3 class="blog-over-grid-title"><a href="' . esc_url(get_permalink()) . '">', '</a></h3>'); ?>
                        <?php
                        break;

                    case 'meta':
                        ?>
                        <div class="blog-over-grid-meta">
                            <?php blog_over_render_entry_meta(); ?>
                        </div>
                        <?php
                        break;

                    case 'summary':
                        ?>
                        <div class="blog-over-grid-excerpt">
                            <?php the_excerpt(); ?>
                        </div>
                        <?php
                        break;

                    case 'footer':
                        ?>
                        <div class="blog-over-grid-footer">
                            <a href="<?php the_permalink(); ?>" class="blog-over-grid-readmore">
                                <?php echo esc_html(get_theme_mod('blog_over_read_more_text', __('Read More', 'blog-over'))); ?>
                            </a>
                        </div>
                        <?php
                        break;
                endswitch;
            endforeach;
            ?>
        </div>
    </div>
</article>