<?php
/**
 * Secondary search: list layout (same as calendar list view), first three items then optional expand.
 *
 * @var \WPEventGenius\Common\Displays\CalendarDisplay $calendar_display
 * @var array<\WPEventGenius\Common\Event\EventPost>   $events
 */

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

use WPEventGenius\Common\Event\EventPost;
use WPEventGenius\Common\Utils\Icon;
use WPEventGenius\Common\Utils\Templater;

if ( empty( $events ) || ! is_array( $events ) ) {
	return;
}

$templater                 = new Templater( 'list' );
$bulk_registration_enabled = $calendar_display->is_bulk_registration_enabled();
$factory                   = new \WPEventGenius\Common\Services\RegistrationObjectFactory();
$db                        = new \WPEventGenius\Common\Database();

$visible_events = array_slice( $events, 0, 3 );
$more_events    = array_slice( $events, 3 );
$more_count     = count( $more_events );
?>

<div class="evge<?php echo esc_attr( $templater->evge_classes() ); ?> evge-month-search-secondary-results-wrapper" data-evge-type="shortcode">
	<div class="evge-content">
		<div class="evge-events-archive-main<?php echo esc_attr( $templater->classes() ); ?>">
			<div class="evge-event-calendar evge-list-layout<?php echo esc_attr( $templater->calendar_classes() ); ?>">
				<?php
				foreach ( $visible_events as $event ) {
					$event_post = new EventPost( $event->get_the_id() );
					$event_post->set_registration_counter(
						$factory->create_registration_counter( $event->get_the_id(), $db )
					);
					EVGE()->template_manager()->get_template(
						'events/calendars/partials/list-item.php',
						[
							'event_post'                => $event_post,
							'bulk_registration_enabled' => $bulk_registration_enabled,
						]
					);
				}
				?>
				<?php if ( $more_count > 0 ) : ?>
					<div class="evge-calendar-secondary-search-more evge-hidden" aria-hidden="true">
						<?php
						foreach ( $more_events as $event ) {
							$event_post = new EventPost( $event->get_the_id() );
							$event_post->set_registration_counter(
								$factory->create_registration_counter( $event->get_the_id(), $db )
							);
							EVGE()->template_manager()->get_template(
								'events/calendars/partials/list-item.php',
								[
									'event_post'                => $event_post,
									'bulk_registration_enabled' => $bulk_registration_enabled,
								]
							);
						}
						?>
					</div>
					<div class="evge-series-events-load-more">
						<button
							type="button"
							class="evge-calendar-secondary-show-all evge-button evge-secondary evge-gray-button"
							aria-busy="false"
						>
							<span class="evge-icon-text">
								<?php Icon::output( 'plus' ); ?>
								<?php esc_html_e( 'Show all results', 'event-genius' ); ?>
							</span>
						</button>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>
