<?php

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

$auto_mechanic_args = '';

auto_mechanic_render_service_section($auto_mechanic_args);

/**
 * Render Service Section.
 */
function auto_mechanic_render_service_section($auto_mechanic_args) { ?>
    <section id="auto_mechanic_trending_section" class="asterthemes-frontpage-section trending-section trending-style-1">
        <?php
        if (is_customize_preview()) :
            auto_mechanic_section_link('auto_mechanic_service_section');
        endif; ?>

        <div class="asterthemes-wrapper">
            <div class="services-section">
                <?php
                // Make sure to provide a default value for get_theme_mod calls.
                $auto_mechanic_heading_services_section = get_theme_mod('auto_mechanic_heading_services_section', '');
                if (!empty($auto_mechanic_heading_services_section)) {
                    $auto_mechanic_heading_words = explode(' ', $auto_mechanic_heading_services_section);
                    $auto_mechanic_last_word = array_pop($auto_mechanic_heading_words);
                    $heading_without_last_word = implode(' ', $auto_mechanic_heading_words);
                ?>
                    <h3><?php echo esc_html($heading_without_last_word); ?> <span class="last-word"><?php echo esc_html($auto_mechanic_last_word); ?></span></h3>
                <?php } ?>
                <div class="tab">
                    <?php
                    $auto_mechanic_featured_post = get_theme_mod('auto_mechanic_services_number', 0);
                    for ($auto_mechanic_j = 1; $auto_mechanic_j <= $auto_mechanic_featured_post; $auto_mechanic_j++) {
                        $auto_mechanic_main_id = get_theme_mod('auto_mechanic_services_text' . $auto_mechanic_j, '');
                        $auto_mechanic_tab_id = str_replace(' ', '-', $auto_mechanic_main_id);
                    ?>
                        <button class="tablinks" onclick="auto_mechanic_services_tab(event, '<?php echo esc_attr($auto_mechanic_tab_id); ?>')">
                            <?php echo esc_html($auto_mechanic_main_id); ?>
                        </button>
                    <?php } ?>
                </div>

                <?php for ($auto_mechanic_j = 1; $auto_mechanic_j <= $auto_mechanic_featured_post; $auto_mechanic_j++) { 
                    $auto_mechanic_main_id = get_theme_mod('auto_mechanic_services_text' . $auto_mechanic_j, '');
                    $auto_mechanic_tab_id = str_replace(' ', '-', $auto_mechanic_main_id); 
                ?>
                    <div id="<?php echo esc_attr($auto_mechanic_tab_id); ?>" class="tabcontent">
                        <div class="services_main_box">
                            <?php
                            $auto_mechanic_default_image_url = get_template_directory_uri() . '/resource/img/default.png'; // Update this with the actual path to your default image

                            $auto_mechanic_args = array(
                                'post_type' => 'post',
                                'post_status' => 'publish',
                                'category_name' => get_theme_mod('auto_mechanic_services_category' . $auto_mechanic_j),
                                'posts_per_page' => 8,
                            );
                            $auto_mechanic_arr_posts = new WP_Query($auto_mechanic_args);
                            if ($auto_mechanic_arr_posts->have_posts()) :
                                while ($auto_mechanic_arr_posts->have_posts()) : $auto_mechanic_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($auto_mechanic_default_image_url); ?>" alt="<?php 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
}