<?php

// Get Events Dates
$dates_array = array();
$filter_args = array(
	'post_type' => 'event',
	'meta_key' => 'event_date',
	'meta_type' => 'DATETIME',
	'orderby' => 'meta_value',
	'order' => 'asc',
	'posts_per_page' => -1,
	'paged' => 1
	);

$filter_query = new WP_Query( $filter_args );

if ($filter_query->have_posts()) : while ($filter_query->have_posts()) : $filter_query->the_post();

	$full_date = new DateTime(odrin_get_field('event_date', get_the_ID()));
	$date = $full_date->format('F Y');
	
	array_push($dates_array, $date);

endwhile;endif;
wp_reset_postdata();

$dates_array = array_unique($dates_array);
?>

<?php if ( !empty($dates_array) ) : ?>
<div class="EventsFilter">
	<?php 

	if ( !empty($_GET['events_date']) ) {
		$event_date_current = $_GET['events_date'];
	} else {
		$event_date_current = '';
		$current = 'true';
	}

	if (odrin_get_field('filter_text')) : ?>
		<span class="events-filter-label special-subtitle-type-2 font-subheading"><?php echo wp_kses_post(odrin_get_field('filter_text')); ?></span>
	<?php endif; 

	?>
	<div id="events-filter" class="events-filter-dropdown dropdown">

		<button id="dropdown-menu-one-column" class="btn-dropdown" data-toggle="dropdown" ><?php esc_html_e('Date', 'odrin') ?></button>

		<ul class="dropdown-menu dropdown-menu-one-column" aria-labelledby="events-filter">
		<?php 
		if ( !isset($current) ) $current = false;  ?>
			<li>
				<a href="<?php echo get_permalink(); ?>" data-current="<?php echo esc_attr($current); ?>"><?php esc_html_e('Upcoming', 'odrin'); ?></a>
			</li>
		<?php
		foreach ( $dates_array as $date ) : ?>
			<?php 

			$parm_data = new DateTime($date);
			$timestamp = $parm_data->getTimestamp();
			$parm_data_format = $parm_data->format('Ym');

			if ( $parm_data_format ==  $event_date_current ) {
				$current = 'true';
			} else {
				$current = 'false';
			}

			?>
			<li>
				<a href="<?php echo add_query_arg('events_date', $parm_data_format, get_permalink()); ?>" data-current="<?php echo esc_attr($current); ?>"><?php echo wp_kses_post(date_i18n('F', $timestamp)); ?></a>
			</li>
		<?php endforeach; ?>
		</ul>

	</div>

</div>

<?php endif; ?>