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

$appointment_management_content_ids  = array();
$appointment_management_content_type = get_theme_mod( 'appointment_management_service_content_type', 'post' );

for ( $appointment_management_i = 1; $appointment_management_i <= 4; $appointment_management_i++ ) {
  $appointment_management_content_ids[] = get_theme_mod( 'appointment_management_service_content_' . $appointment_management_content_type . '_' . $appointment_management_i );
}

// Get the category for the services slider from theme mods or a default category
$appointment_management_services_category = get_theme_mod('appointment_management_services_category', 'services');

// Modify query to fetch posts from a specific category
$appointment_management_services_args = array(
    'post_type'           => $appointment_management_content_type,
    'post__in'            => array_filter( $appointment_management_content_ids ),
    'orderby'             => 'post__in',
    'posts_per_page'      => absint(4),
    'ignore_sticky_posts' => true,
);

// Apply category filter only if content type is 'post'
if ( 'post' === $appointment_management_content_type && ! empty( $appointment_management_services_category ) ) {
    $appointment_management_services_args['category_name'] = $appointment_management_services_category;
}

$appointment_management_services_args = apply_filters( 'appointment_management_service_section_args', $appointment_management_services_args );

appointment_management_render_service_section( $appointment_management_services_args );

/**
 * Render Service Section.
 */

 function appointment_management_render_service_section( $appointment_management_services_args ) {
	$appointment_management_service_button_label = get_theme_mod( 'appointment_management_service_button_label_', '' );
	$appointment_management_service_button_link  = get_theme_mod( 'appointment_management_service_button_link_', '' );
 ?>
  <section id="appointment_management_service_section" class="asterthemes-frontpage-section service-section service-style-1">
    <?php
    if ( is_customize_preview() ) :
      appointment_management_section_link( 'appointment_management_service_section' );
    endif;

    $appointment_management_trending_product_heading = get_theme_mod( 'appointment_management_trending_product_heading');
    ?>
    <div class="asterthemes-wrapper">
      
      <div class="services-inner">
        <?php if ( ! empty( $appointment_management_trending_product_heading ) ) { ?>
          <h3><?php echo esc_html( $appointment_management_trending_product_heading ); ?></h3>
        <?php } ?>
      </div>
      <div class="video-main-box">
        <?php 
        $appointment_management_query = new WP_Query( $appointment_management_services_args );
        if ( $appointment_management_query->have_posts() ) :
          ?>
          <div class="section-body">
            <div class="service-section-wrapper">
              <?php
              $appointment_management_i = 1;
              while ( $appointment_management_query->have_posts() ) :
                $appointment_management_query->the_post();
                ?>
                <div class="service-single">
				  <div class="service-image">
						<?php if ( has_post_thumbnail() ) : ?>
							<?php the_post_thumbnail( 'full' ); ?>
						<?php else : ?>
							<img src="<?php echo esc_url( get_template_directory_uri() ); ?>/resource/img/default.png" />
						<?php endif; ?>
                    </div>
                  <div class="service-title">
                    <h3>
                      <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                      </a>
                    </h3>
                  </div>
				  <div class="service-content">
				    <div class="mag-post-excerpt">
						<?php the_excerpt(); ?>
					</div>
				  </div>
                </div>
                <?php
                $appointment_management_i++;
              endwhile;
              wp_reset_postdata();
              ?>
            </div>
          </div>
        <?php endif; ?>
      </div>
	  <?php if ( ! empty( $appointment_management_service_button_label ) ) { ?>
		<div class="banner-slider-btn">
			<a href="<?php echo esc_url( $appointment_management_service_button_link ); ?>" class="asterthemes-button"><?php echo esc_html( $appointment_management_service_button_label ); ?></a>
		</div>
	  <?php } ?>
    </div>
  </section>
  <?php
}