<?php
/**
 * Default Banner
 *
 * @package NewsNation
 */
if (!function_exists('newsnation_horizontal_vertical_slider')):
    function newsnation_horizontal_vertical_slider($newsnation_home_section, $repeat_times)
    {
        // Extract section values with safe defaults
        $section_category_7 = isset($newsnation_home_section->section_category_7) ? sanitize_text_field($newsnation_home_section->section_category_7) : '';
        $section_post_cat_1 = isset($newsnation_home_section->section_post_cat_1) ? sanitize_text_field($newsnation_home_section->section_post_cat_1) : '';
        // Carousel options
        $slider_arrows = !empty($newsnation_home_section->ed_arrows_carousel) ? $newsnation_home_section->ed_arrows_carousel : 'yes';
        $slider_dots = !empty($newsnation_home_section->ed_dots_carousel) ? $newsnation_home_section->ed_dots_carousel : 'no';
        $slider_autoplay = !empty($newsnation_home_section->ed_autoplay_carousel) ? $newsnation_home_section->ed_autoplay_carousel : 'yes';
        $section_post_offset = isset($newsnation_home_section->section_post_offset) ? absint($newsnation_home_section->section_post_offset) : 0;
        $section_post_offset_1 = isset($newsnation_home_section->section_post_offset_1) ? absint($newsnation_home_section->section_post_offset_1) : 0;

        // Normalize values for JS
        $arrow = ($slider_arrows === 'yes') ? 'true' : 'false';
        $autoplay = ($slider_autoplay === 'yes') ? 'true' : 'false';
        $dots = ($slider_dots === 'yes') ? 'true' : 'false';
        $rtl = is_rtl() ? 'true' : 'false';
        // Queries
        $banner_query_2 = new WP_Query([
            'post_type' => 'post',
            'posts_per_page' => 2,
            'offset' => $section_post_offset_1,
            'post__not_in' => get_option("sticky_posts"),
            'category_name' => $section_category_7,
        ]);
        $banner_query_1 = new WP_Query([
            'post_type' => 'post',
            'posts_per_page' => 4,
            'offset' => $section_post_offset,
            'post__not_in' => get_option("sticky_posts"),
            'category_name' => $section_post_cat_1,
        ]);
        ?>
        <div id="theme-block-<?php echo esc_attr($repeat_times); ?>" class="theme-block theme-default-banner">
            <div class="wrapper">
                <div class="column-row">
                    <!-- Left Column -->
                    <div class="column column-8 column-sm-12 mb-sm-20">
                        <div class="theme-slider-container">
                            <?php if ($banner_query_1->have_posts()):
                                $banner_pagenav = ''; ?>
                                <div class="slider-content-container"
                                    data-slick='{"autoplay": <?php echo esc_attr($autoplay); ?>, "dots": <?php echo esc_attr($dots); ?>, "rtl": <?php echo esc_attr($rtl); ?>}'>

                                    <?php while ($banner_query_1->have_posts()):
                                        $banner_query_1->the_post(); ?>
                                        <div class="slider-item">
                                            <article id="theme-post-<?php the_ID(); ?>" <?php post_class('news-article news-article-banner'); ?>>


                                                <?php
                                                $fallback_image_url = newsnation_get_fallback_image_url();

                                                if (has_post_thumbnail() || $fallback_image_url): ?>
                                                    <div class="entry-image entry-image-large 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; ?>


                                            </article>
                                        </div>
                                        <?php
                                        $banner_pagenav .= '<div class="pagination-content-item">';
                                        $banner_pagenav .= '<h2 class="entry-title entry-title-large line-clamp-3">';
                                        $banner_pagenav .= '<a href="' . esc_url(get_permalink()) . '" rel="bookmark">' . esc_html(get_the_title()) . '</a>';
                                        $banner_pagenav .= '</h2>';
                                        ob_start();
                                        ?>
                                        <div class="entry-meta">
                                            <?php newsnation_posted_by(); ?>
                                        </div>
                                        <?php
                                        $banner_pagenav .= ob_get_clean();
                                        $banner_pagenav .= '<p class="line-clamp-3 mb-16">' . esc_html(get_the_excerpt()) . '</p>';

                                        $banner_pagenav .= '</div>';
                                        ?>
                                    <?php endwhile; ?>
                                </div>
                                <?php wp_reset_postdata(); ?>
                                <div class="slider-content-pagination">
                                    <div class="pagination-content-container" data-slick='{"rtl": <?php echo esc_attr($rtl); ?>}'>
                                        <?php echo wp_kses_post($banner_pagenav); ?>
                                    </div>
                                    <?php if ($slider_arrows === 'yes') { ?>
                                        <div class="pagination-content-button">
                                            <button class="slide-btn slide-prev theme-slide-btn">
                                                <span
                                                    class="screen-reader-text"><?php esc_html_e('Previous Slide', 'newsnation'); ?></span>
                                                <?php newsnation_theme_svg('arrow-left'); ?>
                                            </button>
                                            <button class="slide-btn slide-next theme-slide-btn">
                                                <span class="screen-reader-text"><?php esc_html_e('Next Slide', 'newsnation'); ?></span>
                                                <?php newsnation_theme_svg('arrow-right'); ?>
                                            </button>
                                        </div>
                                    <?php } ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <!-- Right Column -->
                    <div class="column column-4 column-sm-12">
                        <?php if ($banner_query_2->have_posts()): ?>
                            <?php while ($banner_query_2->have_posts()):
                                $banner_query_2->the_post(); ?>
                                <article id="theme-post-<?php the_ID(); ?>" <?php post_class('news-article news-article-tiles'); ?>>
                                    <?php
                                    $fallback_image_url = newsnation_get_fallback_image_url();

                                    if (has_post_thumbnail() || $fallback_image_url): ?>
                                        <div
                                            class="entry-image entry-image-medium image-hover-effect hover-effect-shine entry-image-overlay image-overlay-gradient">
                                            <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">
                                        <div class="theme-article-header">
                                            <h3 class="entry-title entry-title-medium">
                                                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                                            </h3>
                                            <div class="entry-meta">
                                                <?php newsnation_posted_by(); ?>
                                            </div>
                                        </div>
                                    </div>
                                </article>
                            <?php endwhile; ?>
                            <?php wp_reset_postdata(); ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php }
endif;