<?php
/**
 * Преподаватели (горизонтальный скролл)
 * .bento .pksh-section .pksh-section--teachers #teachers
 *
 * @package Dekart
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$data      = Pksh_Data::instance();
$teachers  = $data->get_teachers();
?>
<section class="bento pksh-section pksh-section--teachers" id="teachers" aria-label="Преподаватели">
	<div class="bento__full section-header--centered">
		<span class="bento-label">Преподаватели</span>
		<h2 class="bento-h2">Наши преподаватели — практикующие педагоги</h2>
		<p class="bento-p">Каждый педагог имеет опыт работы с дошкольниками от 5 лет. Все — действующие учителя начальных классов с высшим образованием.</p>

		<div class="pksh-swipe-hint" aria-hidden="true">
			<span class="pksh-swipe-hint__arrow">👇</span>
			<span class="pksh-swipe-hint__text">Листайте, чтобы познакомиться с преподавателями</span>
		</div>
	</div>
	<div class="bento__full">
		<div class="pksh-teachers__scroll-x">
			<div class="pksh-teachers__wrapper">
				<?php if ( is_array( $teachers ) && ! empty( $teachers ) ) : ?>
					<?php foreach ( $teachers as $item ) :
						$t_id    = $item['teacher_id'] ?? 0;
						$t_subj  = $item['teacher_subject'] ?? '';
						$t_about = $item['teacher_about'] ?? '';

						$t_post  = $t_id ? get_post( $t_id ) : null;
						$t_name  = $t_post ? $t_post->post_title : '—';

						$t_photo = 0;
						if ( $t_post ) {
							$t_photo = get_post_meta( $t_id, '_teacher_avatar', true );
							if ( ! $t_photo ) {
								$t_photo = get_post_meta( $t_id, '_thumbnail_id', true );
							}
						}

						if ( empty( $t_subj ) && $t_post ) {
							$t_subj = get_post_meta( $t_id, '_teacher_direction', true );
						}

						if ( empty( $t_about ) && $t_post ) {
							$t_about = get_post_meta( $t_id, '_teacher_about', true );
						}
					?>
						<div class="pksh-teachers__item">
							<div class="pksh-teachers__card">
								<div class="pksh-teachers__card-top">
									<?php if ( $t_photo ) : ?>
										<?php $photo_url = wp_get_attachment_image_url( $t_photo, array( 280, 280 ) ); ?>
										<img class="pksh-teachers__photo" loading="lazy" width="140" height="140"
											 src="<?php echo esc_url( $photo_url ?: get_template_directory_uri() . '/assets/images/no-photo.png' ); ?>"
											 alt="<?php echo esc_attr( $t_name ); ?>">
									<?php else : ?>
										<img class="pksh-teachers__photo" loading="lazy" width="140" height="140"
											 src="<?php echo get_template_directory_uri(); ?>/assets/images/no-photo.png"
											 alt="<?php echo esc_attr( $t_name ); ?>">
									<?php endif; ?>
								</div>
								<div class="pksh-teachers__card-body">
									<h3 class="pksh-teachers__name"><?php echo esc_html( $t_name ); ?></h3>
									<?php if ( $t_subj ) : ?>
										<p class="pksh-teachers__subj"><?php echo esc_html( $t_subj ); ?></p>
									<?php endif; ?>
									<?php if ( $t_post ) :
										$t_desc = get_post_meta( $t_id, '_teacher_description', true );
										if ( ! empty( $t_desc ) ) : ?>
											<p class="pksh-teachers__desc"><?php echo esc_html( wp_strip_all_tags( $t_desc ) ); ?></p>
										<?php endif; ?>
									<?php endif; ?>
									<div class="pksh-teachers__meta">
										<span class="pksh-teachers__meta-badge">👶 Работает с дошкольниками</span>
										<?php if ( $t_post ) :
											$exp_years = '';
											$start_year = get_post_meta( $t_id, '_teacher_start_year', true );
											if ( $start_year && is_numeric( $start_year ) ) {
												$exp = (int) date( 'Y' ) - (int) $start_year;
												if ( $exp > 0 ) {
													$exp_years = $exp . ' лет';
												}
											}
											if ( $exp_years ) : ?>
												<span class="pksh-teachers__meta-exp">📅 Стаж: <?php echo esc_html( $exp_years ); ?></span>
											<?php endif; ?>
										<?php endif; ?>
									</div>
									<button class="pksh-teachers__btn open-bitrix-form">Записаться к этому педагогу</button>
								</div>
							</div>
						</div>
					<?php endforeach; ?>
				<?php endif; ?>
			</div>
		</div>
	</div>
</section>
