<?php
if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
$sidebar_position = get_theme_mod('welding_for_elementor_blog_sidebar_position', 'right');

get_header();
?>

<section>
    <div class="container pt-100" id="main-content">
        <div class="row justify-content-center">
            <?php if (is_active_sidebar('blog-sidebar')): ?>
                <?php if ($sidebar_position === 'left'): ?>
                    <div class="col-12 col-lg-4">
                        <?php get_sidebar(); ?>
                    </div>
                <?php endif; ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class('col-12 col-lg-8 mb-40') ?>>
                <?php else: ?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class('col-12 mb-40') ?>>
                    <?php endif; ?>
                    <div id="tr-post-content" class="fix mb-50">
                        <h1 class="tr-blog-title">
                            <?php the_title(); ?>
                        </h1>
                        <div class="d-flex align-items-center mb-20">
                            <div class="flex-shrink-0 tr-blog-avatar">
                                <?php echo get_avatar(get_the_author_meta('ID'), 42); ?>
                            </div>
                            <div class="flex-grow-1 ml-10">
                                <div class="tr-blog-author">
                                    <?php the_author_posts_link(); ?>
                                </div>
                                <div>
                                    <span class="tr-blog-date">
                                        <?php echo esc_html(get_the_date()); ?>
                                    </span>
                                    <span class="tr-blog-reading-time ml-10">
                                        <i class="fa fa-clock-o" aria-hidden="true"></i>
                                        <?php
                                        $content = get_post_field('post_content', get_the_ID());
                                        $plain_text = wp_strip_all_tags($content);
                                        $word_count = str_word_count($plain_text);
                                        $reading_time = max(1, (int) ceil($word_count / 200));
                                        echo esc_html($reading_time) . esc_html__(' min read', 'welding-for-elementor');
                                        ?>
                                    </span>
                                </div>
                            </div>
                        </div>
                        <div class="d-flex justify-content-center mb-30">
                            <?php if (has_post_thumbnail()) : ?>
                                <?php the_post_thumbnail('full', array('class' => 'img-fluid tr-radius', 'alt' => get_the_title())); ?>
                            <?php endif; ?>
                        </div>
                        <div class="">
                            <div class="">
                                <?php
                                the_content();
                                wp_link_pages([
                                    'next_or_number' => 'next',
                                    'previouspagelink' => esc_html__('« Prev', 'welding-for-elementor'),
                                    'nextpagelink' => esc_html__('Next »', 'welding-for-elementor'),
                                ]);
                                ?>
                            </div>
                        </div>
                    </div>
                    <hr>
                    <?php
                    // Display comments section if comments are open or exist
                    if (comments_open() || get_comments_number()) :
                        comments_template();
                    endif;
                    ?>
                    </article>
                    <?php if ($sidebar_position === 'right'): ?>
                        <?php if (is_active_sidebar('blog-sidebar')): ?>
                            <div class="col-12 col-lg-4">
                                <?php get_sidebar(); ?>
                            </div>
                        <?php endif; ?>
                    <?php endif; ?>
        </div>
    </div>
</section>

<?php
get_footer();
