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

get_header();
?>

<main id="primary">
    <?php 
    $university_institute_main_slider_wrap = absint(get_theme_mod('university_institute_enable_slider', 0));
    if($university_institute_main_slider_wrap == 1): 
    ?>
        <section id="main-slider-wrap">
            <div class="owl-carousel">
                <?php for ($university_institute_main_i=1; $university_institute_main_i <= 3; $university_institute_main_i++): ?>
                    <?php if ($university_institute_slider_image = get_theme_mod('university_institute_slider_image'.$university_institute_main_i)): ?>
                        <div class="main-slider-inner-box">
                            <img src="<?php echo esc_url($university_institute_slider_image); ?>" alt="<?php echo esc_attr( get_theme_mod('university_institute_slider_heading'.$university_institute_main_i) ); ?>">
                            <div class="main-slider-content-box">
                                <?php if ($university_institute_heading = get_theme_mod('university_institute_slider_heading' . $university_institute_main_i)): ?>
                                    <h1><?php echo esc_html($university_institute_heading); ?></h1>
                                <?php endif; ?>
                                <?php if ($university_institute_text = get_theme_mod('university_institute_slider_text'.$university_institute_main_i)): ?>
                                    <p class="slider-content"><?php echo esc_html($university_institute_text); ?></p>
                                <?php endif; ?>
                                <div class="main-slider-button">
                                    <?php if ( get_theme_mod('university_institute_slider_button1_link'.$university_institute_main_i) ||  get_theme_mod('university_institute_slider_button1_text'.$university_institute_main_i )) : ?><a class="slide-btn-1" href="<?php echo esc_url( get_theme_mod('university_institute_slider_button1_link'.$university_institute_main_i) ); ?>"><?php echo esc_html( get_theme_mod('university_institute_slider_button1_text'.$university_institute_main_i) ); ?></a><?php endif; ?>
                                    <?php 
                                        $university_institute_button2_link = get_theme_mod('university_institute_slider_button2_link' . $university_institute_main_i);
                                        $university_institute_button2_text = get_theme_mod('university_institute_slider_button2_text' . $university_institute_main_i);
                                        if ($university_institute_button2_link || $university_institute_button2_text): 
                                        ?>
                                            <a class="slide-btn-2" href="<?php echo esc_url($university_institute_button2_link); ?>">
                                                <?php echo esc_html($university_institute_button2_text); ?>
                                            </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endfor; ?>
            </div>
        </section>
    <?php endif; ?>

   <?php 
        $university_institute_servicesec = get_theme_mod('university_institute_service_show_hide', 0);
        if ($university_institute_servicesec == 1) : 
        ?>
        <section id="service-section">
          <div class="container"> 
            <div class="serv-head"> 
                <div class="serv-left">
                    <?php 
                        $university_institute_title = get_theme_mod('university_institute_section_title');
                        if ($university_institute_title) : 
                            $university_institute_title_words = explode(' ', $university_institute_title); // Split the title into words
                            $university_institute_last_word = array_pop($university_institute_title_words); // Extract the last word
                            $university_institute_remaining_title = implode(' ', $university_institute_title_words); // Reconstruct the remaining words
                        ?>
                        <i class="fas fa-user-graduate"></i>
                        <h2 class="section-title">
                            <?php echo esc_html($university_institute_remaining_title); ?>
                            <span class="big-font"><?php echo esc_html($university_institute_last_word); ?></span>
                        </h2>
                    <?php endif; ?>
                </div>
                <div class="serv-right">
                    <span class="view-more-button">
                        <?php if (get_theme_mod('university_institute_view_more_button_link') && get_theme_mod('university_institute_view_more_button_text', 'VIEW ALL COURSES')): ?>
                            <a href="<?php echo esc_url(get_theme_mod('university_institute_view_more_button_link')); ?>"><?php echo esc_html(get_theme_mod('university_institute_view_more_button_text', 'VIEW ALL COURSES')); ?></a>
                        <?php endif; ?>
                    </span>
                </div>
            </div>
            <div class="owl-carousel owl-theme">
              <?php
              $university_institute_post_category = get_theme_mod('university_institute_offer_section_category','uncategorized');
              if ($university_institute_post_category) :
                $university_institute_category_id = term_exists($university_institute_post_category, 'category');
                if ($university_institute_category_id !== 0 && $university_institute_category_id !== null) :
                  $university_institute_posts_to_show = get_theme_mod('university_institute_posts_to_show', 3);
                  $university_institute_page_query = new WP_Query(array(
                    'category_name' => esc_attr($university_institute_post_category),
                    'posts_per_page' => $university_institute_posts_to_show,
                  ));

                  $university_institute_post_count = 0;
                  if ($university_institute_page_query->have_posts()) :
                    while ($university_institute_page_query->have_posts()) : $university_institute_page_query->the_post(); 
                      $university_institute_post_count++;
              ?>
                      <div class="item">
                        <div class="serv-box">
                            <div class="post-main-img">
                                <div class="flex-row">
                                    <div class="post-thumbnail">
                                        <?php if (has_post_thumbnail()) : ?>
                                          <img src="<?php echo esc_url(get_the_post_thumbnail_url(null, 'full')); ?>" />
                                        <?php else : ?>
                                          <div class="serv-color"></div>
                                        <?php endif; ?>
                                        <?php if ($university_institute_courses_price = get_theme_mod('university_institute_courses_price' . $university_institute_post_count)) { ?>
                                                <p class="course-price"><?php echo esc_html($university_institute_courses_price); ?></p>
                                            <?php } ?>
                                    </div>
                                    <div class="post-content">
                                        <div class="post-top-content">
                                            <h3 class="text-capitalize"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                                            <p class="post-para"><?php echo wp_trim_words(get_the_content(), 15); ?></p>
                                        </div>
                                        <?php
                                        // Get theme mod values
                                        $university_institute_course_duration = get_theme_mod('university_institute_course_duration' . $university_institute_post_count);
                                        $university_institute_total_user = get_theme_mod('university_institute_total_user' . $university_institute_post_count);
                                        $university_institute_course_timing = get_theme_mod('university_institute_course_timing' . $university_institute_post_count);

                                        // Check if any one of them is available
                                        if ($university_institute_course_duration || $university_institute_total_user || $university_institute_course_timing) :
                                        ?>
                                            <div class="bottom-post-details">
                                                <div class="post-details-main">
                                                    <?php if ($university_institute_course_duration) { ?>
                                                        <p class="course-duration"><i class="fas fa-calendar-alt"></i> <?php echo esc_html($university_institute_course_duration); ?></p>
                                                    <?php } ?>
                                                    <?php if ($university_institute_total_user) { ?>
                                                        <p class="course-user"><i class="fas fa-user"></i> <?php echo esc_html($university_institute_total_user); ?></p>
                                                    <?php } ?>
                                                    <?php if ($university_institute_course_timing) { ?>
                                                        <p class="course-time"><i class="fas fa-clock"></i> <?php echo esc_html($university_institute_course_timing); ?></p>
                                                    <?php } ?>
                                                </div>
                                            </div>
                                        <?php endif; ?>
                                    </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>
        </section>
    <?php endif; ?>
</main>
<?php
get_footer();
?>