<?php
/**
 * Default Wohno listing-card template.
 *
 * To customise the markup, copy this file into your (child) theme as
 * `wohno/listing-card.php` — the plugin will use yours instead.
 *
 * A click opens an in-page detail modal (rendered from `data-wohno-listing`);
 * without JavaScript the card link falls back to the canonical detail page on
 * wohno.de, where the application also takes place.
 *
 * Provided variable:
 *
 * @var array{
 *     title:string, url:string, image:string, image_alt:string, place:string,
 *     meta:string, rooms:string, area:string, price:string, price_note:string,
 *     images:array<int,array{url:string,alt:string}>, payload:string
 * } $card Raw (unescaped) card fields — always escape on output.
 *
 * @package Wohno\Plugin
 */

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

$wohno_title  = isset( $card['title'] ) ? (string) $card['title'] : '';
$wohno_rooms  = isset( $card['rooms'] ) ? (string) $card['rooms'] : '';
$wohno_area   = isset( $card['area'] ) ? (string) $card['area'] : '';
$wohno_note   = isset( $card['price_note'] ) ? (string) $card['price_note'] : '';
$wohno_images = isset( $card['images'] ) && is_array( $card['images'] ) ? $card['images'] : array();

// Fall back to the single cover field when no gallery array is present.
if ( array() === $wohno_images && ! empty( $card['image'] ) ) {
	$wohno_images = array(
		array(
			'url' => (string) $card['image'],
			'alt' => isset( $card['image_alt'] ) ? (string) $card['image_alt'] : $wohno_title,
		),
	);
}
$wohno_count = count( $wohno_images );
?>
<article class="wohno-card" data-wohno-listing="<?php echo esc_attr( isset( $card['payload'] ) ? (string) $card['payload'] : '' ); ?>">
	<div class="wohno-card__media">
		<div class="wohno-card__gallery" data-wohno-gallery>
			<?php if ( $wohno_count > 0 ) : ?>
				<?php foreach ( $wohno_images as $wohno_i => $wohno_img ) : ?>
					<img class="wohno-card__img<?php echo 0 === $wohno_i ? ' is-active' : ''; ?>"
						src="<?php echo esc_url( (string) $wohno_img['url'] ); ?>"
						alt="<?php echo esc_attr( '' !== (string) $wohno_img['alt'] ? (string) $wohno_img['alt'] : $wohno_title ); ?>"
						loading="lazy" data-wohno-slide="<?php echo (int) $wohno_i; ?>" />
				<?php endforeach; ?>
			<?php else : ?>
				<div class="wohno-card__img wohno-card__img--empty" role="img" aria-label="<?php echo esc_attr( $wohno_title ); ?>"></div>
			<?php endif; ?>
		</div>

		<?php if ( $wohno_count > 1 ) : ?>
			<button type="button" class="wohno-card__nav wohno-card__nav--prev" data-wohno-prev aria-label="<?php echo esc_attr__( 'Vorheriges Bild', 'wohno' ); ?>">&#8249;</button>
			<button type="button" class="wohno-card__nav wohno-card__nav--next" data-wohno-next aria-label="<?php echo esc_attr__( 'Nächstes Bild', 'wohno' ); ?>">&#8250;</button>
			<div class="wohno-card__dots" data-wohno-dots>
				<?php foreach ( $wohno_images as $wohno_i => $wohno_img ) : ?>
					<button type="button" class="wohno-card__dot<?php echo 0 === $wohno_i ? ' is-active' : ''; ?>"
						data-wohno-dot="<?php echo (int) $wohno_i; ?>"
						aria-label="<?php echo esc_attr( sprintf( /* translators: %d: image number */ __( 'Bild %d', 'wohno' ), (int) $wohno_i + 1 ) ); ?>"></button>
				<?php endforeach; ?>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $card['price'] ) ) : ?>
			<div class="wohno-card__price">
				<span class="wohno-card__price-value"><?php echo esc_html( $card['price'] ); ?></span>
				<?php if ( '' !== $wohno_note ) : ?>
					<span class="wohno-card__price-note"><?php echo esc_html( $wohno_note ); ?></span>
				<?php endif; ?>
			</div>
		<?php endif; ?>
	</div>

	<div class="wohno-card__body">
		<h3 class="wohno-card__title">
			<a class="wohno-card__link" href="<?php echo esc_url( $card['url'] ?? '' ); ?>" data-wohno-open><?php echo esc_html( $wohno_title ); ?></a>
		</h3>
		<?php if ( '' !== (string) ( $card['place'] ?? '' ) ) : ?>
			<p class="wohno-card__place"><?php echo esc_html( $card['place'] ); ?></p>
		<?php endif; ?>
		<?php if ( '' !== $wohno_rooms || '' !== $wohno_area ) : ?>
			<p class="wohno-card__meta">
				<?php if ( '' !== $wohno_rooms ) : ?>
					<span class="wohno-card__stat wohno-card__stat--rooms"><?php echo esc_html( $wohno_rooms ); ?></span>
				<?php endif; ?>
				<?php if ( '' !== $wohno_area ) : ?>
					<span class="wohno-card__stat wohno-card__stat--area"><?php echo esc_html( $wohno_area ); ?></span>
				<?php endif; ?>
			</p>
		<?php elseif ( '' !== (string) ( $card['meta'] ?? '' ) ) : ?>
			<p class="wohno-card__meta"><?php echo esc_html( $card['meta'] ); ?></p>
		<?php endif; ?>
	</div>
</article>
