<?php 
global $wp_locale;
?>
<?php get_header(); ?>
<div class="container-boxed max offset">
	<div class="row">
		<div class="<?php noo_main_class(); ?>" role="main">
			<?php while ( have_posts() ) : the_post(); ?>
			<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>			
				<header class="content-header clearfix">
					<h1 class="content-title">
						<?php the_title(); ?>
					</h1>
					<p class="content-meta">
						<?php if($trainer_ids = noo_get_post_meta(get_the_ID(),'_trainer')):?>
						<span>
							<?php _e('Trainer:','noo')?>
							<?php Noo_Class::get_trainer_list($trainer_ids);?>
						</span>
						<?php endif;?>
						<?php if($category_list = get_the_term_list(get_the_ID(), 'class_category',' ',', ')):?>
						<span>
							<?php _e('Category:','noo')?>
							<?php echo ($category_list)?>
						</span>
						<?php endif;?>
						<?php if($level_list = get_the_term_list(get_the_ID(), 'class_level',' ',', ')):?>
						<span>
							<?php _e('Level:','noo')?>
							<?php echo ($level_list)?>
						</span>
						<?php endif;?>
					</p>
				</header>
				<?php if( has_featured_content() ) : ?>
					<div class="content-featured">
						<?php noo_featured_default(); ?>
					</div>
				<?php endif; ?>
				<div class="content-wrap">
					<?php the_content(); ?>
					<?php wp_link_pages(); ?>
					<?php if(has_tag() && noo_get_option('noo_blog_post_show_post_tag',true)) : ?>
						<div class="entry-tags">
						<?php the_tags(sprintf('<i class="fa fa-tags"></i>'),',')?>
						</div>
					<?php endif;?>
					<?php $use_advanced_multi_time = noo_get_post_meta( get_the_ID(), "_use_advanced_multi_time", false ); ?>
					<?php if( $use_advanced_multi_time ) : ?>
						<div id="time-table" class="timetable_week">
							<h4><span><?php echo __('Timetable', 'noo'); ?></span></h4>
							<table>
								<tr>
									<th>&nbsp;</th>
									<th><?php echo __('Open Time', 'noo'); ?></th>
									<th><?php echo __('Close Time', 'noo'); ?></th>
									<th><?php echo __('Trainer', 'noo'); ?></th>
									<th><?php echo __('Address', 'noo'); ?></th>
								</tr>
								<?php
									$time_format = get_option('time_format');
									$trainer = (array) noo_json_decode($trainer_ids);
									$number_days = (array) noo_json_decode( noo_get_post_meta( get_the_ID(), "_number_day", '' ) );
									foreach ($number_days as $index => $day) :
										$day_class = 'day-' . ( $index % 2 ? 'odd' : 'even' );
										
										$new_open_id    = '_open_time_' . $day;

										$new_closed_id  = '_closed_time_' . $day;	
										$new_trainer_id = '_trainer_' . $day;
										$new_address_id = '_address_' . $day;
										
										$meta_open    = noo_get_post_meta( get_the_ID(), $new_open_id, '' );
										$meta_closed  = noo_get_post_meta( get_the_ID(), $new_closed_id, '' );
										$meta_trainer = noo_get_post_meta( get_the_ID(), $new_trainer_id, '' );
										$meta_address = noo_get_post_meta( get_the_ID(), $new_address_id, '' );
										
										$meta_open    = (array) noo_json_decode($meta_open);
										$meta_closed  = (array) noo_json_decode($meta_closed);
										$meta_trainer = (array) noo_json_decode($meta_trainer);
										$meta_address = (array) noo_json_decode($meta_address);

										if ( ($meta_open && $meta_open[0] == '') || !$meta_open ) {
											$meta_open[0] = noo_get_post_meta( get_the_ID(), "_open_time", '' );
										}
										if ( ($meta_closed && $meta_closed[0] == '') || !$meta_closed ) {
											$meta_closed[0] = noo_get_post_meta( get_the_ID(), "_close_time", '' );
										}

										if ( is_array($meta_open) && count($meta_open) > 0 ) :
											foreach ($meta_open as $k => $mopen) :
												$time_open_text = is_numeric( $mopen ) ? date( $time_format, $mopen ) : $mopen;
												$time_open = is_numeric( $mopen ) ? $mopen : strtotime( $mopen );

												$time_closed_text = '';
												$time_closed = '';
												if ( isset($meta_closed[$k]) ){
													$time_closed_text = is_numeric( $meta_closed[$k] ) ? date( $time_format, $meta_closed[$k] ) : $meta_closed[$k];
													$time_closed = is_numeric( $meta_closed[$k] ) ? $meta_closed[$k] : strtotime( $meta_closed[$k] );
												}
												?>
												<tr class="day_wrap <?php echo $day_class; ?>">
													<td><span class="day"><?php echo Noo_Class::_get_week_day($day, true); ?></span></td>
													<td><?php echo esc_attr( $time_open_text ); ?></td>
													<td><?php echo esc_attr( $time_closed_text ); ?></td>
													<td><?php echo (isset($meta_trainer[$k]) && is_numeric($meta_trainer[$k]) ) ? get_the_title($meta_trainer[$k]) : get_the_title($trainer[0]) ?></td>
													<td><?php echo (isset($meta_address[$k]) && $meta_address[$k] != '' ) ? esc_attr($meta_address[$k]) : noo_get_post_meta( get_the_ID(), "_address", '' ); ?></td>
												</tr>
												
												<?php		
											endforeach;
										endif;
									endforeach;
								?>
							</table>
						</div>
					<?php endif; ?>
					<div class="noo-social">
						<?php echo noo_social_share(get_the_ID(),true, 'noo_class'); ?>
					</div>
				</div>
				<?php if(apply_filters('noo_class_trainer_bio', true)):?>
					<footer class="content-footer">
					<?php
					$trainer_ids = (array) noo_json_decode($trainer_ids);
					foreach ( $trainer_ids as $trainer_id ) : ?>
					<div id="author-bio">
						<div class="author-avatar">
                            <?php echo get_the_post_thumbnail($trainer_id, 'thumbnail') ?>
						</div>
						<div class="author-info">
							<h4>
								<a title="<?php printf( __( 'Post by %s','noo'), get_the_title($trainer_id) ); ?>" href="<?php echo esc_url( get_permalink($trainer_id) ); ?>" rel="author">
									<?php echo get_the_title($trainer_id) ?>
								</a>
							</h4>
							<p>
								<?php echo noo_get_the_excerpt($trainer_id) ?>
							</p>
                            <a class="view-profile" title="<?php printf( __( 'Post by %s','noo'), get_the_title($trainer_id) ); ?>" href="<?php echo esc_url( get_permalink($trainer_id) ); ?>" rel="author"><i class="fa fa-share-alt"></i>&nbsp;<?php _e('View my profile', 'noo'); ?></a>
						</div>
					</div>
					<?php endforeach; ?>
					</footer>
					<?php endif;?>
				</article> <!-- /#post- -->
				<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'noo' ), 'after' => '</div>' ) ); ?>
				<?php if ( comments_open() ) : ?>
					<?php comments_template( '', true ); ?>
				<?php endif; ?>
			<?php endwhile; ?>
		</div>
		<div class="<?php noo_sidebar_class(); ?> hidden-print">
		    <?php if(is_singular('noo_class')): ?>
		        <?php
		        //Class Information Location
		        $class_address		= noo_get_post_meta( get_the_ID(), "_address", '' );
		        $number_of_weeks	= noo_get_post_meta( get_the_ID(), "_number_of_weeks", '' );
		        $open_date		    = noo_get_post_meta( get_the_ID(), "_open_date", '' );
		        $number_days		= (array) noo_json_decode( noo_get_post_meta( get_the_ID(), "_number_day", '' ) );
		        $open_time		    = noo_get_post_meta( get_the_ID(), "_open_time", '' );
		        $close_time		    = noo_get_post_meta( get_the_ID(), "_close_time", '' );
		        $register_link		= noo_get_post_meta( get_the_ID(), "_register_link", '' );

		        // Get Open Date & Next Date
		        $args = array(
					'open_date'       => $open_date,
					'number_of_weeks' => $number_of_weeks,
					'number_days'     => $number_days
		        );
		        $class_dates = Noo_Class::get_open_date_display( $args );
		        ?>
		        <div class="single-sidebar">
		            <h4 class="widget-title"><?php noo_nth_word(esc_html__('Class Information','noo'),'first');?></h4>
		            <div class="class-info-sidebar">
		            	<?php if( !$use_advanced_multi_time ) : ?>
							<?php if( !empty( $class_address ) ) : ?>
								<div class="clearfix"><i class="fa fa-map-marker"></i>&nbsp;<?php echo esc_html($class_address); ?></div>
							<?php endif; ?>
						<?php endif; ?>
		                <?php if( !empty( $number_of_weeks ) ) : ?>
		                    <div class="clearfix"><i class="fa fa-file-text-o"></i>&nbsp;<?php echo _e('Number of Week','noo');?><span><?php echo esc_html($number_of_weeks); ?></span></div>
		                <?php endif; ?>
		                <?php if( !empty( $class_dates['open_date'] ) ) : ?>
		                    <div class="clearfix"><i class="fa fa-calendar"></i>&nbsp;<?php echo __('Open:', 'noo'); ?> <?php echo esc_html(date_i18n(get_option('date_format'),$open_date)); ?></div>
		                <?php endif; ?>
		                <?php if( !empty( $class_dates['next_date'] ) ) : ?>
		                    <div class="clearfix"><i class="fa fa-calendar"></i>&nbsp;<?php echo __('Next:', 'noo'); ?> <?php echo esc_html(date_i18n(get_option('date_format'),$class_dates['next_date'])); ?></div>
		                <?php endif; ?>
		                <?php if( !empty( $number_days ) ) : 
		                	arsort( $number_days ); // Have to reverse order because of float right styling
		                ?>
		                    <div class="clearfix">
		                    	<i class="fa fa-check"></i>&nbsp;<?php echo _e('Days','noo');?>
		                    	<?php foreach ($number_days as $number_day) : ?>
		                    		<span><?php echo esc_html($wp_locale->get_weekday_abbrev($wp_locale->get_weekday($number_day))) ?></span>
								<?php endforeach; ?>
		                    </div>
		                <?php endif; ?>
		                <?php if( !$use_advanced_multi_time ) : ?>
			                <?php if( !empty( $open_time ) || !empty( $close_time ) ) :?>
			                	<div class="clearfix"><i class="fa fa-clock-o"></i>&nbsp;<?php echo date_i18n(get_option('time_format'),$open_time).'-'. date_i18n(get_option('time_format'),$close_time); ?></div>
			                <?php endif; ?>
			            <?php else : ?>
			            	<div class="clearfix"><a href="#time-table"><i class="fa fa-clock-o"></i>&nbsp;<?php echo __('Multiple time', 'noo'); ?> <i class="fa fa-long-arrow-right"></i></a></div>
			            <?php endif; ?>
		            </div>
		            <?php if( !empty( $register_link ) ) : ?>
		            	<a href="<?php echo esc_url( $register_link );?>" class="btn-primary register_button"><?php echo _e('Register Now','noo');?></a>
		            <?php endif; ?>
		        </div>
		    <?php endif;?>
		    
            <?php
            noo_related_class();
            ?>
		    <?php
			$sidebar = get_sidebar_id();
			if( ! empty( $sidebar ) ) :
			?>
			<div class="noo-sidebar-wrap">

				<?php // Dynamic Sidebar
				if ( ! function_exists( 'dynamic_sidebar' ) || ! dynamic_sidebar( $sidebar ) ) : ?>
					<!-- Sidebar fallback content -->
			
				<?php endif; // End Dynamic Sidebar sidebar-main ?>
			</div>
			<?php endif; // End sidebar ?> 
		</div>
	</div> <!-- /.row -->
</div> <!-- /.container-boxed.max.offset -->
<?php get_footer(); ?>