<?php
if ( ! get_theme_mod( 'online_school_courses_enable_service_section', false ) ) {
    return;
}

$online_school_courses_post_heading   = get_theme_mod( 'online_school_courses_trending_post_heading', '' );
$online_school_courses_services_category = get_theme_mod( 'online_school_courses_services_category', '' );

$online_school_courses_args = array(
    'posts_per_page' => 8,
    'post_type'      => 'courses',
);

if ( ! empty( $online_school_courses_services_category ) ) {
    $online_school_courses_args['tax_query'] = array(
        array(
            'taxonomy' => 'course-category',
            'field'    => 'term_id',
            'terms'    => absint( $online_school_courses_services_category ),
        ),
    );
}

$online_school_courses_args = apply_filters( 'online_school_courses_service_section_args', $online_school_courses_args );

$loop = new WP_Query( $online_school_courses_args );
?>

<section id="online_school_courses_service_section" class="asterthemes-frontpage-section service-section service-style-1 wow zoomInUp" data-wow-duration="1.5s">
  <div class="asterthemes-wrapper">
    <?php if ( ! empty( $online_school_courses_post_heading ) ) : ?>
      <h1 class="service-caption-title">
        <?php echo esc_html( $online_school_courses_post_heading ); ?>
      </h1>
    <?php endif; ?>
    
    <div class="service-section-wrapper">
      <?php if ( $loop->have_posts() ) : ?>
        <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
          <div class="courses-box">          
            <div class="course-card">
              <div class="course-thumb">
                <a href="<?php the_permalink(); ?>">
                  <?php if ( has_post_thumbnail() ) : ?>
                    <?php the_post_thumbnail('medium'); ?>
                  <?php else : ?>
                    <img src="<?php echo esc_url( get_template_directory_uri() . '/resource/img/default.png' ); ?>" alt="<?php the_title_attribute(); ?>">
                  <?php endif; ?>
                </a>
              </div>
              <div class="course-content">
                <h4 class="course-title">
                  <?php the_title(); ?>
                </h4>
                <p class="course-excerpt">
                  <?php echo wp_trim_words( get_the_excerpt(), 20 ); ?>
                </p>
                <div class="course-btn tutor-course-pricing">
                  <div class="tutor-course-list-btn">
                    <a href="<?php the_permalink(); ?>">
                      <?php esc_html_e( 'Apply Now', 'online-school-courses' ); ?>
                    </a>
                  </div>
                </div>                  
              </div>
            </div>
          </div>
        <?php endwhile; wp_reset_postdata(); ?>
      <?php else : ?>
        <p><?php esc_html_e( 'No posts found in this category.', 'online-school-courses' ); ?></p>
      <?php endif; ?>
    </div>
  </div>
</section>
