<?php
$count = -1;
if ( isset( $args['count'] ) ) {
	$count = (int) $args['count'];
}

$sc_teachers = get_query_var( 'sc_teachers' );
?>

<section class="our-teachers">
	<div class="container">
		<?php if ( $sc_teachers !== '1' ) : ?>
			<h2 class="page_h2">Наша команда</h2>
			<p class="our-teachers_text"><?php echo wp_kses_post( carbon_get_theme_option( 'teachers_block' ) ); ?></p>
		<?php endif; ?>

		<?php
		// Параметры запроса к базе данных
		$teachers_args = array(
			'posts_per_page' => $count,
			'orderby'        => 'date',
			'order'          => 'DESC',
			'post_type'      => 'teacher',
			'no_found_rows'  => true,
			'update_post_meta_cache' => false,
			'update_post_term_cache' => false,
		);

		// Получение списка преподавателей
		$teachers = get_posts( $teachers_args );
		?>

		<div class="teachers-owl-wrapper">
			<div class="swiper teachers-owl">
				<div class="swiper-wrapper">
					<?php foreach ( $teachers as $teacher ) : setup_postdata( $teacher );

						// Логика фильтрации временно отключена — раскомментируйте при необходимости
						$show = true;

						if ( $show ) :
							$avatar_id = carbon_get_post_meta( $teacher->ID, 'teacher_avatar' );
							$avatar_url = $avatar_id ? wp_get_attachment_url( $avatar_id ) : '';
							$alt_text   = $avatar_id ? get_post_meta( $avatar_id, '_wp_attachment_image_alt', true ) : '';

							// Разбиваем имя на части
							$full_name = get_the_title( $teacher->ID );
							$name_parts = explode( ' ', $full_name, 3 );
							if ( count( $name_parts ) >= 3 ) {
								$name_text = $name_parts[0] . '<br>' . $name_parts[1] . ' ' . $name_parts[2];
							} else {
								$name_text = esc_html( $full_name );
							}
							?>
							<div class="swiper-slide teachers-owl_item">
								<div class="ourteacher">
									<a href="<?php echo esc_url( get_permalink( $teacher->ID ) ); ?>" title="<?php echo esc_attr( $full_name ); ?>">
										<?php if ( $avatar_url ) : ?>
											<img loading="lazy" width="232" height="232"
											     src="<?php echo esc_url( $avatar_url ); ?>"
											     alt="<?php echo esc_attr( $alt_text ); ?>">
										<?php endif; ?>
									</a>
									<h3 class="ourteacher_title"><?php echo esc_html( carbon_get_post_meta( $teacher->ID, 'teacher_direction' ) ); ?></h3>
									<div class="ourteacher_name"><?php echo wp_kses_post( $name_text ); ?></div>
								</div>
							</div>
						<?php endif; ?>
					<?php endforeach; ?>
				</div>
			</div>

			<?php wp_reset_postdata(); ?>

			<div class="custom-button-next teachers-owl-next"></div>
			<div class="custom-button-prev teachers-owl-prev"></div>
		</div>

		<?php if ( $count !== -1 ) : ?>
			<div class="buttons">
				<a href="/prepodavateli/" class="btn-yellow-link">все преподаватели</a>
			</div>
		<?php endif; ?>
	</div>
</section>