<?php
/*
Template Name: Отзывы с формой
*/
get_header(); 
?>

<section class="section testimonials-section">
  <h2>Оставить отзыв</h2>
  <!-- Форма -->
  <form id="testimonial-form" class="testimonial-form">
    <div class="form-group">
      <label for="testimonial-name">Ваше имя</label>
      <input type="text" id="testimonial-name" name="name" required />
    </div>

    <div class="form-group">
      <label for="testimonial-text">Ваш отзыв</label>
      <textarea id="testimonial-text" name="content" rows="5" required></textarea>
    </div>

    <div class="form-group">
      <label for="testimonial-photo">Загрузите фото (необязательно)</label>
      <input type="file" id="testimonial-photo" name="photo" accept="image/*" />
    </div>

    <div class="form-group">
      <input type="checkbox" id="agree-policy" required />
      <label for="agree-policy">Я даю согласие на обработку персональных данных</label>
    </div>

    <button type="submit" class="btn-submit-testimonial">Отправить отзыв</button>
    <div class="form-message" id="form-message"></div>
  </form>

  <h2 style="margin-top: 3rem;">Отзывы наших гостей</h2>
  <?php
  $loop = new WP_Query(array(
      'post_type' => 'testimonial',
      'post_status' => 'publish',
      'posts_per_page' => -1
  ));

  if ($loop->have_posts()) : ?>
    <div class="testimonials-carousel">
      <div class="carousel-inner">
        <?php while ($loop->have_posts()) : $loop->the_post(); ?>
          <div class="testimonial-card">
            <div class="testimonial-photo">
              <?php the_post_thumbnail('medium', ['class' => 'testimonial-img']); ?>
            </div>
            <div class="testimonial-content">
              <p class="testimonial-text"><?php echo wp_trim_words(get_the_content(), 20); ?></p>
              <h4 class="testimonial-author"><?php the_title(); ?></h4>
            </div>
          </div>
        <?php endwhile; wp_reset_postdata(); ?>
      </div>
      <button class="carousel-control prev">‹</button>
      <button class="carousel-control next">›</button>
    </div>
  <?php else: ?>
    <p>Нет отзывов.</p>
  <?php endif; ?>
</section>

<?php get_footer(); ?>