<?php $collection = cms_collection($module['collection'] ?? 'houses'); ?>
<section class="section section--houses-premium" id="<?= e($module['anchor'] ?? 'houses') ?>">
  <div class="container">
    <div class="section-heading section-heading--premium">
      <div class="eyebrow"><?= e($module['eyebrow'] ?? '') ?></div>
      <h2><?= e($module['title'] ?? '') ?></h2>
      <p><?= e($module['text'] ?? '') ?></p>
    </div>
    <div class="houses-list houses-list--luxury">
      <?php foreach (($collection['items'] ?? []) as $index => $item): ?>
        <?php
          $gallery = $item['gallery'] ?? [];
          if (!$gallery && !empty($item['image'])) { $gallery = [$item['image']]; }
          $houseTitle = (string)($item['title'] ?? '');
          $houseSlug = slugify($houseTitle);
          $highlights = [];
          foreach (['capacity','area','view'] as $field) {
            if (!empty($item[$field])) { $highlights[] = (string)$item[$field]; }
          }
          $checkMeta = [];
          if (!empty($item['checkin_time'])) { $checkMeta[] = 'Заезд: ' . (string)$item['checkin_time']; }
          if (!empty($item['checkout_time'])) { $checkMeta[] = 'Выезд: ' . (string)$item['checkout_time']; }
          $leadText = trim((string)($item['full_description'] ?? $item['description'] ?? ''));
          $leadTextShort = function_exists('mb_substr') ? mb_substr($leadText, 0, 210) : substr($leadText, 0, 210);
          if ($leadTextShort !== $leadText) { $leadTextShort = rtrim($leadTextShort, " .,") . '…'; }
          $cover = $gallery[0] ?? ($item['image'] ?? '');
        ?>
        <article class="card house-card house-card--luxury" id="house-<?= e($houseSlug) ?>" data-house-card="<?= e($houseTitle) ?>" data-mobile-preview-src="<?= e($cover) ?>">
          <div class="house-card__media-shell">
            <div class="house-slider house-slider--luxury" data-house-slider>
              <div class="house-card__topline">
                <span class="house-card__label"><?= e($item['meta'] ?? 'Премиальный отдых') ?></span>
                <?php if (!empty($item['price'])): ?>
                  <span class="house-card__price-chip"><?= e($item['price']) ?></span>
                <?php endif; ?>
              </div>
              <?php foreach ($gallery as $gIndex => $image): ?>
                <button type="button" class="house-slide <?= $gIndex === 0 ? 'is-active' : '' ?>" data-open-details data-force-open-details="1" data-house="<?= e($houseTitle) ?>" onclick="if(window.SiteOpenHouseDetails){ window.SiteOpenHouseDetails(this.dataset.house); } return false;" aria-label="Открыть подробнее о домике <?= e($houseTitle) ?> — фото <?= (int)$gIndex + 1 ?>">
                  <img src="<?= e($image) ?>" alt="<?= e($houseTitle) ?> — фото <?= (int)$gIndex + 1 ?>" loading="lazy">
                </button>
              <?php endforeach; ?>
              <div class="house-card__media-overlay"></div>
              <?php if (count($gallery) > 1): ?>
                <button class="house-slider__nav house-slider__nav--prev" type="button" data-slide-prev aria-label="Предыдущее фото">‹</button>
                <button class="house-slider__nav house-slider__nav--next" type="button" data-slide-next aria-label="Следующее фото">›</button>
                <div class="house-slider__dots">
                  <?php foreach ($gallery as $gIndex => $image): ?>
                    <button type="button" class="<?= $gIndex === 0 ? 'is-active' : '' ?>" data-slide-dot="<?= (int)$gIndex ?>" aria-label="Фото <?= (int)$gIndex + 1 ?>"></button>
                  <?php endforeach; ?>
                </div>
              <?php endif; ?>
            </div>
          </div>

          <div class="house-card__body house-card__body--luxury">
            <div class="house-card__header">
              <div>
                <h3><?= e($houseTitle) ?></h3>
                <?php if (!empty($item['description'])): ?>
                  <p class="house-card__subtitle"><?= e($item['description']) ?></p>
                <?php endif; ?>
              </div>
            </div>

            <?php if ($highlights): ?>
              <div class="house-badges house-badges--luxury">
                <?php foreach ($highlights as $hl): ?><span><?= e($hl) ?></span><?php endforeach; ?>
              </div>
            <?php endif; ?>

            <?php if ($checkMeta): ?>
              <div class="house-times house-times--luxury">
                <?php foreach ($checkMeta as $tm): ?><span><?= e($tm) ?></span><?php endforeach; ?>
              </div>
            <?php endif; ?>

            <?php if ($leadTextShort !== ''): ?>
              <p class="house-card__lead"><?= e($leadTextShort) ?></p>
            <?php endif; ?>

            <?php if (!empty($item['features'])): ?>
              <ul class="feature-list feature-list--luxury">
                <?php foreach (array_slice($item['features'], 0, 6) as $feature): ?><li><?= e($feature) ?></li><?php endforeach; ?>
              </ul>
            <?php endif; ?>

            <div class="house-card__footer">
              <div class="house-card__footer-note">Нажмите на фото или кнопку «Подробнее», чтобы открыть полное описание домика.</div>
              <div class="module-actions module-actions--luxury">
                <button class="button button--ghost" type="button" data-open-details data-force-open-details="1" data-house="<?= e($houseTitle) ?>" onclick="if(window.SiteOpenHouseDetails){ window.SiteOpenHouseDetails(this.dataset.house); } return false;">Подробнее</button>
                <button class="button button--primary" type="button" data-open-booking data-force-open-booking="1" data-house="<?= e($houseTitle) ?>" onclick="window.SiteOpenBooking && window.SiteOpenBooking(this.dataset.house); return false;">Забронировать</button>
              </div>
            </div>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
  </div>
</section>
