<?php
// Fetch the slider setting from the Customizer
$pet_house_slider = get_theme_mod('pet_house_slider_setting', false);

// Check if the slider is enabled
if ($pet_house_slider == '1') : ?>
<section id="slider-section" class="slider-area">
    <div class="container">
        <div class="slider-content">
            <div id="owl-carousel" class="owl-carousel owl-theme">
                <?php

                $pet_house_pages = array();
                for ($pet_house_count = 1; $pet_house_count <= 3; $pet_house_count++) {
                    $pet_house_mod = intval(get_theme_mod('pet_house_slider' . $pet_house_count));
                
                    // Check that the value is valid and not the placeholder value 'page-none-selected'
                    if ('page-none-selected' !== $pet_house_mod && $pet_house_mod > 0) {
                        $pet_house_pages[] = $pet_house_mod;
                    }
                }

                // If pages were selected, run the query
                if (!empty($pet_house_pages)) :
                    $args = array(
                        'post_type' => 'page',
                        'post__in' => $pet_house_pages,
                        'orderby' => 'post__in'
                    );
                    $query = new WP_Query($args);

                    // If pages were found, display them
                    if ($query->have_posts()) :
                        while ($query->have_posts()) : $query->the_post(); ?>
                                <div class="row">
                                    <div class="col-lg-6 col-md-6 align-self-center">
                                        <div class="carousel-caption">
                                            <div class="inner_carousel pe-lg-5 wow fadeInUp" data-wow-delay="0.5s">
                                                <?php
                                                // Fetch slider text from Customizer
                                                $pet_house_slider_text = get_theme_mod('pet_house_slider_text');
                                                if (!empty($pet_house_slider_text)) { ?>
                                                    <p class="mb-2 slider-top-text"><?php echo esc_html($pet_house_slider_text); ?></p>
                                                <?php } ?>

                                                <!-- Display the page title -->
                                                <h1 class="mb-3">
                                                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                </h1>

                                                <p class="mb-2 slider-content"><?php echo esc_html(wp_trim_words(get_the_content(), 28)); ?></p>

                                                <div class="slider-mainbtn mt-md-4 mt-3 mb-3">
                                                    <!-- button -->
                                                    <span class="slide-btn1">
                                                        <a href="<?php the_permalink(); ?>" class="btn-text me-md-3 mb-2">
                                                            <?php echo esc_html__('Shop Now', 'pet-house'); ?><i class="fas fa-shopping-bag ms-2"></i>
                                                        </a>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Slider image section -->
                                    <div class="col-lg-6 col-md-6 slider-img-col">
                                        <div class="sliderimg">
                                            <?php if (has_post_thumbnail()) { ?>
                                                <img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title_attribute(); ?>" />
                                            <?php } else { ?>
                                                <div class="slider-color-box"></div>
                                            <?php } ?>
                                        </div>
                                    </div>
                                </div>
                        <?php endwhile;
                        wp_reset_postdata();  // Reset post data after the loop
                    else : ?>
                        <div class="no-postfound"></div>
                    <?php endif;
                endif; ?>
            </div>
        </div>
    </div>
</section>
<?php endif; ?>