<?php
/**
 * Legacy: Blog Grid Template (Deprecated)
 *
 * This file is kept for backward compatibility only.
 * Please use "Blog Custom Layout" template for new pages.
 * New template file: template-blog-custom.php
 *
 * @package Blog_Over
 */


get_header();

// Get page ID
$page_id = get_the_ID();

// Get template settings with defaults
$sidebar_position = get_post_meta($page_id, '_blog_over_template_sidebar', true);
$sidebar_position = $sidebar_position ?: 'right-sidebar';

$blog_style = get_post_meta($page_id, '_blog_over_template_blog_style', true);
$blog_style = $blog_style ?: 'default';

$grid_columns = get_post_meta($page_id, '_blog_over_template_grid_columns', true);
$grid_columns = $grid_columns ?: 3;

$posts_per_page = get_post_meta($page_id, '_blog_over_template_posts_per_page', true);
$posts_per_page = $posts_per_page ?: 9;

$category_filter = get_post_meta($page_id, '_blog_over_template_categories', true);
$category_filter = $category_filter ?: '';

$order_by = get_post_meta($page_id, '_blog_over_template_orderby', true);
$order_by = $order_by ?: 'date';

$order = get_post_meta($page_id, '_blog_over_template_order', true);
$order = $order ?: 'DESC';

$show_pagination = get_post_meta($page_id, '_blog_over_template_show_pagination', true);
$show_pagination = $show_pagination !== '0'; // Default true

// Section Width
$section_width = get_post_meta($page_id, '_blog_over_template_section_width', true);
$section_width = $section_width ?: 'boxed';
$container_class = ($section_width === 'full-width') ? 'blog-over-container blog-over-container-wide' : 'blog-over-container';

// Parse layout values
$has_sidebar = ($sidebar_position !== 'no-sidebar');
$sidebar_pos = ($sidebar_position === 'left-sidebar') ? 'left' : 'right';

// Determine if using modern cards
$is_modern = ($blog_style === 'modern-cards');

// Query arguments
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'post',
    'posts_per_page' => intval($posts_per_page),
    'paged' => $paged,
    'orderby' => $order_by,
    'order' => $order,
);

// Add category filter if set
if (!empty($category_filter)) {
    $cats = array_map('intval', explode(',', $category_filter));
    $args['category__in'] = $cats;
}

$blog_query = new WP_Query($args);
?>

<main id="primary" class="blog-over-site-main">
    <div class="blog-over-page-header-container">
        <?php blog_over_page_header(); ?>
    </div>
    <?php if (blog_over_should_render_breadcrumb_container()): ?>
        <div class="blog-over-breadcrumb-container">
            <?php blog_over_breadcrumb(); ?>
        </div>
    <?php endif; ?>

    <div class="<?php echo esc_attr($container_class); ?>">
        <div
            class="blog-over-content-wrapper <?php echo esc_attr($has_sidebar ? 'has-sidebar sidebar-' . $sidebar_pos : 'no-sidebar'); ?>">

            <?php if ($has_sidebar && $sidebar_pos === 'left'): ?>
                <aside class="blog-over-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php endif; ?>

            <div class="blog-over-main-content">
                <?php if ($blog_query->have_posts()): ?>
                    <?php if ($is_modern): ?>
                        <!-- Modern Card Grid Layout -->
                        <?php $masonry_class = (get_theme_mod('blog_over_grid_layout_mode', 'default') === 'masonry') ? ' bo-masonry' : ''; ?>
                        <div class="<?php echo esc_attr( 'blog-over-modern-grid blog-over-grid-cols-' . $grid_columns . $masonry_class ); ?>">
                            <?php
                            while ($blog_query->have_posts()):
                                $blog_query->the_post();
                                get_template_part('template-parts/content', 'modern');
                            endwhile;
                            ?>
                        </div>
                    <?php else: ?>
                        <!-- Default Grid Layout -->
                        <div class="blog-over-posts-grid blog-over-grid-cols-<?php echo esc_attr($grid_columns); ?>">
                            <?php
                            while ($blog_query->have_posts()):
                                $blog_query->the_post();
                                get_template_part('template-parts/content', 'grid');
                            endwhile;
                            ?>
                        </div>
                    <?php endif; ?>

                    <?php if ($show_pagination): ?>
                        <?php 
                        $pagination_html = '<nav class="blog-over-pagination">';
                        $pagination_html .= paginate_links(array(
                            'total' => $blog_query->max_num_pages,
                            'current' => $paged,
                            'mid_size' => 2,
                            'prev_text' => '<i class="fas fa-chevron-left"></i>',
                            'next_text' => '<i class="fas fa-chevron-right"></i>',
                        ));
                        $pagination_html .= '</nav>';

                        // Allow plugins (like Blog Over Pro) to override pagination with infinite scroll / load more
                        echo apply_filters('blog_over_pagination_output', $pagination_html);
                        ?>
                    <?php endif; ?>
                <?php else: ?>
                    <div class="blog-over-no-posts">
                        <p>
                            <?php esc_html_e('No posts found.', 'blog-over'); ?>
                        </p>
                    </div>
                <?php endif; ?>
                <?php wp_reset_postdata(); ?>
            </div>

            <?php if ($has_sidebar && $sidebar_pos === 'right'): ?>
                <aside class="blog-over-sidebar">
                    <?php get_sidebar(); ?>
                </aside>
            <?php endif; ?>

        </div>
    </div>
</main>

<?php
get_footer();
