<?php
/**
 * You May Like Blocks
 *
 * @package Vintuna
 */
if (!function_exists('vintuna_you_may_like_block_section')) :
    function vintuna_you_may_like_block_section($vintuna_home_section, $repeat_times)
    {
        $section_category       = esc_html(isset($vintuna_home_section->section_category) ? $vintuna_home_section->section_category : '');
        $home_section_title     = isset($vintuna_home_section->home_section_title) ? $vintuna_home_section->home_section_title : '';
        $section_post_offset    = isset($vintuna_home_section->section_post_offset) ? absint($vintuna_home_section->section_post_offset) : 0;
        $section_view_more_title = esc_html(isset($vintuna_home_section->section_view_more_title) ? $vintuna_home_section->section_view_more_title : '');

        $you_may_like_post_query = new WP_Query(array(
            'post_type'      => 'post',
            'posts_per_page' => 12,
            'post__not_in'   => get_option("sticky_posts"),
            'offset'         => $section_post_offset,
            'category_name'  => esc_html($section_category),
        ));

        $category_id   = get_cat_ID($section_category);
        $category_link = get_category_link($category_id);

        if ($you_may_like_post_query->have_posts()) : ?>

            <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-block-recommended">

                <?php if ($home_section_title || $section_category) : ?>
                    <header class="theme-block-header">
                        <h2 class="theme-block-title">
                            <?php echo esc_html($home_section_title); ?>
                        </h2>
                        <span class="block-title-dash"></span>

                        <?php if (!empty($category_link) && !empty($section_view_more_title)) : ?>
                            <a href="<?php echo esc_url($category_link); ?>"
                               class="theme-btn theme-primary-btn title-view-more">
                                <?php echo esc_attr($section_view_more_title); ?>
                                <?php vintuna_theme_svg('arrow-right'); ?>
                            </a>
                        <?php endif; ?>
                    </header>
                <?php endif; ?>

                <div class="theme-panel-body">
                    <div class="column-row">

                        <?php
                        $i = 0;
                        while ($you_may_like_post_query->have_posts()) :
                            $you_may_like_post_query->the_post();
                            $i++;

                            // Position inside the current column group (1, 2, or 3)
                            $pos_in_group = ($i - 1) % 3 + 1;

                            // Open a new column every 3 posts
                            if ($pos_in_group === 1) : ?>
                                <div class="column column-3 column-md-6 column-xs-12 mb-24">
                            <?php endif; ?>

                            <article id="theme-post-<?php the_ID(); ?>"
                                <?php post_class('news-article news-article-default mb-30'); ?>>

                                <?php
                                $fallback_image_url = vintuna_get_fallback_image_url();
                                if ($pos_in_group === 1) : ?>
                                    <div class="entry-image entry-image-small image-hover-effect hover-effect-shine">
                                        <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                            <?php
                                            if (has_post_thumbnail()) {
                                                the_post_thumbnail('medium_large', [
                                                    'alt' => the_title_attribute(['echo' => false]),
                                                ]);
                                            } else {
                                                echo '<img src="' . esc_url($fallback_image_url) . '" alt="' . esc_attr(get_the_title()) . '" />';
                                            }
                                            ?>
                                        </a>
                                    </div>
                                <?php endif; ?>

                                <div class="article-content">
                                    <h3 class="entry-title <?php echo $pos_in_group === 1 ? 'entry-title-medium' : 'entry-title-xsmall'; ?> line-clamp-3">
                                        <a href="<?php the_permalink(); ?>" tabindex="0" rel="bookmark"
                                           title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                                    </h3>
                                    <div class="entry-meta">
                                        <?php vintuna_posted_by(); ?>
                                    </div>
                                </div>
                            </article>

                            <?php
                            // Close column after 3 posts
                            if ($pos_in_group === 3) : ?>
                                </div> <!-- .column -->
                            <?php endif; ?>

                        <?php endwhile; ?>

                        <?php
                        // Close any unclosed column if total posts not multiple of 3
                        if ($i % 3 !== 0) : ?>
                    </div> <!-- .column -->
                    <?php endif; ?>

                </div> <!-- .column-row -->
            </div> <!-- .theme-panel-body -->
            </div> <!-- .theme-block -->

            <?php
            wp_reset_postdata();
        endif;
    }
endif;
