<?php

if ( ! get_theme_mod( 'florist_bouquet_enable_about_section', false ) ) {
    return;
}

$florist_bouquet_slider_content_ids  = array();
$florist_bouquet_slider_content_type = get_theme_mod( 'florist_bouquet_about_content_type', 'post' );

$florist_bouquet_slider_content_ids[] = get_theme_mod( 'florist_bouquet_about_content_' . $florist_bouquet_slider_content_type . '_' );

// Get the category for the services slider from theme mods or a default category
$florist_bouquet_about_category = get_theme_mod('florist_bouquet_about_category', 'about');

// Modify query to fetch posts from a specific category
$florist_bouquet_about_args = array(
    'post_type'           => $florist_bouquet_slider_content_type,
    'post__in'            => array_filter( $florist_bouquet_slider_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(3),
    'ignore_sticky_posts' => true,
);

// Apply category filter only if content type is 'post'
if ( 'post' === $florist_bouquet_slider_content_type && ! empty( $florist_bouquet_about_category ) ) {
    $florist_bouquet_about_args['category_name'] = $florist_bouquet_about_category;
}

$florist_bouquet_about_args = apply_filters( 'florist_bouquet_about_section_args', $florist_bouquet_about_args );

florist_bouquet_render_about_section( $florist_bouquet_about_args );

/**
 * Render about Section.
 */
function florist_bouquet_render_about_section( $florist_bouquet_about_args ) { ?>

    <section id="florist_bouquet_about_section" class="about-section about-style-1">
        <?php
        if ( is_customize_preview() ) :
            florist_bouquet_section_link( 'florist_bouquet_about_section' );
        endif;
        ?>
        <div class="asterthemes-wrapper">
            <?php
            $florist_bouquet_query = new WP_Query( $florist_bouquet_about_args );
            if ( $florist_bouquet_query->have_posts() ) :
                while ( $florist_bouquet_query->have_posts() ) :
                    $florist_bouquet_query->the_post();
                    $florist_bouquet_button_label = get_theme_mod( 'florist_bouquet_about_button_label_');
                    $florist_bouquet_button_link  = get_theme_mod( 'florist_bouquet_about_button_link_');
                    $florist_bouquet_button_link  = ! empty( $florist_bouquet_button_link ) ? $florist_bouquet_button_link : get_the_permalink();
                    
                    // Count custom images and featured image
                    $florist_bouquet_custom_images_count = 0;
                    $florist_bouquet_custom_image_url_1 = get_theme_mod('custom_image_setting_1');
                    $florist_bouquet_custom_image_url_2 = get_theme_mod('custom_image_setting_2');
                    $florist_bouquet_custom_image_url_3 = get_theme_mod('custom_image_setting_3');
                    $florist_bouquet_custom_image_url_4 = get_theme_mod('custom_image_setting_4'); 

                    $florist_bouquet_custom_images_count += !empty($florist_bouquet_custom_image_url_1) ? 1 : 0;
                    $florist_bouquet_custom_images_count += !empty($florist_bouquet_custom_image_url_2) ? 1 : 0;
                    $florist_bouquet_custom_images_count += !empty($florist_bouquet_custom_image_url_3) ? 1 : 0;
                    $florist_bouquet_custom_images_count += !empty($florist_bouquet_custom_image_url_4) ? 1 : 0;

                    $florist_bouquet_has_featured_image = has_post_thumbnail();

                    // Check if only the default image will be displayed
                    $florist_bouquet_only_default_image = ($florist_bouquet_custom_images_count === 0 && !$florist_bouquet_has_featured_image);
                    ?>
                    <div class="about-single<?php echo $florist_bouquet_only_default_image ? ' full-width' : ''; ?>">
                        <div class="about-img">
                            <?php
                            // Display the custom images if available
                            if ($florist_bouquet_custom_image_url_1) {
                                echo '<img src="' . esc_url($florist_bouquet_custom_image_url_1) . '" alt="Custom Image 1">';
                            }
                            if ($florist_bouquet_custom_image_url_2) {
                                echo '<img src="' . esc_url($florist_bouquet_custom_image_url_2) . '" alt="Custom Image 2">';
                            }
                            if ($florist_bouquet_custom_image_url_3) {
                                echo '<img src="' . esc_url($florist_bouquet_custom_image_url_3) . '" alt="Custom Image 3">';
                            }
                            if ($florist_bouquet_custom_image_url_4) {
                                echo '<img src="' . esc_url($florist_bouquet_custom_image_url_4) . '" alt="Custom Image 4">';
                            }
                            // Display the featured image of the current post if no custom images are set
                            if (!($florist_bouquet_custom_image_url_1 || $florist_bouquet_custom_image_url_2 || $florist_bouquet_custom_image_url_3 || $florist_bouquet_custom_image_url_4) && has_post_thumbnail()) {
                                the_post_thumbnail('full');
                            }
                            ?>
                        </div>

                        <div class="about-caption">
                            <h1 class="about-caption-title">
                                <a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?>
                                </a>
                            </h1>
                            <div class="caption-description">
                                <p>
                                    <?php echo wp_kses_post( wp_trim_words( get_the_content(), 50 ) ); ?>
                                </p>
                            </div>
                            <?php if ( ! empty( $florist_bouquet_button_label ) ) { ?>
                                <div class="about-slider-btn">
                                    <a href="<?php echo esc_url( $florist_bouquet_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $florist_bouquet_button_label ); ?></a>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                    <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </div>
    </section>

    <?php
}
?>