<?php

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

$bike_workshop_args = '';

bike_workshop_render_service_section($bike_workshop_args);

/**
 * Render Service Section.
 */
function bike_workshop_render_service_section($bike_workshop_args) { ?>
    <section id="bike_workshop_trending_section" class="asterthemes-frontpage-section trending-section trending-style-1">
        <?php
        if (is_customize_preview()) :
            bike_workshop_section_link('bike_workshop_service_section');
        endif; ?>

        <div class="asterthemes-wrapper">
            <div class="services-section">
                <?php
                // Make sure to provide a default value for get_theme_mod calls.
                $bike_workshop_heading_services_section = get_theme_mod('bike_workshop_heading_services_section', '');
                if (!empty($bike_workshop_heading_services_section)) {
                    $bike_workshop_heading_words = explode(' ', $bike_workshop_heading_services_section);
                    $bike_workshop_last_word = array_pop($bike_workshop_heading_words);
                    $heading_without_last_word = implode(' ', $bike_workshop_heading_words);
                ?>
                    <h3><?php echo esc_html($heading_without_last_word); ?> <span class="last-word"><?php echo esc_html($bike_workshop_last_word); ?></span></h3>
                <?php } ?>
                <div class="tab">
                    <?php
                    $bike_workshop_featured_post = get_theme_mod('bike_workshop_services_number', 0);
                    for ($bike_workshop_j = 1; $bike_workshop_j <= $bike_workshop_featured_post; $bike_workshop_j++) {
                        $bike_workshop_main_id = get_theme_mod('bike_workshop_services_text' . $bike_workshop_j, '');
                        $bike_workshop_tab_id = str_replace(' ', '-', $bike_workshop_main_id);
                    ?>
                        <button class="tablinks" onclick="bike_workshop_services_tab(event, '<?php echo esc_attr($bike_workshop_tab_id); ?>')">
                            <?php echo esc_html($bike_workshop_main_id); ?>
                        </button>
                    <?php } ?>
                </div>

                <?php for ($bike_workshop_j = 1; $bike_workshop_j <= $bike_workshop_featured_post; $bike_workshop_j++) { 
                    $bike_workshop_main_id = get_theme_mod('bike_workshop_services_text' . $bike_workshop_j, '');
                    $bike_workshop_tab_id = str_replace(' ', '-', $bike_workshop_main_id); 
                ?>
                    <div id="<?php echo esc_attr($bike_workshop_tab_id); ?>" class="tabcontent">
                        <div class="services_main_box">
                            <?php
                            $bike_workshop_default_image_url = get_template_directory_uri() . '/resource/img/default.png'; // Update this with the actual path to your default image

                            $bike_workshop_args = array(
                                'post_type' => 'post',
                                'post_status' => 'publish',
                                'category_name' => get_theme_mod('bike_workshop_services_category' . $bike_workshop_j),
                                'posts_per_page' => 3,
                            );
                            $bike_workshop_arr_posts = new WP_Query($bike_workshop_args);
                            if ($bike_workshop_arr_posts->have_posts()) :
                                while ($bike_workshop_arr_posts->have_posts()) : $bike_workshop_arr_posts->the_post();
                            ?>
                                    <div class="services_inner_box">
                                        <?php if (has_post_thumbnail()) : ?>
                                            <?php the_post_thumbnail(); ?>
                                        <?php else : ?>
                                            <img src="<?php echo esc_url($bike_workshop_default_image_url); ?>" alt="<?php echo esc_attr(get_the_title()); ?>">
                                        <?php endif; ?>
                                        <div class="services_content_box">
                                            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                            <?php the_excerpt(); ?>
                                        </div>
                                    </div>
                            <?php
                                endwhile;
                                wp_reset_postdata();
                            endif;
                            ?>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
    </section>
<?php
}