<?php
/**
 * Template part for displaying slider section
 *
 * @package Online Education LMS
 * @subpackage online_education_lms
 */

$online_education_lms_slider_arrows = get_theme_mod('online_education_lms_slider_arrows', true);

?>
<?php if ($online_education_lms_slider_arrows) : ?>
  <section id="slider">
    <div class="container">
      <div class="item">
        <div class="row m-0">
          <div class="col-lg-5 col-md-5 col-12 slider-content-col align-self-center">
            <div class="carousel-caption">
              <div class="inner_carousel">
                <?php if (get_theme_mod('online_education_lms_slider_short_heading')) : ?>
                  <p class="slidetop-text mb-2 text-uppercase"><i class="fas fa-graduation-cap"></i> <?php echo esc_html(get_theme_mod('online_education_lms_slider_short_heading')); ?></p>
                <?php endif; ?>
                <?php if ( get_theme_mod('online_education_lms_show_slider_title') ) : 
                  $online_education_lms_slider_title = get_theme_mod('online_education_lms_show_slider_title');
                  $online_education_lms_words = explode(' ', $online_education_lms_slider_title);
                  if ( count($online_education_lms_words) >= 6 ) {
                      $online_education_lms_words[5] = '<span class="slider-middle-word">' . esc_html($online_education_lms_words[5]) . '</span>';
                      $online_education_lms_modified_title = implode(' ', $online_education_lms_words);
                  } else {
                      $online_education_lms_modified_title = esc_html($online_education_lms_slider_title);
                  }
                ?>
                    <h1 class="mb-md-3 mb-0"><?php echo wp_kses_post($online_education_lms_modified_title); ?></h1>
                <?php endif; ?>
                <?php if (get_theme_mod('online_education_lms_show_slider_content')) : ?>
                  <p class="slide-content"><?php echo esc_html(get_theme_mod('online_education_lms_show_slider_content')); ?></p>
                <?php endif; ?>
                <div class="more-btn mt-md-4 mt-2">
                  <?php if(get_theme_mod('online_education_lms_product_btn_text1','Explore Courses') != '' && get_theme_mod('online_education_lms_product_btn_link1')){ ?>
                    <a class="text-capitalize me-2 mb-3 slider-btn1" href="<?php echo esc_url(get_theme_mod('online_education_lms_product_btn_link1')); ?>"><?php echo esc_html(get_theme_mod('online_education_lms_product_btn_text1','Explore Courses')); ?><i class="fas fa-chevron-right ms-2"></i><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('online_education_lms_product_btn_text1','Explore Courses')); ?></span></a>
                  <?php }?>
                  <?php if(get_theme_mod('online_education_lms_product_btn_text2','Become an Instructor') != '' && get_theme_mod('online_education_lms_product_btn_link2')){ ?>
                    <a class="text-capitalize slider-btn2" href="<?php echo esc_url(get_theme_mod('online_education_lms_product_btn_link2')); ?>"><?php echo esc_html(get_theme_mod('online_education_lms_product_btn_text2','Become an Instructor')); ?><i class="fas fa-chevron-right ms-2"></i><span class="screen-reader-text"><?php echo esc_html(get_theme_mod('online_education_lms_product_btn_text2','Become an Instructor')); ?></span></a>
                  <?php }?>
                </div>
              </div>
            </div>
          </div>
          <div class="col-lg-4 col-md-3 slider-img-col">
            <?php
              $online_education_lms_banner_main_img = get_theme_mod('online_education_lms_banner_main_img');
              $online_education_lms_banner_main_img = get_stylesheet_directory_uri() . '/assets/images/slider-img.png'; 
            ?>
            <div class="<?php echo empty($online_education_lms_banner_main_img) ? 'no-image1' : ''; ?>">
              <?php if (!empty($online_education_lms_banner_main_img)) : ?>
                <img src="<?php echo esc_url($online_education_lms_banner_main_img); ?>" alt="<?php esc_attr_e('Background Image 1', 'online-education-lms'); ?>" />
              <?php else: ?>
                <img src="<?php echo esc_url($online_education_lms_banner_main_img); ?>" alt="<?php esc_attr_e('Default Background Image 1', 'online-education-lms'); ?>" />
              <?php endif; ?>
            </div>
            <?php 
              if ( get_theme_mod('online_education_lms_banner_main_tab_heading') !== '' ) : ?>
              <div class="banner-content-main">
                <?php 
                $online_education_lms_main_tab_heading = get_theme_mod('online_education_lms_banner_main_tab_heading', '');
                $online_education_lms_main_tab_para    = get_theme_mod('online_education_lms_banner_main_tab_para', '');
                $online_education_lms_icon_class       = get_theme_mod('online_education_lms_banner_main_tab_icon', 'fas fa-university');

                if ( $online_education_lms_main_tab_heading !== '' ) : ?>
                    <div class="main-abt-sentences">
                        <div class="main-banner-tab-details align-items-center">
                            <i class="<?php echo esc_attr($online_education_lms_icon_class); ?>"></i>
                            <div class="main-abt-content">
                                <h4 class="m-0"><?php echo esc_html($online_education_lms_main_tab_heading); ?></h4>
                                <?php if ( $online_education_lms_main_tab_para !== '' ) : ?>
                                    <p class="mb-0 mt-md-1 mt-0"><?php echo esc_html($online_education_lms_main_tab_para); ?></p>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
              </div>
            <?php endif; ?>
          </div>
          <div class="col-lg-3 col-md-4 align-self-center courses-col">
            <?php
            if ( get_theme_mod( 'online_education_lms_online_courses_enable', true ) ) { ?>
            <div class="online-courses">
                <?php 
                // Check if LearnPress Plugin is Active
                if ( class_exists( 'LearnPress' ) ) { ?>
                  <div class="owl-carousel">
                    <?php
                      $args = array(
                        'post_type'      => 'lp_course',
                        'posts_per_page' => absint( get_theme_mod( 'online_education_lms_online_courses_per_page' ) ),
                        'tax_query'      => taxonomy_exists( 'course_category' ) ? array(
                          array(
                            'taxonomy' => 'course_category',
                            'field'    => 'slug',
                            'terms'    => get_theme_mod( 'online_education_lms_online_courses_category' ),
                          ),
                        ) : '',
                        'order'          => 'ASC',
                      );

                      $loop = new WP_Query( $args );

                      // Loop Through Courses
                      if ( $loop->have_posts() ) {
                        while ( $loop->have_posts() ) {
                          $loop->the_post();
                          global $post;
                          $course   = LP_Global::course();
                          $lessons  = $course->get_items( LP_LESSON_CPT );
                          $lessons  = is_array( $lessons ) ? count( $lessons ) : 0;
                          $students = $course->count_students();
                          $duration = learn_press_get_post_translated_duration( get_the_ID() );
                          $duration = ! empty( $duration ) ? $duration : __( 'N/A', 'online-education-lms' );
                    ?>
                      <div class="courses-box text-md-start text-center">
                        <div class="img-course position-relative mb-3">
                          <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'full' ); ?></a>
                          <div class="save-top-btn">
                            <?php
                            $online_education_lms_save_icon_link = get_theme_mod('online_education_lms_save_link');

                            if ( !empty($online_education_lms_save_icon_link) ) { ?>
                                <a class="text-capitalize save-btn" href="<?php echo esc_url($online_education_lms_save_icon_link); ?>">
                                    <i class="fas fa-bookmark"></i>
                                </a>
                            <?php }
                            ?>
                          </div>
                          <div class="course-small-img">
                            <?php
                                $online_education_lms_courses_small_img = get_theme_mod('online_education_lms_courses_small_img');
                                $online_education_lms_courses_small_img = get_stylesheet_directory_uri() . '/assets/images/small-img.png'; 
                            ?>
                            <div class="<?php echo empty($online_education_lms_courses_small_img) ? 'no-image1' : ''; ?>">
                              <?php if (!empty($online_education_lms_courses_small_img)) : ?>
                                <img src="<?php echo esc_url($online_education_lms_courses_small_img); ?>" alt="<?php esc_attr_e('Background Image 1', 'online-education-lms'); ?>" />
                              <?php else: ?>
                                <img src="<?php echo esc_url($online_education_lms_courses_small_img); ?>" alt="<?php esc_attr_e('Default Background Image 1', 'online-education-lms'); ?>" />
                              <?php endif; ?>
                            </div>
                          </div>
                        </div>
                        <div class="courses-box-content">
                          <h3 class="p-0 mt-4 text-start">
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                          </h3>
                          <div class="row">
                            <div class="col-lg-6 col-md-6 col-6 align-self-center text-start">
                              <span class="course-duration text-start">
                                  <i class="far fa-clock me-2"></i><?php echo esc_html( $duration ); ?>
                                </span>
                            </div>
                            <div class="col-lg-6 col-md-6 col-6 align-self-center text-start">
                              <span class="lp-meta-students total-student text-start">
                                <!-- Student Icon Before -->
                                <i class="fas fa-user-graduate me-1"></i>
                                <?php
                                $online_education_lms_course_id = get_the_ID();
                                $online_education_lms_max_students = get_post_meta( $online_education_lms_course_id, '_lp_max_students', true );
                                $online_education_lms_student_text = $online_education_lms_max_students && $online_education_lms_max_students > 0 ? esc_html( $online_education_lms_max_students ) : esc_html__( 'Unlimited', 'online-education-lms' );
                                echo $online_education_lms_student_text . ' ' . esc_html__('students', 'online-education-lms');
                                ?>
                            </span>
                            </div>
                          </div>
                          <hr>
                          <div class="courses-info">
                            <div class="row">
                              <div class="col-lg-6 col-md-6 col-6 align-self-center">
                                <?php
                                  $online_education_lms_course_id = get_the_ID();
                                  $online_education_lms_sale_price = get_post_meta( $online_education_lms_course_id, '_lp_sale_price', true );

                                  if ( $online_education_lms_sale_price !== '' ) {
                                      // Show label with translation support
                                      echo '<div class="course-price-label text-start">' . esc_html__( 'Price', 'online-education-lms' ) . '</div>'; // Adjust text domain as needed
                                      // Show the price value
                                      echo '<div class="course-price-value text-start">' . esc_html( $online_education_lms_sale_price ) . '</div>';
                                  }
                                ?>
                              </div>
                              <div class="col-lg-6 col-md-6 col-6 align-self-center">
                               <span class="course-cat text-end"><?php echo wp_kses_post( get_the_term_list( get_the_ID(), 'course_category' ) ); ?></span>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    <?php 
                        } // End while loop
                      } else {
                        echo '<p>' . esc_html__( 'No courses found.', 'online-education-lms' ) . '</p>';
                      }
                      wp_reset_postdata(); 
                    ?>
                  </div>
                <?php } else { 
                  // If LearnPress is not active
                  echo '<p>' . esc_html__( 'LearnPress plugin is not active.', 'online-education-lms' ) . '</p>';
                } ?>
            </div>
            <?php } ?>
          </div>
        </div>
      </div>
    </div>
    <div class="clearfix"></div>
  </section>
<?php endif; ?>