<?php
/**
 * Template Name: Home Page
 */

get_header();
?>

<main id="primary">

    <?php 
        $machine_repair_main_slider_wrap = absint(get_theme_mod('machine_repair_enable_slider', 0));

        if ($machine_repair_main_slider_wrap == 1): 
    ?>
        <section id="main-slider-wrap">
            <div class="slider-main">
                <div class="owl-carousel">
                    <?php for ($machine_repair_main_i = 1; $machine_repair_main_i <= 3; $machine_repair_main_i++): ?>
                        <?php 
                        $machine_repair_slider_image = get_theme_mod('machine_repair_slider_image' . $machine_repair_main_i);
                        if ($machine_repair_slider_image): 
                        ?>
                            <div class="main-slider-inner-box">
                                <div class="container">
                                    <div class="flex-row">
                                        <div class="slider-left wow zoomIn" data-wow-duration="1.5s">
                                            <div class="main-slider-content-box">
                                                <?php if ($machine_repair_top_text = get_theme_mod('machine_repair_slider_top_text' . $machine_repair_main_i)): ?>
                                                    <p class="slider-top"><?php echo esc_html($machine_repair_top_text); ?></p>
                                                <?php endif; ?>

                                                <?php if ($machine_repair_heading = get_theme_mod('machine_repair_slider_heading' . $machine_repair_main_i)): ?>
                                                    <h1><?php echo esc_html($machine_repair_heading); ?></h1>
                                                <?php endif; ?>

                                                <?php if ($machine_repair_text = get_theme_mod('machine_repair_slider_text' . $machine_repair_main_i)): ?>
                                                    <p class="slider-para"><?php echo wp_kses_post($machine_repair_text); ?></p>
                                                <?php endif; ?>

                                                <div class="main-slider-button">
                                                    <?php 
                                                    $machine_repair_button1_link = get_theme_mod('machine_repair_slider_button1_link' . $machine_repair_main_i);
                                                    $machine_repair_button1_text = get_theme_mod('machine_repair_slider_button1_text' . $machine_repair_main_i);
                                                    if ($machine_repair_button1_link || $machine_repair_button1_text): 
                                                    ?>
                                                        <a class="slide-btn-1" href="<?php echo esc_url($machine_repair_button1_link); ?>">
                                                            <?php echo esc_html($machine_repair_button1_text); ?>
                                                        </a>
                                                    <?php endif; ?>

                                                    <?php 
                                                    $machine_repair_button2_link = get_theme_mod('machine_repair_slider_button2_link' . $machine_repair_main_i);
                                                    $machine_repair_button2_text = get_theme_mod('machine_repair_slider_button2_text' . $machine_repair_main_i);
                                                    if ($machine_repair_button2_link || $machine_repair_button2_text): 
                                                    ?>
                                                        <a class="slide-btn-2" href="<?php echo esc_url($machine_repair_button2_link); ?>">
                                                            <?php echo esc_html($machine_repair_button2_text); ?>
                                                        </a>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="slider-right">
                                            <img src="<?php echo esc_url($machine_repair_slider_image); ?>" alt="<?php echo esc_attr($machine_repair_heading); ?>">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endif; ?>
                    <?php endfor; ?>
                </div>
            </div>
        </section>
    <?php endif; ?>

    <?php 
    // Check if the section is enabled
    $machine_repair_main_expert_wrap = absint(get_theme_mod('machine_repair_enable_event', 0));
    if ($machine_repair_main_expert_wrap == 1): 

        // Get category and posts to show values
        $machine_repair_catData2 = get_theme_mod('machine_repair_blog_cat','uncategorized');
        $machine_repair_posts_to_show = absint(get_theme_mod('machine_repair_posts_to_show', 3));

        // Check if category is set and not the default "select"
        if (!empty($machine_repair_catData2) && $machine_repair_catData2 !== 'select'): 
            $machine_repair_page_query = new WP_Query(array(
                'category_name' => sanitize_text_field($machine_repair_catData2),
                'posts_per_page' => $machine_repair_posts_to_show,
            ));

            if ($machine_repair_page_query->have_posts()):
                $machine_repair_post_count = 0;
    ?>
    <section id="main-expert-wrap">
        <div class="container">
            <div class="heading-expert-wrap">
                <?php 
                    $machine_repair_servicesec_heading = get_theme_mod('machine_repair_servicesec_heading');
                    $machine_repair_servicesec_content = get_theme_mod('machine_repair_servicesec_content');

                    // Check if there is any heading or content to display
                    if ($machine_repair_servicesec_heading || $machine_repair_servicesec_content): 
                    ?>
                        <p class="side-text"><?php esc_html_e('OUR PLANS', 'machine-repair'); ?></p>
                        <div class="heading-main-serv">
                            <?php if ($machine_repair_servicesec_heading): ?>
                                <h2 class="serv-heading"><?php echo esc_html($machine_repair_servicesec_heading); ?></h2>
                            <?php endif; ?>
                            <?php if ($machine_repair_servicesec_content): ?>
                                <p class="serv-content"><?php echo esc_html($machine_repair_servicesec_content); ?></p>
                            <?php endif; ?>
                        </div>
                    <?php endif; ?>
            </div>
            <div id="primary">
                <div class="owl-carousel">
                    <?php 
                    // Loop through each post
                    while ($machine_repair_page_query->have_posts()): 
                        $machine_repair_page_query->the_post();
                        $machine_repair_post_count++; 
                    ?>
                    <div class="box wow zoomIn" data-wow-duration="2s">
                        <div class="box-content">
                            <div class="head-title">
                                <div class="flex-row">
                                    <?php 
                                    // Get the icon class for the current post only
                                    $machine_repair_icon_class = get_theme_mod("machine_repair_services_icon" . ($machine_repair_page_query->current_post + 1)); // +1 to match the settings index
                                    if ($machine_repair_icon_class): 
                                    ?>
                                        <div class="icon-main">
                                            <div class="icon">
                                                <i class="<?php echo esc_attr($machine_repair_icon_class); ?> main-icon"></i>
                                                <span class="starting-text"><?php esc_html_e('starting at...', 'machine-repair'); ?></span> 
                                            </div>
                                        </div>
                                    <?php endif; ?>


                                    <?php 
                                    // Display price for the current post only
                                    $machine_repair_price = get_theme_mod("machine_repair_courses_prices" . ($machine_repair_page_query->current_post + 1)); // +1 because current_post is zero-based
                                    if ($machine_repair_price): 
                                    ?>
                                        <div class="serv-price">
                                            <p class="post-price mb-0"><?php echo esc_html($machine_repair_price); ?></p>
                                        </div>
                                    <?php endif; ?>

                                </div>
                            </div>
                            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

                            <?php 
                            // Check if any feature text is set for this post
                            $machine_repair_features_to_show = false;
                            for ($machine_repair_feature_count = 1; $machine_repair_feature_count <= 3; $machine_repair_feature_count++) {
                                if (get_theme_mod("machine_repair_post{$machine_repair_post_count}_feature_heading{$machine_repair_feature_count}") != '') {
                                    $machine_repair_features_to_show = true;
                                    break;
                                }
                            }
                            ?>

                            <?php if ($machine_repair_features_to_show): ?>
                                <div class="post-features">
                                    <?php 
                                    // Display each feature if it has text
                                    for ($machine_repair_feature_count = 1; $machine_repair_feature_count <= 3; $machine_repair_feature_count++): 
                                        $machine_repair_feature_text = get_theme_mod("machine_repair_post{$machine_repair_post_count}_feature_heading{$machine_repair_feature_count}");
                                        if ($machine_repair_feature_text): 
                                    ?>
                                        <div class="feature-item">
                                            <i class="fas fa-check"></i>
                                            <span class="main-features"><?php echo esc_html($machine_repair_feature_text); ?></span>
                                        </div>
                                    <?php 
                                        endif;
                                    endfor;
                                    ?>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="box-image">
                            <?php 
                            // Display post thumbnail or fallback color block
                            if (has_post_thumbnail()) { 
                                the_post_thumbnail(); 
                            } else { 
                            ?>
                                <div class="post-color"></div>
                            <?php } ?>
                            <div class="choose-plan">
                                <a href="<?php the_permalink(); ?>"><?php esc_html_e('CHOOSE PLAN','machine-repair'); ?></a>
                            </div>
                        </div>
                        <div class="clearfix"></div> 
                    </div>
                    <?php endwhile; wp_reset_postdata(); ?>
                </div>
            </div>                
        </div>
    </section>
    <?php else: ?>
        <p><?php esc_html_e('No posts found in this category.', 'machine-repair'); ?></p>
    <?php 
        endif; 
    endif; 
    endif; 
    ?>

</main>

<?php
get_footer();
?>