<?php
/**
 * Template Name: Past Events Listing
 *
 * @package WordPress
 * @subpackage Charity
 * @since Charity 1.0
 */
?>
<?php get_header(); ?>
<?php
	$wplook_page_meta 			= get_post_meta($post->ID);
	$wplook_sidebar_position 	= isset($wplook_page_meta['wpl_cpt_sidebar_position'][0]) ? $wplook_page_meta['wpl_cpt_sidebar_position'][0] : "right";
	$wplook_page_sidebar  		= (isset($wplook_page_meta['wpl_cpt_sidebar_select'][0]) && $wplook_page_meta['wpl_cpt_sidebar_select'][0] != '') ? $wplook_page_meta['wpl_cpt_sidebar_select'][0] :  "event-1";
	$wplook_page_header = (isset($wplook_page_meta['wpl_page_header'][0]) && $wplook_page_meta['wpl_page_header'][0] != '') ? $wplook_page_meta['wpl_page_header'][0] : false;
 ?>
	<div class="main">
		<div class="main-head">
			<div class="main-head-image fullscreener">
				<?php
					if ( $wplook_page_header ) {
						echo '<img class="hide" src="'. esc_url( $wplook_page_header ).'">';
					}?>
			</div><!-- /.main-head-image fullscreener -->

			<div class="main-head-content">
				<div class="grid-container">
					<div class="grid-x grid-margin-x">
						<div class="cell small-12">
							<h1 class="main-title main-title-alt"><?php the_title(); ?></h1><!-- /.main-title main-title-alt -->
							<?php if (ot_get_option('wpl_breadcrumbs_status') == 'on') : ?>
								<ul class="breadcrumbs">
									<?php wplook_breadcrumbs(); ?>
								</ul><!-- /.breadcrumbs -->
							<?php endif; ?>
						</div><!-- /.cell small-12 -->
					</div><!-- /.grid-x grid-margin-x -->
				</div><!-- /.grid-container -->
			</div><!-- /.main-head-content -->
		</div><!-- /.main-head -->

		<?php
			$wplook_datenow =  date("Y-m-d H:i");
			$args = array(
			'post_type' => 'post_events',
			'post_status' => 'publish',
			'posts_per_page' => ot_get_option('wpl_events_per_page'),
			'paged'=> $paged,
			'orderby'				=> 'meta_value',
			'meta_key'				=> 'wpl_event_date_time',
			'order' => 'DESC',
			'meta_query' => array(
				array(
					'key'     => 'wpl_event_date_time',
					'value'   => $wplook_datenow,
					'compare' => '<=',
					),
				),
			);

			$wp_query = null;
			$wp_query = new WP_Query( $args );
		?>

		<div class="main-body">
			<div class="grid-container">
				<div class="grid-x grid-margin-x">

					<div class="content cell small-12 <?php if ( $wplook_sidebar_position == 'right') { echo "large-8";} elseif ( $wplook_sidebar_position == 'disable' ) {echo "large-12"; } else { echo " large-8";} ?>">
						<div class="grid-x grid-margin-x">
							<?php

								if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
									<?php
										$event_meta		= get_post_meta(get_the_ID());
										$wpl_event_date_time = isset($event_meta['wpl_event_date_time'][0]) ? $event_meta['wpl_event_date_time'][0] : false;
										$wpl_event_url = isset($event_meta['wpl_event_url'][0]) ? $event_meta['wpl_event_url'][0] : false;
										$wpl_event_address = isset($event_meta['wpl_event_address'][0]) ? $event_meta['wpl_event_address'][0] : false;
										$wpl_event_eventbrite_url = isset($event_meta['wpl_event_eventbrite_url'][0]) ? $event_meta['wpl_event_eventbrite_url'][0] : "";
										$wpl_event_custom_url = isset($event_meta['wpl_event_custom_url'][0]) ? $event_meta['wpl_event_custom_url'][0] : "";
									?>

									<div class="cell small-12 medium-6 <?php if ( $wplook_sidebar_position == 'disable' ) { echo "large-4"; } else { echo "large-6";}?>">
										<div class="event" itemscope itemtype="https://schema.org/Event">
											<div class="event-image">
												<a href="<?php the_permalink(); ?>">
													<?php
													if ( has_post_thumbnail() ) {
														the_post_thumbnail( 'medium-event', array( 'itemprop' => 'image' ) );
													}?>
												</a>
											</div><!-- /.event-image -->

											<h4 class="event-title">
												<a href="<?php the_permalink(); ?>" itemprop="name"><?php the_title(); ?></a>
											</h4><!-- /.event-title -->

											<div class="event-meta">
												<?php
													if ($wpl_event_date_time) {
														echo '<span><i class="far fa-calendar-alt"></i> ' . sprintf( _x( '%1$s at %2$s', '[date] at [time]', 'charity-wpl' ), date( get_option( 'date_format' ), strtotime( $wpl_event_date_time ) ), date( get_option( 'time_format' ), strtotime( $wpl_event_date_time ) ) ) . '</span>';
													}

													if ($wpl_event_address) {
														echo '<span><i class="fas fa-map-marker-alt"></i> '.$wpl_event_address.'</span>';
													}
												?>
											</div><!-- /.event-meta -->

											<div class="event-entry">
												<p itemprop="description">
													<?php echo wplook_short_excerpt(ot_get_option('wpl_events_excerpt_limit')); ?>
												</p>
											</div><!-- /.event-entry -->

											<div class="event-actions">
												<a href="<?php the_permalink(); ?>" class="button"><?php _e('More', 'charity-wpl'); ?></a>

												<?php if ($wpl_event_url) { ?>
													<a class="button button-event-social button-facebook" href="<?php echo esc_url( $wpl_event_url ); ?>"><i class="fab fa-facebook-f"></i></a>
												<?php } ?>
												<?php if ($wpl_event_eventbrite_url) { ?>
													<a class="button button-event-social button-eventbrite" href="<?php echo esc_url( $wpl_event_eventbrite_url ); ?>"><i class="far fa-calendar-alt"></i></a>
												<?php } ?>
												<?php if ($wpl_event_custom_url) { ?>
													<a class="button button-event-social button-custom" href="<?php echo esc_url( $wpl_event_custom_url ); ?>"><i class="fas fa-globe"></i></a>
												<?php } ?>
											</div><!-- /.event-actions -->
										</div><!-- /.event -->
									</div>

								<?php endwhile; ?>
								<?php else : ?>
									<div class="cell small-12 medium-6 <?php if ( $wplook_sidebar_position == 'disable' ) { echo "large-4"; } else { echo "large-6";}?>">
										<h2><?php _e('Sorry, no posts matched your criteria.', 'charity-wpl'); ?></h2>
										<p><?php _e('Try again later.', 'charity-wpl'); ?></p>
									</div>
								<?php endif; ?>
							<?php wp_reset_postdata(); ?>

						</div><!-- /.grid-x grid-margin-x -->

						<div class="grid-x grid-margin-x">
							<div class="cell">
								<?php wplook_pagination() ?>
							</div>
						</div><!-- /.grid-x grid-margin-x -->

					</div><!-- /.cell small-12 -->

					<?php if ( $wplook_sidebar_position != 'disable') { ?>
						<div id="sidebar" class="cell large-3 large-offset-1">
							<div class="sidebar">
								<?php dynamic_sidebar( $wplook_page_sidebar ); ?>
							</div>
						</div>
					<?php } ?>
				</div><!-- /.grid-x grid-margin-x -->
			</div><!-- /.grid-container -->

		</div><!-- /.main-body -->
	</div><!-- /.main -->
<?php get_footer(); ?>
