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

get_header();
?>

<main id="primary">

    <?php 
    $surgical_hospital_main_slider_wrap = absint(get_theme_mod('surgical_hospital_enable_slider', 0));
    if($surgical_hospital_main_slider_wrap == 1): 
    ?>
        <section id="main-slider-wrap">
            <div class="owl-carousel">
                <?php for ($surgical_hospital_main_i=1; $surgical_hospital_main_i <= 3; $surgical_hospital_main_i++): ?>
                    <?php if ($surgical_hospital_slider_image = get_theme_mod('surgical_hospital_slider_image'.$surgical_hospital_main_i)): ?>
                        <div class="main-slider-inner-box">
                            <img src="<?php echo esc_url($surgical_hospital_slider_image); ?>" alt="<?php echo esc_attr( get_theme_mod('surgical_hospital_slider_heading'.$surgical_hospital_main_i) ); ?>">
                            <div class="main-slider-content-box wow zoomIn" data-wow-duration="1.5s">
                                <?php
                                    $surgical_hospital_heading = get_theme_mod('surgical_hospital_slider_heading' . $surgical_hospital_main_i);
                                    if ($surgical_hospital_heading) {
                                        $surgical_hospital_words = explode(' ', $surgical_hospital_heading);

                                        if (count($surgical_hospital_words) >= 2) {
                                            // Wrap the second word
                                            $surgical_hospital_words[1] = '<span class="highlight">' . esc_html($surgical_hospital_words[1]) . '</span>';
                                        }

                                        // Join and display the title
                                        echo '<h1>' . wp_kses_post(implode(' ', $surgical_hospital_words)) . '</h1>';
                                    }
                                ?>
                                <?php if ($surgical_hospital_text = get_theme_mod('surgical_hospital_slider_text'.$surgical_hospital_main_i)): ?>
                                    <p class="slider-content"><?php echo esc_html($surgical_hospital_text); ?></p>
                                <?php endif; ?>
                                <div class="main-slider-button">
                                    <?php if ( get_theme_mod('surgical_hospital_slider_button1_link'.$surgical_hospital_main_i) ||  get_theme_mod('surgical_hospital_slider_button1_text'.$surgical_hospital_main_i )) : ?><a class="slide-btn-1" href="<?php echo esc_url( get_theme_mod('surgical_hospital_slider_button1_link'.$surgical_hospital_main_i) ); ?>"><?php echo esc_html( get_theme_mod('surgical_hospital_slider_button1_text'.$surgical_hospital_main_i) ); ?></a><?php endif; ?>
                                    <?php if ( get_theme_mod('surgical_hospital_slider_button2_link'.$surgical_hospital_main_i) ||  get_theme_mod('surgical_hospital_slider_button2_text'.$surgical_hospital_main_i )) : ?><a class="slide-btn-2" href="<?php echo esc_url( get_theme_mod('surgical_hospital_slider_button2_link'.$surgical_hospital_main_i) ); ?>"><?php echo esc_html( get_theme_mod('surgical_hospital_slider_button2_text'.$surgical_hospital_main_i) ); ?></a><?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endfor; ?>
            </div>
        </section>
    <?php endif; ?>

   <?php 
    $surgical_hospital_main_expert_wrap = absint(get_theme_mod('surgical_hospital_projetcs_sec_show', 0));
    if ($surgical_hospital_main_expert_wrap == 1): 
    ?>
        <section id="projetcs-sec">
            <div class="container">
                <div class="serv-header">
                    <div class="serv-left">
                        <div class="title-icon"><i class="fas fa-hospital-alt"></i></div>
                        <div class="title-head">
                            <?php if( get_theme_mod('surgical_hospital_projetcs_main_text') != '' ){ ?>
                                <p class="project-top-text"><?php echo esc_html(get_theme_mod('surgical_hospital_projetcs_main_text',''));?></p>
                            <?php }?>
                            <?php if( get_theme_mod('surgical_hospital_projetcs_main_heading') != '' ){ ?>
                                <h2><?php echo esc_html(get_theme_mod('surgical_hospital_projetcs_main_heading',''));?></h2>
                            <?php }?>
                        </div>
                    </div>
                    <?php 
                    $surgical_hospital_featured_post = absint(get_theme_mod('surgical_hospital_projetcs_number', 6)); 
                    $surgical_hospital_has_tabs = true; // Flag to check if there's any tab text
                    ?>
                    <div class="tab">
                        <?php
                        for ( $surgical_hospital_j = 1; $surgical_hospital_j <= $surgical_hospital_featured_post; $surgical_hospital_j++ ){ 
                            $surgical_hospital_main_tab_text = get_theme_mod('surgical_hospital_projetcs_text'.$surgical_hospital_j);
                            if ( !empty($surgical_hospital_main_tab_text) ) {
                                $surgical_hospital_has_tabs = true; // At least one tab has text
                                ?>
                                <button class="tablinks <?php echo ($surgical_hospital_j === 1) ? 'active' : ''; ?>" 
                                        onclick="surgical_hospital_projetcs_tab(event, '<?php echo esc_attr(str_replace(' ', '-', $surgical_hospital_main_tab_text)); ?>')">
                                    <?php echo esc_html($surgical_hospital_main_tab_text); ?>
                                </button>
                            <?php } 
                        } ?>
                    </div>
                </div>
            </div>
                <?php if ($surgical_hospital_has_tabs): // Only show tab content if there's at least one tab ?>
                    <?php for ( $surgical_hospital_j = 1; $surgical_hospital_j <= $surgical_hospital_featured_post; $surgical_hospital_j++ ){ 
                        $surgical_hospital_main_tab_text = get_theme_mod('surgical_hospital_projetcs_text'.$surgical_hospital_j);
                        if ( !empty($surgical_hospital_main_tab_text) ) { ?>
                            <div id="<?php echo esc_attr(str_replace(' ', '-', $surgical_hospital_main_tab_text)); ?>" 
                                 class="tabcontent mt-3 <?php echo ($surgical_hospital_j === 1) ? 'active' : ''; ?>"
                                 style="<?php echo ($surgical_hospital_j === 1) ? 'display:block;' : 'display:none;'; ?>">
                                <div class="posts-wrapper">
                                    <div class="owl-carousel owl-theme">
                                        <?php
                                          $surgical_hospital_post_category = get_theme_mod('surgical_hospital_offer_section_category' . $surgical_hospital_j, 'uncategorized');
                                          if ($surgical_hospital_post_category) :
                                            $surgical_hospital_category_id = term_exists($surgical_hospital_post_category, '');
                                            if ($surgical_hospital_category_id !== 0 && $surgical_hospital_category_id !== null) :
                                              $surgical_hospital_posts_to_show = get_theme_mod('surgical_hospital_posts_to_show', 6);
                                              $surgical_hospital_page_query = new WP_Query(array(
                                                'category_name' => esc_attr($surgical_hospital_post_category),
                                                'posts_per_page' => $surgical_hospital_posts_to_show,
                                              ));

                                              $surgical_hospital_post_count = 0;
                                              if ($surgical_hospital_page_query->have_posts()) :
                                                while ($surgical_hospital_page_query->have_posts()) : $surgical_hospital_page_query->the_post(); 
                                                  $surgical_hospital_post_count++;
                                          ?>
                                                  <div class="item wow zoomIn" data-wow-duration="2s">
                                                    <div class="serv-box">
                                                        <div class="post-main-img">
                                                            <?php if (has_post_thumbnail()) : ?>
                                                              <img src="<?php echo esc_url(get_the_post_thumbnail_url(null, 'full')); ?>" alt="<?php the_title_attribute(); ?>" />
                                                            <?php else : ?>
                                                              <img src="<?php echo esc_url(get_template_directory_uri() . '/revolution/assets/images/slider.png'); ?>" alt="Default Image" />
                                                            <?php endif; ?>
                                                              <!-- Content and Social Icons Over Image -->
                                                            <div class="post-content">
                                                                <?php 
                                                                    // Get the icon class for the current post only
                                                                    $surgical_hospital_icon_class = get_theme_mod("surgical_hospital_services_icon" . ($surgical_hospital_page_query->current_post + 1),'fas fa-user'); // +1 to match the settings index
                                                                    if ($surgical_hospital_icon_class): 
                                                                    ?>
                                                                        <div class="icon-main">
                                                                            <div class="icon">
                                                                                <i class="<?php echo esc_attr($surgical_hospital_icon_class); ?> main-icon"></i>
                                                                            </div>
                                                                        </div>
                                                                <?php endif; ?>
                                                                <h3>
                                                                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                                                                </h3>
                                                                <p class="post-para"><?php echo wp_trim_words(get_the_content(), 25); ?></p>
                                                                <div class="post-read-btn">
                                                                    <a href="<?php the_permalink(); ?>"><?php esc_html_e('Read More', 'surgical-hospital'); ?></a>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                  </div>
                                                <?php endwhile; ?>
                                                <?php wp_reset_postdata(); ?>
                                              <?php else : ?>
                                                <div class="no-postfound"></div>
                                              <?php endif; ?>
                                            <?php endif; ?>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        <?php } 
                    } ?> 
                <?php endif; ?>   
        </section>
    <?php endif; ?>

</main>
<?php
get_footer();
?>