<?php
// Determine the page layout based on the context (archive, author, search, or blog)
$page_layout = is_author() ? glamer_options('glamer_authors_layout', 'right-sidebar') :
    (is_archive() ? glamer_options('glamer_archive_layout') : 
    (is_search() ? glamer_options('glamer_search_layout', 'right-sidebar') : 
    glamer_options('glamer_blog_layout', 'right-sidebar')));

// Determine the post list style based on the context (archive, author, search, or blog)
$post_list_style = is_author() ? glamer_options('glamer_author_sidebar_style', 'one') :
    (is_archive() ? glamer_options('glamer_archive_sidebar_style', 'one') : 
    (is_search() ? glamer_options('glamer_search_sidebar_style', 'one') : 
    glamer_options('glamer_blog_sidebar_style', 'one')));

// Allow layout override through the URL parameter 'bs'
$blog_layout_overwrite = $_GET['bs'] ?? $page_layout;
$post_list_style_overwrite = $_GET['style'] ?? $post_list_style;

// Recalculate the column class based on the overridden layout and sidebar
$page_column_class = (in_array($blog_layout_overwrite, ['left-sidebar', 'right-sidebar']) && is_active_sidebar('sidebar')) 
    ? 'col-lg-12 col-xl-8'
    : 'col-lg-12';

// Pagination option
$show_pagination = glamer_options('glamer_show_pagination', true);
?>

<div class="row content-<?php echo esc_attr($blog_layout_overwrite); ?> list-style-v-<?php echo esc_attr($post_list_style_overwrite); ?>">
    <?php if (in_array($blog_layout_overwrite, ['left-sidebar']) && is_active_sidebar('sidebar')): ?>
        <?php get_sidebar(); ?>
    <?php endif; ?>

    <div class="<?php echo esc_attr($page_column_class); ?>">
        <div class="row all-posts-wrapper">
            <?php if (have_posts()) : ?>
                <?php if (is_home() && !is_front_page()) : ?>
                    <header>
                        <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
                    </header>
                <?php endif; ?>

                <?php while (have_posts()) : the_post(); ?>
                    <?php get_template_part('template-parts/post/post-list/list-style-'.$post_list_style_overwrite.''); ?>
                <?php endwhile; ?>

            <?php else : ?>
                <?php get_template_part('template-parts/content', 'none'); ?>
            <?php endif; ?>
        </div>

        <?php if ($show_pagination) : ?>
            <?php get_template_part('template-parts/post/post-pagination'); ?>
        <?php endif; ?>
    </div>

    <?php if (in_array($blog_layout_overwrite, ['right-sidebar']) && is_active_sidebar('sidebar')): ?>
        <?php get_sidebar(); ?>
    <?php endif; ?>
</div>