<?php
if (! defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}
$blog_style = get_theme_mod('welding_for_elementor_blog_style');
?>
<?php if ($blog_style === 'list') : ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('col-md-10 mb-30') ?>>
        <div class="card tr-blog-card tr-radius fix">
            <div class="row g-0">
                <div class="col-md-4">
                    <div class="tr-post-thumbnail h-100">
                        <?php if (has_post_thumbnail()) : ?>
                            <?php the_post_thumbnail('medium_large', array('class' =>'img-fluid w-100 h-100 object-fit-cover', 'alt' => get_the_title())); ?>
                        <?php endif; ?>
                    </div> <!-- tr-post-thumbnail -->
                </div> <!-- col-md-4 -->
                <div class="col-md-8">
                    <div class="card-body p-24">
                        <span class="tr-blog-category">
                            <?php
                            $categories = get_the_category();
                            if (! empty($categories)) {
                                echo '<a href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>';
                            } ?>
                        </span> <!-- tr-blog-category -->
                        <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> <!-- tr-blog-reading-time -->
                        <h2 class="">
                            <a href="<?php the_permalink(); ?>">
                                <?php the_title(); ?>
                            </a>
                        </h2>
                        <div class="tr-blog-card-body-text"><?php the_excerpt();?></div>
                        <div class="tr-blog-last-row pt-16 mt-16">
                            <span class="tr-blog-date"><?php echo esc_html(get_the_date()); ?></span>
                            <a href="<?php the_permalink(); ?>" class="tr-blog-read-more"><?php echo esc_html__('Read more', 'welding-for-elementor') ?><i class="fa fa-arrow-right ml-4" aria-hidden="true"></i></a>
                        </div> <!-- tr-blog-last-row -->
                    </div> <!-- card-body -->
                </div> <!-- col-md-8 -->
            </div> <!-- row -->
        </div> <!-- card -->
    </article>

<?php else: ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('col-lg-4 mb-30 masonry-item') ?>>
        <div class="card tr-blog-card tr-radius fix">
            <div class="tr-post-thumbnail">
                <?php if (has_post_thumbnail()) : ?>
                    <?php the_post_thumbnail('medium_large', array('class' => 'img-fluid', 'alt' => get_the_title())); ?>
                <?php endif; ?>
            </div>
            <div class="card-body p-24">
                <span class="tr-blog-category">
                    <?php
                    $categories = get_the_category();
                    if (! empty($categories)) {
                        echo '<a href="' . esc_url(get_category_link($categories[0]->term_id)) . '">' . esc_html($categories[0]->name) . '</a>';
                    } ?>
                </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>
                <h2 class="">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h2>
                <div class="tr-blog-card-body-text"><?php the_excerpt(); ?></div>
                <div class="tr-blog-last-row pt-16 mt-16">
                    <span class="tr-blog-date"><?php echo esc_html(get_the_date()); ?></span>
                    <a href="<?php the_permalink(); ?>" class="tr-blog-read-more"><?php echo esc_html__('Read more', 'welding-for-elementor') ?><i class="fa fa-arrow-right ml-4" aria-hidden="true"></i></a>
                </div>
            </div>
        </div>
    </article>

<?php
endif; ?>