<?php
/**
 * Event Grid block — server-side render.
 *
 * @var array    $attributes Block attributes.
 * @var string   $content    Block content.
 * @var WP_Block $block      Block instance.
 *
 * @package Northfield_Theatre
 * @since 1.0.0
 */

$heading      = $attributes['heading'] ?? '';
$count        = (int) ( $attributes['count'] ?? 6 );
$columns      = (int) ( $attributes['columns'] ?? 3 );
$category     = $attributes['category'] ?? '';
$venue        = $attributes['venue'] ?? '';
$featured     = $attributes['featured'] ?? false;
$show_view_all = $attributes['showViewAll'] ?? true;

$query_args = array(
	'limit'   => $count,
	'orderby' => 'date',
	'order'   => 'ASC',
);

if ( ! empty( $category ) ) {
	$query_args['category'] = $category;
}

if ( ! empty( $venue ) ) {
	$query_args['venue'] = $venue;
}

if ( $featured ) {
	$query_args['featured'] = true;
}

$events = northfield_get_events( $query_args );

if ( empty( $events ) ) {
	return;
}

$grid_class = 'event-grid event-grid--' . $columns;
?>
<div <?php echo get_block_wrapper_attributes(); ?>>
	<?php if ( ! empty( $heading ) ) : ?>
		<h2 class="event-grid-block__heading"><?php echo esc_html( $heading ); ?></h2>
	<?php endif; ?>

	<div class="<?php echo esc_attr( $grid_class ); ?>">
		<?php foreach ( $events as $event ) : ?>
			<?php get_template_part( 'template-parts/content', 'event-card', array( 'event' => $event ) ); ?>
		<?php endforeach; ?>
	</div>

	<?php if ( $show_view_all ) : ?>
		<div class="event-grid-block__cta" style="text-align: center; margin-top: var(--space-2xl);">
			<a href="<?php echo esc_url( home_url( '/whats-on/' ) ); ?>" class="btn btn--outline">
				<?php esc_html_e( 'View all events', 'northfield-theatre' ); ?>
			</a>
		</div>
	<?php endif; ?>
</div>
