<?php
/**
 * Template Name: Trails Template
 
 **/
get_header();

/**
 * DATA COLLECTION - START
 *
 */

/** Init variables **/
$limit_width = $limit_content_width = $the_content = $main_content = $layout = $bg_color = $sidebar_style = $sidebar_bg_color = $sidebar = $sidebar_size = $sidebar_sticky = $sidebar_padding = $sidebar_inner_padding = $sidebar_content = $title_content = $media_content = $navigation_content = $page_custom_width = $row_classes = $main_classes = $footer_content = $footer_classes = $content_after_body = '';
$with_builder = false;

$post_type = $post->post_type;

/** Get general datas **/
if (isset($metabox_data['_uncode_specific_style'][0]) && $metabox_data['_uncode_specific_style'][0] !== '') {
	$style = $metabox_data['_uncode_specific_style'][0];
	if (isset($metabox_data['_uncode_specific_bg_color'][0]) && $metabox_data['_uncode_specific_bg_color'][0] !== '') {
		$bg_color = $metabox_data['_uncode_specific_bg_color'][0];
	}
} else {
	$style = ot_get_option('_uncode_general_style');
	if (isset($metabox_data['_uncode_specific_bg_color'][0]) && $metabox_data['_uncode_specific_bg_color'][0] !== '') {
		$bg_color = $metabox_data['_uncode_specific_bg_color'][0];
	} else {
		$bg_color = ot_get_option('_uncode_general_bg_color');
	}
}
$bg_color = ($bg_color == '') ? ' style-'.$style.'-bg' : ' style-'.$bg_color.'-bg';


/** Get page width info **/
$boxed = ot_get_option('_uncode_boxed');

$page_content_full = (isset($metabox_data['_uncode_specific_layout_width'][0])) ? $metabox_data['_uncode_specific_layout_width'][0] : '';
if ($page_content_full === '') {

	/** Use generic page width **/
	$generic_content_full = ot_get_option('_uncode_' . $post_type . '_layout_width');
	if ($generic_content_full === '') {
		$main_content_full = ot_get_option('_uncode_body_full');
		if ($main_content_full === '' || $main_content_full === 'off') {
			$limit_content_width = ' limit-width';
		}
	} else {
		if ($generic_content_full === 'limit') {
			$generic_custom_width = ot_get_option('_uncode_' . $post_type . '_layout_width_custom');
			if ($generic_custom_width[1] === 'px') {
				if ( $generic_custom_width[0] == '' || ! is_numeric($generic_custom_width[0]) ) {
					$generic_custom_width[0] = 1200;
				}
				$generic_custom_width[0] = 12 * round(($generic_custom_width[0]) / 12);
			}
			if (is_array($generic_custom_width) && !empty($generic_custom_width)) {
				$page_custom_width = ' style="max-width: ' . implode("", $generic_custom_width) . '; margin: auto;"';
			}
		}
	}
} else {

	/** Override page width **/
	if ($page_content_full === 'limit') {
		$limit_content_width = ' limit-width';
		$page_custom_width = (isset($metabox_data['_uncode_specific_layout_width_custom'][0])) ? unserialize($metabox_data['_uncode_specific_layout_width_custom'][0]) : '';
		if (is_array($page_custom_width) && !empty($page_custom_width) && $page_custom_width[0] !== '') {
			if ($page_custom_width[1] === 'px') {
				$page_custom_width[0] = 12 * round(($page_custom_width[0]) / 12);
			}
			$page_custom_width = ' style="max-width: ' . implode("", $page_custom_width) . '; margin: auto;"';
		} else {
			$page_custom_width = '';
		}
	}
}

/** Collect header data **/
if (isset($metabox_data['_uncode_header_type'][0]) && $metabox_data['_uncode_header_type'][0] !== '') {
	$page_header_type = $metabox_data['_uncode_header_type'][0];
	if ($page_header_type !== 'none') {
		$meta_data = uncode_get_specific_header_data($metabox_data, $post_type, $featured_image);
		$metabox_data = $meta_data['meta'];
		$show_title = $meta_data['show_title'];
	}
} else {
	$page_header_type = ot_get_option('_uncode_' . $post_type . '_header');
	if ($page_header_type !== '' && $page_header_type !== 'none') {
		$metabox_data['_uncode_header_type'] = array($page_header_type);
		$meta_data = uncode_get_general_header_data($metabox_data, $post_type, $featured_image);
		$metabox_data = $meta_data['meta'];
		$show_title = $meta_data['show_title'];
	}
}

/** Get breadcrumb info **/
$generic_breadcrumb = ot_get_option('_uncode_' . $post_type . '_breadcrumb');
$page_breadcrumb = (isset($metabox_data['_uncode_specific_breadcrumb'][0])) ? $metabox_data['_uncode_specific_breadcrumb'][0] : '';
if ($page_breadcrumb === '') {
	$breadcrumb_align = ot_get_option('_uncode_' . $post_type . '_breadcrumb_align');
	$show_breadcrumb = ($generic_breadcrumb === 'off') ? false : true;
} else {
	$breadcrumb_align = (isset($metabox_data['_uncode_specific_breadcrumb_align'][0])) ? $metabox_data['_uncode_specific_breadcrumb_align'][0] : '';
	$show_breadcrumb = ($page_breadcrumb === 'off') ? false : true;
}

/** Get title info **/
$generic_show_title = ot_get_option('_uncode_' . $post_type . '_title');
$page_show_title = (isset($metabox_data['_uncode_specific_title'][0])) ? $metabox_data['_uncode_specific_title'][0] : '';
if ($page_show_title === '') {
	$show_title = ($generic_show_title === 'off') ? false : true;
} else {
	$show_title = ($page_show_title === 'off') ? false : true;
}

/**
 * DATA COLLECTION - END
 *
 */

while (have_posts()):
	the_post();

	/** Build header **/
	if ($page_header_type !== '' && $page_header_type !== 'none') {
		$page_header = new unheader($metabox_data, $post->post_title, $post->post_excerpt);

		$header_html = $page_header->html;
		if ($header_html !== '') {
			echo '<div id="page-header">';
			echo uncode_remove_p_tag( $page_header->html );
			echo '</div>';
		}

		if (!empty($page_header->poster_id) && $page_header->poster_id !== false && $media !== '') {
			$media = $page_header->poster_id;
		}
	}
	echo '<script type="text/javascript">UNCODE.initHeader();</script>';
	/** Build breadcrumb **/

	if ($show_breadcrumb && !is_front_page() && !is_home()) {
		if ($breadcrumb_align === '') {
			$breadcrumb_align = 'right';
		}
		$breadcrumb_align = ' text-' . $breadcrumb_align;

		$content_breadcrumb = uncode_breadcrumbs();
		$breadcrumb_title = '<div class="breadcrumb-title h5 text-bold">' . get_the_title() . '</div>';
		echo uncode_get_row_template($breadcrumb_title . $content_breadcrumb , '', ($page_custom_width !== '' ? ' limit-width' : $limit_content_width), $style, ' row-breadcrumb row-breadcrumb-' . $style . $breadcrumb_align, 'half', true, 'half');
	}
	$logo_content = '';
	if ( function_exists( 'get_field' ) ) {
		
		$logo = get_field( 'logo' );
		if ( !empty( $logo ) ) {
			
			$logo_content .= '<div class="trail_thumb">'.wp_get_attachment_image( $logo['ID'] , 'medium' ).'</div>';
		}
		
	}
	
	/** Build title **/

	if ($show_title) {
		$title_content .= apply_filters( 'uncode_before_body_title', '' );
		$title_content .= '<div class="post-title-wrapper"><h1 class="post-title">' . get_the_title() . '</h1>';
		// $title_content .= uncode_post_info();
		$title_content .= '</div>';
		$title_content .= apply_filters( 'uncode_after_body_title', '' );
	}


	

	/** Build post footer **/

	$page_show_share = (isset($metabox_data['_uncode_specific_share'][0])) ? $metabox_data['_uncode_specific_share'][0] : '';
	if ($page_show_share === '') {
		$generic_show_share = ot_get_option('_uncode_' . $post_type . '_share');
		$show_share = ($generic_show_share === 'off') ? false : true;
	} else {
		$show_share = ($page_show_share === 'off') ? false : true;
	}

	if ($show_share) {
		$footer_content = '<div class="post-share">
	          						<div class="detail-container margin-auto">
													<div class="share-button share-buttons share-inline only-icon"></div>
												</div>
											</div>';
	}

	

	/** Build and display navigation html **/
	$navigation_option = ot_get_option('_uncode_' . $post_type . '_navigation_activate');
	if ( $navigation_option !== 'off' && false ) {
		$generic_index = true;
		if (isset($metabox_data['_uncode_specific_navigation_index'][0]) && $metabox_data['_uncode_specific_navigation_index'][0] !== '') {
			$navigation_index = $metabox_data['_uncode_specific_navigation_index'][0];
			$generic_index = false;
		} else {
			$navigation_index = ot_get_option('_uncode_' . $post_type . '_navigation_index');
		}
		if ($navigation_index !== '') {
			$navigation_index_label = ot_get_option('_uncode_' . $post_type . '_navigation_index_label');
			$navigation_index_link = get_permalink($navigation_index);
			$navigation_index_btn = '<a class="btn btn-link text-default-color" href="' . esc_url($navigation_index_link) . '">' . ($navigation_index_label === '' ? get_the_title($navigation_index) : esc_html($navigation_index_label)) . '</a>';
		} else {
			$navigation_index_btn = '';
		}
		$navigation_nextprev_title = ot_get_option('_uncode_' . $post_type . '_navigation_nextprev_title');
		$navigation = uncode_post_navigation($navigation_index_btn, $navigation_nextprev_title, $navigation_index, $generic_index);
		if ($page_custom_width !== '') {
			$limit_content_width = ' limit-width';
		}
		if (!empty($navigation) && $navigation !== '') {
			$navigation_content = uncode_get_row_template($navigation, '', $limit_content_width, $style, ' row-navigation row-navigation-' . $style, true, true, true);
		}
	}
	
	/** Build post footer **/

	$page_show_share = (isset($metabox_data['_uncode_specific_share'][0])) ? $metabox_data['_uncode_specific_share'][0] : '';
	if ($page_show_share === '') {
		$generic_show_share = ot_get_option('_uncode_' . $post_type . '_share');
		$show_share = ($generic_show_share === 'off') ? false : true;
	} else {
		$show_share = ($page_show_share === 'off') ? false : true;
	}
	
// get the content
ob_start();
?>
<div class="inner">
		<!-- Begin main content -->
		<div class="inner_wrapper">
			<div class="sidebar_content full_width blog_f">
				<?php
	
	?>
<!-- Begin each blog post -->
<div id="post-<?php  the_ID(); ?>" <?php  post_class(); ?>>
	<div class="post_wrapper">
		<div class="post_content_wrapper">
			<div class="custom_fields custom_fields_light clear clearfix">
<?php 
  $division= get_field('division');
  $trail = get_field('trail');
  $landings = get_field('landing');
  $contact_persons_name = get_field('contact_persons_name');
  $phone = get_field('phone');
  $sponsors = get_field('sponsors');
  $email = get_field('email_address');
  $web_address = get_field('web_address');
  $description = get_field('large_description');
  $logo = get_field('logo');
  $state = get_field('state');
  //print_r($logo['sizes']['large'] );
?> 
<?php 


 if( !empty($logo) ) {
 
	$logo_img = '';
	if ( intval( $logo ) > 0 ) {
		$logo_img = wp_get_attachment_image( $logo, 'medium' );
	} elseif ( is_array( $logo ) ) {
		$logo_img = '<img src="'. $logo['sizes']['medium'] .'" alt="'. $trail .'"  style="border:none;" />';
	}
?>
<div class="acf-trail_logo" style=" background:#fff; border:none !important;">
	<?php echo $logo_img; ?>
</div>
 <?php } ?> 

 <div class="extra_fields" style="width:50%;"><?php if(!empty($division)){?><div class="extra_field"><strong>Division:&nbsp; </strong> <?php echo $division ;?></div> <?php }
if(!empty($phone)){?><div class="extra_field"><strong>Phone:&nbsp; </strong> <?php echo $phone ;?></div> <?php } if(!empty($contact_persons_name)){?><div class="extra_field"><strong>Contact:&nbsp; </strong> <?php echo $contact_persons_name ;?></div> <?php  }  if(!empty($email)){?><div class="extra_field"><strong>Email:&nbsp; </strong><a href="mailto:<?php echo $email ;?>"> <?php echo $email ;?></a></div> <?php  } if(!empty($sponsors)){?> <div class="extra_field"><strong>Sponsors:&nbsp; </strong><?php echo $sponsors ;?></div><?php } if(!empty($web_address)){?> <div class="extra_field"><strong>Website:&nbsp; </strong><a href="http://<?php echo $web_address ;?>" target="_blank"> <?php echo $web_address ;?></a></div><?php } if(!empty($state)){?> <div class="extra_field"><strong>State:&nbsp; </strong><?php echo $state ;?></div><?php } ?> 
 
 </div>
  </div>
 <div class="clear clearfix"></div>
 
 <?php
	$trail_post = new WP_Query( 
        array( 'post_type'		=>	'tournaments',
                    'meta_query' => array(
                        array(
                            'key' => 'trail',
                            'value' => get_the_ID(),
                            'compare' => '='
                        )
                    ),
                    'posts_per_page' => -1,
                    'nopaging' => true,
                    'meta_key' => 'from_date',
                    'orderby' => 'meta_value', 
                    'order' => 'ASC'
										)
				);
        
        // echo '<pre>'.print_r($trail_post, true).'</pre>';
      ?>
			<div class="dtable">
    
    <div class="dheading">
        <div class="dcell">
            <p>Tournaments</p>
        </div>
        <div class="dcell">
            <p>Trail</p>
        </div>
        <div class="dcell">
            <p>LAKE/LANDING</p>
        </div>
        
    </div>
    <?php 
	if( $trail_post->posts ):
	foreach( $trail_post->posts as $trailposts ): 
	setup_postdata( $trailposts );
	//print_r($trailposts);
	$from_date= get_post_meta($trailposts->ID,'from_date',true);
	$to_date= get_post_meta($trailposts->ID,'to_date',true);
	?>

    <div class="drow">
        <div class="dcell text_title">
            <p ><a href="<?php echo get_permalink($trailposts->ID);?>"><?php echo titlechange($trailposts->post_title);?></a>
            <br /><span class="post_info_date" style="text-transform:none;"><?php echo date_i18n('m/d/Y',strtotime($from_date)); ?> to 
			<?php echo date_i18n('m/d/Y',strtotime($to_date)); ?> </span>
                    </p>
        </div>
        <div class="dcell">
            <?php  
			//print_r(get_post_meta( get_the_ID()));
			$values= get_post_meta( $trailposts->ID,'trail');
			if(isset($values[0]) && is_numeric($values[0]) && $values[0] > 0) {
			$trailpost = new WP_Query(array( 'p' => $values[0],
											'post_type'=> 'trails' ));
			if ( $trailpost->have_posts() ) {
				
				while ( $trailpost->have_posts() ) {
					$trailpost->the_post();
					echo  '<a href="'.get_permalink().'" >'.titlechange(get_the_title()).'</a><br>';
					echo "<span>".get_field('division')."</span>";
				}
				
				/* Restore original Post Data */
				wp_reset_postdata();
			}
			}?>
        </div>
        <div class="dcell">
            <?php  
			$lakesvalues= get_post_meta( $trailposts->ID,'lake');
			$landingvalues= get_post_meta( $trailposts->ID,'landing');
			//print_r($lakesvalues);
			if(isset($lakesvalues[0]) && is_numeric($lakesvalues[0]) && $lakesvalues[0] > 0) {
			$lakespost = new WP_Query(array( 'p' => $lakesvalues[0],
											'post_type'=> 'lakes' ));
			if ( $lakespost->have_posts() ) {
				
				while ( $lakespost->have_posts() ) {
					$lakespost->the_post();
					echo  '<a href="'.get_permalink().'" >'.titlechange(get_the_title()).'</a>';
				}
				/* Restore original Post Data */
				wp_reset_postdata();
			}
			}
			
			//print_r($lakesvalues);
			if(isset($landingvalues[0]) && is_numeric($landingvalues[0]) && $landingvalues[0] > 0) {
			$landingpost = new WP_Query(array( 'p' => $landingvalues[0],
											'post_type'=> 'landings' ));
			if ( $landingpost->have_posts() ) {
				
				while ( $landingpost->have_posts() ) {
					$landingpost->the_post();
					echo  '<br><a href="'.get_permalink().'" ><span style="text-transform:uppercase;" class="post_info_date">'.titlechange(get_the_title()).'</span></a>';
				}
				
				/* Restore original Post Data */
				wp_reset_postdata();
			}
			}?>
        </div>
        
    </div>
    <?php endforeach; endif; ?>
</div>
<?php if(!empty($description)){ ?>
 			<div class="description_txt" style="color:#000;">
            <?php echo  $description; ?>
            </div>
<?php }?>
 
 <?php

 if ( $show_share ) {
	echo '<div class="post-share">
		<div class="detail-container margin-auto">
						<div class="share-button share-buttons share-inline only-icon"></div>
					</div>
				</div>';
}

				//Get related posts
			    $tg_blog_display_related = true;
			    
			    if($tg_blog_display_related)
			    {
			?>
			
			<?php
			//for use in the loop, list 9 post titles related to post's tags on current post
				   
			
			  	$args=array(
					  'post_type'		=>	'post',
			      	  'post__not_in' => array(get_the_ID()),
					  'meta_value'	=> get_the_ID(),'meta_key'		=> 'trail',
					  'showposts' => 12,
			      	  'orderby' => 'date',
			      	  'order' => 'DESC'
			  	 );
				// print_r(	$args);exit;
			  	$my_query = new WP_Query($args);
			  	$i_post = 1;
			  	
if( $my_query->have_posts() ) {
			?>
			 	<h1><?php  echo esc_html_e( 'Related Posts', 'grandnews' ); ?></h1>
				<hr class="post_divider"/><br class="clear"/>
				<div class="post_related">
				<div class="isotope-system">
					<div class="isotope-wrapper single-gutter">
						<div class="isotope-container isotope-layout style-masonry" data-type="masonry" data-layout="masonry" data-lg="800" >
				<?php
			while ($my_query->have_posts()) :
			$my_query->the_post();
			// $opinion= get_post_meta(the_ID());
			$opinion_piece= strtolower(trim(get_field('opinion_piece',get_the_ID())));
			//echo $opinion_piece;
			$last_class = '';
			
			if($i_post%3==0)   {
				$last_class = 'last';
			}

			$image_thumb = '';
			
			if(has_post_thumbnail(get_the_ID(), 'large')){
				$image_id = get_post_thumbnail_id(get_the_ID());
				$image_thumb = wp_get_attachment_image_src($image_id, 'large', true);
			}

			?>
		
					<div id="post-<?php  the_ID(); ?>" class="tmb tmb-iso-w3 tmb-iso-h4 tmb-round tmb-light tmb-text-showed tmb-content-center tmb-image-anim tmb-shadowed tmb-shadowed-sm tmb-entry-title-uppercase post-<?php  the_ID(); ?> status-publish hentry tmb-content-under tmb-iso <?php  echo esc_attr($last_class); ?>">
						<div class="t-inside style-light-bg">
							<div class="t-entry-text">
					   <!-- Begin each blog post -->
						
								<?php
			//Get post featured content
			
			if(!empty($image_thumb)){
				$small_image_url = wp_get_attachment_image_src($image_id, 'grandnews_blog_thumb', true);
				?>
								
								
									<div class="t-entry-visual" style="height: 252px;">
										<div class="t-entry-visual-tc">
											<div class="t-entry-visual-cont">
												<div class="dummy" style="padding-top: 66.7%;"></div>
												<a tabindex="-1" href="<?php  the_permalink(); ?>" class="pushed" target="_self" data-lb-index="2">
													<div class="t-entry-visual-overlay">
														<div class="t-entry-visual-overlay-in style-color-wayh-bg" style="opacity: 0.5;"></div>
													</div>
													<div class="t-overlay-wrap">
														<div class="t-overlay-inner">
															<div class="t-overlay-content">
																<div class="t-overlay-text half-block-padding">
																	<div class="t-entry t-single-line"></div>
																</div>
															</div>
														</div>
													</div>
												  <img src="<?php  echo esc_url($small_image_url[0]); ?>" alt="" class="related_img" /></a>
											</div>
										</div>
									</div>
								<?php
			} ?>
			
								<div class="t-entry-text">
									<div class="t-entry-text-tc half-block-padding">
										<div class="t-entry">
											<p class="t-entry-meta"><span class="t-entry-date"><?php  echo date_i18n(get_option( 'date_format' ), get_the_time('U')); ?></span></p>
											<h3 class="t-entry-title h5"><a href="<?php  the_permalink(); ?>" title="<?php  the_title(); ?>" target="_self"><?php  echo titlechange(get_the_title()); ?></a></h3></div>
									</div>
								</div>						
						
						<!-- End each blog post -->
						</div>
					</div>
				</div>

				 <?php
			$i_post++;
			endwhile;
			wp_reset_postdata();
			?>
				</div>
				</div>
			</div>
		</div>
            <?php
 }
			//}
			?>
			
			<?php
			    }
?>
			
			
		</div>
	</div>
</div>
<!-- End each blog post -->

</div>
</div>
</div>
<?php
	
$content = ob_get_clean();



	/** Display post html **/
	echo 	'<article id="post-'. get_the_ID().'" class="'.implode(' ', get_post_class('page-body' . $bg_color)) .'">
          <div class="post-wrapper">
          	<div class="post-body">
				<div class="row-container">
					<div class="row row-parent style-light limit-width double-top-padding double-bottom-padding">
			'. $logo_content .'
			'. $title_content .'
			'. $content .'
					</div>
				</div>
			</div>
		' .
          	$navigation_content . '
          </div>
        </article>';

endwhile;
// end of the loop.

get_footer(); ?>
