<?php
/**
 * Public view: event chooser / listing.
 *
 * @package BitForgeNet_Guest_Tickets
 *
 * @var array      $events       Event rows.
 * @var array|null $result       Submission result.
 * @var bool       $listing_only Whether to hide booking calls to action.
 */

defined( 'ABSPATH' ) || exit;

// This file is a template: it is included from a method -- never at file
// scope -- so everything declared here belongs to that method's variable
// scope and not to the global one. PHPCS reads each file on its own and has
// no way to see where the include happens, so it reads these as globals.
// Stated once, for the whole file, per this plugin's suppression rule.
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

$listing_only = isset( $listing_only ) ? (bool) $listing_only : false;
?>
<div class="bfnet">

	<?php if ( ! empty( $result['error'] ) ) : ?>
		<div class="bfnet-alert bfnet-alert--error" role="alert"><?php echo esc_html( $result['error'] ); ?></div>
	<?php endif; ?>

	<?php if ( empty( $events ) ) : ?>

		<div class="bfnet-empty">
			<h3><?php esc_html_e( 'No event is open right now', 'bitforgenet-guest-tickets' ); ?></h3>
			<p><?php esc_html_e( 'Our next night is being prepared. Come back soon, or follow us to be the first to know.', 'bitforgenet-guest-tickets' ); ?></p>
		</div>

	<?php else : ?>

		<div class="bfnet-cards">
			<?php foreach ( $events as $bfnet_event ) : ?>
				<?php
				$bfnet_venue = BFNET_Venue::get( $bfnet_event->venue_id );
				$bfnet_open  = BFNET_Event::registration_open( $bfnet_event );
				$bfnet_left  = BFNET_Event::seats_left( $bfnet_event );
				$bfnet_cover = BFNET_Event::cover_url( $bfnet_event, 'medium_large' );
				$bfnet_types = BFNET_Offers::tiers( $bfnet_event->id );
				$bfnet_from  = BFNET_Offers::price( $bfnet_event, $bfnet_types ? $bfnet_types[0] : null );
				$bfnet_url   = add_query_arg( 'bfnet_event', $bfnet_event->slug, remove_query_arg( array( 'bfnet_confirmed' ) ) );
				?>
				<article class="bfnet-card">
					<a class="bfnet-card__media" href="<?php echo esc_url( $bfnet_url ); ?>">
						<?php if ( $bfnet_cover ) : ?>
							<img src="<?php echo esc_url( $bfnet_cover ); ?>" alt="<?php echo esc_attr( $bfnet_event->title ); ?>" loading="lazy">
						<?php else : ?>
							<span class="bfnet-card__placeholder" aria-hidden="true"></span>
						<?php endif; ?>
						<span class="bfnet-card__date">
							<em><?php echo esc_html( BFNET_Util::date( $bfnet_event->starts_at, 'd' ) ); ?></em>
							<i><?php echo esc_html( BFNET_Util::date( $bfnet_event->starts_at, 'M' ) ); ?></i>
						</span>
					</a>

					<div class="bfnet-card__body">
						<?php if ( $bfnet_event->series ) : ?>
							<p class="bfnet-eyebrow"><?php echo esc_html( $bfnet_event->series ); ?></p>
						<?php endif; ?>

						<h3 class="bfnet-card__title"><a href="<?php echo esc_url( $bfnet_url ); ?>"><?php echo esc_html( $bfnet_event->title ); ?></a></h3>

						<p class="bfnet-card__meta">
							<?php echo esc_html( BFNET_Util::date( $bfnet_event->starts_at, 'l j F Y — H:i' ) ); ?>
							<?php if ( $bfnet_venue ) : ?>
								<span class="bfnet-block bfnet-muted"><?php echo esc_html( $bfnet_venue->name . ( $bfnet_venue->city ? ', ' . $bfnet_venue->city : '' ) ); ?></span>
							<?php endif; ?>
						</p>

						<?php if ( $bfnet_event->short_description ) : ?>
							<p class="bfnet-card__excerpt"><?php echo esc_html( BFNET_Util::excerpt( $bfnet_event->short_description, 140 ) ); ?></p>
						<?php endif; ?>

						<div class="bfnet-card__footer">
							<span class="bfnet-price">
								<?php if ( $bfnet_from > 0 ) : ?>
									<?php
									/* translators: %s: lowest ticket price. */
									echo esc_html( sprintf( __( 'from %s', 'bitforgenet-guest-tickets' ), BFNET_Util::money( $bfnet_from, $bfnet_event->currency ) ) );
									?>
								<?php else : ?>
									<?php esc_html_e( 'Free entry', 'bitforgenet-guest-tickets' ); ?>
								<?php endif; ?>
							</span>

							<?php if ( ! $listing_only ) : ?>
								<?php if ( $bfnet_open ) : ?>
									<a class="bfnet-btn" href="<?php echo esc_url( $bfnet_url ); ?>"><?php esc_html_e( 'Book a place', 'bitforgenet-guest-tickets' ); ?></a>
								<?php else : ?>
									<span class="bfnet-badge bfnet-badge--muted"><?php echo esc_html( 'sold_out' === $bfnet_event->status ? __( 'Sold out', 'bitforgenet-guest-tickets' ) : __( 'Closed', 'bitforgenet-guest-tickets' ) ); ?></span>
								<?php endif; ?>
							<?php endif; ?>
						</div>

						<?php if ( $bfnet_open && null !== $bfnet_left && $bfnet_left <= 20 ) : ?>
							<p class="bfnet-scarcity bfnet-scarcity--small">
								<?php
								/* translators: %d: seats remaining. */
								echo esc_html( sprintf( _n( 'Only %d place left', 'Only %d places left', $bfnet_left, 'bitforgenet-guest-tickets' ), $bfnet_left ) );
								?>
							</p>
						<?php endif; ?>
					</div>
				</article>
			<?php endforeach; ?>
		</div>

	<?php endif; ?>
</div>
