<?php
/**
 * Tiles Blocks
 *
 * @package Vintuna
 */

if (!function_exists('vintuna_tiles_block_section')) :
    function vintuna_tiles_block_section($vintuna_home_section, $repeat_times)
    {
        // Sanitize and assign section data
        $section_category = !empty($vintuna_home_section->section_category) ? sanitize_text_field($vintuna_home_section->section_category) : '';
        $section_category_tile_slider = !empty($vintuna_home_section->section_category_tile_slider) ? sanitize_text_field($vintuna_home_section->section_category_tile_slider) : '';
        $home_section_title = !empty($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 : '');

        $home_section_title_2 = esc_html(isset($vintuna_home_section->home_section_title_2) ? $vintuna_home_section->home_section_title_2 : '');
        $section_view_more_title_2 = esc_html(isset($vintuna_home_section->section_view_more_title_2) ? $vintuna_home_section->section_view_more_title_2 : '');

        // Query posts
        $tiles_post_query_1 = new WP_Query([
            'post_type'      => 'post',
            'posts_per_page' => 4,
            'post__not_in'   => get_option('sticky_posts'),
            'offset'              => $section_post_offset,
            'category_name'  => $section_category_tile_slider,
        ]);
        $category_id = get_cat_ID( $section_category_tile_slider );
        $category_link = get_category_link( $category_id );
        $tiles_post_query = new WP_Query([
            'post_type'      => 'post',
            'posts_per_page' => 9,
            'post__not_in'   => get_option('sticky_posts'),
            'offset'              => $section_post_offset,
            'category_name'  => $section_category,
        ]);
        $category_id_2 = get_cat_ID( $section_category );
        $category_link_2 = get_category_link( $category_id_2 );
        // Slider options
        $slider_options = [
            'arrows'   => empty($vintuna_home_section->ed_arrows_carousel) || $vintuna_home_section->ed_arrows_carousel === 'yes',
            'dots'     => !empty($vintuna_home_section->ed_dots_carousel) && $vintuna_home_section->ed_dots_carousel === 'yes',
            'autoplay' => empty($vintuna_home_section->ed_autoplay_carousel) || $vintuna_home_section->ed_autoplay_carousel === 'yes',
            'rtl'      => is_rtl(),
        ];
        ?>
        <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-block-tiles">



            <div class="column-row">
                <div class="column column-6 column-sm-12">

                    <?php if ($home_section_title) : ?>
                        <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_html($section_view_more_title);?><?php vintuna_theme_svg('arrow-right'); ?></a>
                            <?php }?>
                        </header>
                    <?php endif; ?>

                    <div class="theme-block-content">
                        <?php if ($slider_options['arrows']) : ?>
                            <div class="slider-block-navarea">
                                <button type="button" class="slide-btn slide-btn-small slide-prev-tiles">
                                    <?php vintuna_theme_svg('chevron-left'); ?>
                                </button>
                                <button type="button" class="slide-btn slide-btn-small slide-next-tiles">
                                    <?php vintuna_theme_svg('chevron-right'); ?>
                                </button>
                            </div>
                        <?php endif; ?>

                        <div class="theme-tiles-slide" data-slick='<?php echo wp_json_encode($slider_options); ?>'>
                            <?php if ($tiles_post_query_1->have_posts()) : ?>
                                <?php while ($tiles_post_query_1->have_posts()) : $tiles_post_query_1->the_post(); ?>
                                    <article id="theme-post-<?php the_ID(); ?>" <?php post_class('news-article news-article-default'); ?>>
                                        <?php
                                        $fallback_image_url = vintuna_get_fallback_image_url();

                                        if ( has_post_thumbnail() || $fallback_image_url ) : ?>
                                            <div class="entry-image entry-image-big 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('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">
                                            <div class="entry-meta">
                                                <?php vintuna_entry_footer(true, false, false, false, false); ?>
                                            </div>
                                            <h3 class="entry-title entry-title-large line-clamp-2">
                                                <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 class="entry-content hidden-xs-element entry-content-muted">
                                                <?php
                                                if (has_excerpt()) {
                                                    the_excerpt();
                                                } else {
                                                    echo '<p>' . esc_html(wp_trim_words(get_the_content(), 20, '...')) . '</p>';
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </article>
                                <?php endwhile; ?>
                            <?php endif; ?>
                            <?php wp_reset_postdata(); ?>
                        </div>
                    </div>

                </div>

                <?php if ($tiles_post_query->have_posts()) : ?>

                    <div class="column column-6 column-sm-12">
                        <?php if ($home_section_title_2) : ?>
                            <header class="theme-block-header">
                                <h2 class="theme-block-title"><?php echo esc_html($home_section_title_2); ?></h2>
                                <span class="block-title-dash"></span>
                                <?php if (!empty($category_link_2)&& !empty($section_view_more_title_2)){ ?>
                                    <a href="<?php echo esc_url($category_link_2); ?>" class="theme-btn theme-primary-btn title-view-more"><?php echo esc_html($section_view_more_title_2);?><?php vintuna_theme_svg('arrow-right'); ?></a>
                                <?php }?>
                            </header>
                        <?php endif; ?>
                        <div class="article-list-wrapper">
                            <?php
                            $i = 0;
                            while ($tiles_post_query->have_posts()) : $tiles_post_query->the_post();
                                $i++;
                                $is_prime = $i === 1;
                                $thumbnail_size = $is_prime ? 'medium_large' : 'medium';
                                $title_class = $is_prime ? 'entry-title-big' : 'entry-title-xsmall';
                                ?>
                                <article id="theme-post-<?php the_ID(); ?>" class="news-article news-article-list <?php echo $is_prime ? 'article-list-prime' : ''; ?>">

                                    <?php
                                    $fallback_image_url = vintuna_get_fallback_image_url();

                                    if ( has_post_thumbnail() || $fallback_image_url ) : ?>
                                        <div class="entry-image <?php echo $is_prime ? 'entry-image-small' : 'entry-image-thumbnail'; ?> 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($thumbnail_size, [
                                                        '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 $title_class; ?> line-clamp-3">
                                            <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
                                                <?php the_title(); ?>
                                            </a>
                                        </h3>

                                        <?php if ($is_prime) : ?>
                                            <div class="entry-meta mb-10">
                                                <?php vintuna_posted_by(); ?>
                                            </div>
                                            <?php
                                            if (has_excerpt()) {
                                                the_excerpt();
                                            } else {
                                                echo '<p>' . esc_html(wp_trim_words(get_the_content(), 20, '...')) . '</p>';
                                            }
                                            ?>
                                        <?php endif; ?>
                                    </div>
                                </article>
                            <?php endwhile; ?>
                            <?php wp_reset_postdata(); ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
        <?php
    }
endif;