<?php if ( ! defined( 'FW' ) ) {
	die( 'Forbidden' );
}

$number_of_events = $atts['number_of_events'] ? $atts['number_of_events'] : -1;
$view_all = $atts['view_all'];
$show_past_events = $atts['show_past_events'];

if ( $show_past_events == 'true' ) {
	$args = array(
		'post_type' => 'event',
		'posts_per_page' => $number_of_events,
		'meta_key' => 'event_date',
		'meta_type' => 'DATETIME',
		'orderby' => 'meta_value',
		'order' => 'asc',
		'paged' => 1
	);
} else {
	$args = array(
		'post_type' => 'event',
		'posts_per_page' => $number_of_events,
		'meta_key' => 'event_date',
		'meta_type' => 'DATETIME',
		'meta_value' => date( "Ymd" ),
		'meta_compare' => '>=',
		'orderby' => 'meta_value',
		'order' => 'asc',
		'paged' => 1
	);
}

$mobile_classes ='';

$mobile_classes .=  $atts['hide_on_desktop'] == 'true' ? 'hidden-md hidden-lg ' : '';
$mobile_classes .=  $atts['hide_on_mobile'] == 'true' ? 'hidden-xs hidden-sm' : '';

// WOW Animation
$wow_classes = '';
$wow_data = '';

if ( isset($atts['animation_on_scroll']) && $atts['animation_on_scroll']['animation_enable'] == 'yes') {
	$wow_classes = 'is-wow ' . $atts['animation_on_scroll']['yes']['animation_type'];

	if ( isset($atts['animation_on_scroll']['yes']['animation_delay']) ) {
		$wow_data =  'data-wow-delay="' . ($atts['animation_on_scroll']['yes']['animation_delay'] / 1000) . 's"';
	}
	
}
?>

<div class ="<?php echo esc_attr($mobile_classes); ?> <?php echo esc_attr($wow_classes); ?>" id="shortcode-<?php echo esc_attr($atts['id']); ?>" <?php echo wp_kses_post($wow_data); ?>>

<?php

	$events_query = new WP_Query($args); 
	?>

	<div class="EventsWrapper is-matchheight-group">

		<?php
		if ($events_query->have_posts()) : while ($events_query->have_posts()) : $events_query->the_post();

		$event_image = odrin_get_field('event_image');

		?>

		<div class="Event pos-r">

			<a href="<?php the_permalink(); ?>">
				<?php if ( $event_image ) : ?>
				<div class="event-content-wrapper section-light">
					<div class="bg-image" data-bg-image="<?php echo esc_url($event_image['sizes']['odrin_landscape_medium']); ?>"></div>
					<div class="overlay-dark"></div>
				<?php else : ?>
				<div class="event-content-wrapper section-dark">
				<?php endif; ?>
					<div class="event-date-wrapper pos-r">
						<?php 
							$date = new DateTime(odrin_get_field('event_date'));
							$timestamp = $date->getTimestamp();
							$date_number = $date->format("d");
							$date_month = $date->format("F");
						?>
						<span class="event-date-number font-heading"><?php echo wp_kses_post($date_number); ?></span>
						<span class="event-date-month"><?php echo wp_kses_post(date_i18n('F', $timestamp)); ?></span>
					</div>
					<div class="event-content pos-r">
						<h3 class="event-title"><?php the_title(); ?></h3>
						<?php if ( odrin_get_field('event_location') ) : ?>
							<div class="event-location special-subtitle-type-2"><?php echo wp_kses_post(odrin_get_field('event_location')); ?></div>
						<?php endif; ?>
					</div>
				</div> <!-- end event-content-wrapper -->
			</a>
		</div> <!-- end Event -->

		<?php
		endwhile;
		endif;
		wp_reset_postdata();
		?>

	</div> <!-- end EventsWrapper -->

	<?php if ( $view_all['view_all'] == 'true') : ?>
		<?php $parent_events_id = odrin_parent_events_id(); 
		if ( $parent_events_id ) : ?>
		<div class="events-show-more special-link text-right">
			<a href="<?php echo get_permalink($parent_events_id) ;?>"><?php echo wp_kses_post($view_all['true']['label']); ?></a>
		</div>
		<?php else : 
			if ( current_user_can('publish_pages') ) : ?>
			<span class="admin-alert"><?php echo wp_kses_post(__('<strong>Note!</strong> Make sure than you have published one (and only one) page that uses the Page Template "Events" in order to display the View All button.', 'odrin')); ?></span>
			<?php endif; ?>
		<?php endif; ?>
	<?php endif; ?>
	
</div><!-- end shortcode-id -->