<?php
/**
 * The template for displaying single posts and pages.
 * @package Maintenance Works
 * @since 1.0.0
 */

get_header();

$maintenance_works_default = maintenance_works_get_default_theme_options();
$maintenance_works_global_layout = get_theme_mod('maintenance_works_global_sidebar_layout', $maintenance_works_default['maintenance_works_global_sidebar_layout']);
$maintenance_works_page_layout = get_theme_mod('maintenance_works_page_sidebar_layout', $maintenance_works_global_layout);
$maintenance_works_post_layout = get_theme_mod('maintenance_works_post_sidebar_layout', $maintenance_works_global_layout);
$maintenance_works_post_meta = get_post_meta(get_the_ID(), 'maintenance_works_post_sidebar_option', true);

$maintenance_works_final_layout = $maintenance_works_global_layout;
if (!empty($maintenance_works_post_meta) && $maintenance_works_post_meta !== 'default') {
    $maintenance_works_final_layout = $maintenance_works_post_meta;
} elseif (is_page() || (function_exists('is_shop') && is_shop())) {
    $maintenance_works_final_layout = $maintenance_works_page_layout;
} elseif (is_single()) {
    $maintenance_works_final_layout = $maintenance_works_post_layout;
}

// Set content column order based on sidebar layout
$maintenance_works_sidebar_column_class = 'column-order-1';
if ($maintenance_works_final_layout === 'left-sidebar') {
    $maintenance_works_sidebar_column_class = 'column-order-2';
}

?>

<?php if (!is_front_page()) : get_template_part('template-parts/page-banner'); endif; ?>
<div id="single-page" class="singular-main-block">
    <div class="wrapper">
        <div class="column-row <?php echo esc_attr($maintenance_works_final_layout === 'no-sidebar' ? 'no-sidebar-layout' : ''); ?>">

            <?php if ($maintenance_works_final_layout === 'left-sidebar') : ?>
                <?php get_sidebar(); ?>
            <?php endif; ?>

            <div id="primary" class="content-area <?php echo esc_attr($maintenance_works_final_layout === 'no-sidebar' ? 'full-width-content' : $maintenance_works_sidebar_column_class); ?>">
                <main id="site-content" role="main">

                    <?php
                    if (have_posts()) : ?>

                        <div class="article-wraper">
                            <?php while (have_posts()) : the_post(); ?>

                                <?php get_template_part('template-parts/content', 'single'); ?>

                                <?php if ((is_single() || is_page()) && (comments_open() || get_comments_number()) && !post_password_required()) : ?>
                                    <div class="comments-wrapper">
                                        <?php comments_template(); ?>
                                    </div>
                                <?php endif; ?>

                            <?php endwhile; ?>
                        </div>

                    <?php else : ?>

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

                    <?php endif;

                    do_action('maintenance_works_navigation_action');
                    ?>

                </main>
            </div>

            <?php if ($maintenance_works_final_layout === 'right-sidebar') : ?>
                <?php get_sidebar(); ?>
            <?php endif; ?>

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

<?php get_footer(); ?>