<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package uncode
 */

if(!is_null($post))
{
	$page_obj = get_page($post->ID);
}

$current_page_id = '';

/**
*	Get current page id
**/

if(!is_null($post) && isset($page_obj->ID))
{
    $current_page_id = $page_obj->ID;
}

get_header();

$is_standard_wp_post = FALSE;
$search_filter 	= false;
$search_trail 	= "";
$state 			= "";

if(isset($_GET['search_trail']) && $_GET['search_trail']!=""){
	$search_trail = $_GET['search_trail'];
	$search_filter = true;
}
if(isset($_GET['bystate']) && $_GET['bystate']!=""){
	$state = $_GET['bystate'];
	$search_filter = true;
}

if(isset($_GET['starts_with']) && $_GET['starts_with']!=""){
	$starts_with = esc_sql( $_GET['starts_with'] );
	$search_filter = true;
}


echo '<script type="text/javascript">UNCODE.initHeader();</script>';


	/** Display post html **/
	echo '<div class="page-body">
          <div class="post-wrapper">
          	<div class="post-body">';
?>

<div id="page_content_wrapper">
    <div class="inner">

    	<!-- Begin main content -->
    	<div class="inner_wrapper">
		
    			<div class="sidebar_content full_width blog_f">



<div class="row-container">
<div class="row row-parent style-light limit-width double-top-padding double-bottom-padding">

	<?php
	
	$f_args = array(
		'post_type'		=> 'trails',
		'posts_per_page'=> -1,
		'orderby'    => 'rand',
		'fields' => 'ids',
		'meta_query' => array(
			'relation'		=> 'and',
			array (
				'key'		=> 'featured',
				'value'   => 'a:1:{i:0;s:1:"1";}',
				'compare' => '=',
			),
		),
	);
	
	$f_posts = get_posts( $f_args );
	
	if ( !empty( $f_posts ) ) {
		?>
	<div class="featured_trails_wrap">
		<div class="row">
		<?php
		$i = 1;
		foreach( $f_posts as $fp ) {
			?>
			<div class="col-lg-3 col-md-25 col-sm-50 text-center">
				<div class="square">
					<div class="content text-center">
					<?php 
						$logo = get_field( 'logo', $fp );
						
						if ( $logo ) {
							echo '<a href="'. get_permalink( $fp ) .'">'.wp_get_attachment_image( $logo[ 'ID' ], 'medium' ).'</a>';
						}
										
					?>
					</div>
				</div>
				<?php
				echo '<h6 class="sidespace"><a href="'. get_permalink( $fp ) .'">'. get_the_title( $fp ) .'</a></h6>';
				?>
			</div>
			<?php
			if ( ( $i % 4 ) == false ) {
				?>
				</div>
				<div class="row">
				<?php
			}
			
			$i++;
		}
		?>
		</div>
	</div>
		<?php 
		
	}
	
	?>
	
	
	


		<div class="starts_with_wrap">
		<ul class="starts_with_list">
				<li><a href="#"  data-letter="">Reset</a></li>
			<?php foreach ( range('A', 'Z') as $letter ) { ?>
				
				<li><a href="#" data-letter="<?=$letter;?>"><?=$letter;?></a></li>
			
			<?php } ?>
		</ul>		
		</div>

<div id="search-wrapper">
	
		<?php
		
			$args = array( 'posts_per_page' => -1, 'post_type' => 'trails', 'orderby'    => 'post_title', 'order' => 'asc' );

			
			// if(!empty($state)) {
				// $args['tax_query'][] = array(
					// 'taxonomy'	=> 'state',
					// 'terms'		=> $state,
					// 'field'		=> 'slug'
				// );
			// }
			$rand_posts = get_posts( $args );

			
			
			$states_terms_args = array(
				'taxonomy' => 'state',
				'hide_empty' => false,
				'fields' => 'id=>slug'
			);
			// if( !empty( $filter_lake_name ) ) {
				// $states_of_lake = wp_get_post_terms ($rand_posts, 'state');
			// }
		
			$states_terms = get_terms( $states_terms_args );
		
		?>	
		<form action="" method="get">
			<input type="text" name="search_trail" id="search_trail" value="<?=$search_trail;?>" class="text" placeholder="search trail..." />
			<select class="drop" id="state01" name="bystate">
				<option value="" >State</option>
				<?php 

				if ( !empty( $states_terms ) && !is_wp_error( $states_terms ) ) {
					foreach($states_terms as $k =>$value)
					{
						echo "<option value='$value'";
						if(($value)==$state){echo " selected ";}
						echo ">" . strtoupper($value) . '</option>';
						
					}
				}
				?>
			</select>
			
			<select name="starts_with" id="starts_with_select">
			<?php
			foreach ( range('A', 'Z') as $letter ) {
				?>
				<option value=""></option>
				<option value="<?=$letter;?>" <?php selected( $starts_with, $letter ); ?>><?=$letter;?></option>
				<?php
			}
			?>
			</select>
			 
			<input class="button-red" type="submit" value="Search" id="filter_submit" />
		</form>
	</div>


<!-- Begin each blog post -->
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

	<div class="post_wrapper">
	    
	    <div class="post_content_wrapper">
	    
	    	<div class="post_header mixed">
	    	<div class="dtable post-listing">
    
    <div class="dheading">
        <div class="dcell">
            <p>Name</p>
        </div>
        <div class="dcell">
            <p>State</p>
        </div>
        
    </div>
    <?php 
	
	
	$paged = ( get_query_var( 'paged') ) ? absint( get_query_var( 'paged' ) ) : 1; 
	$args = array(
		'numberposts'	=> -1,
		'post_type'		=> 'trails',
		'paged' => $paged,
		'posts_per_page'=>35,
		'orderby'    => 'post_title',
		'order' => 'asc'
	);
	if($search_filter == true) {
		$args['meta_query']	= array(
			'relation'		=> 'and',
		);
		if(!empty($state)) {
			$args['meta_query'][] = array(
				'key'		=> 'state',
				'value'   => $state,
				'compare' => '=',
			);
		}
		if(!empty($search_trail)) {
			$args['s']	= $search_trail;
		}
		if(!empty($starts_with)) {
			$args['starts_with']	= $starts_with;
		}
	}
	
	$trails = new WP_Query( $args );
	//if (have_posts()) : while (have_posts()) : the_post();
	if ($trails->have_posts()) : while ($trails->have_posts()) : $trails->the_post();
	$alternate_names= get_post_meta( get_the_ID(),'alternate_names');
	$state= strtoupper( get_field('state') );
		
	?>

    <div class="drow">
        <div class="dcell">
            <a href="<?php the_permalink();?>"><?php echo titlechange(get_the_title());?></a>
            <br /><span class="post_info_date"><?php echo $alternate_names[0]; ?> </span>
        </div>
        <div class="dcell">
            <span class="post_info_date"><?php echo ( !empty( $state )  && $state != 'NULL' ? $state : '' ); ?> </span>
        </div>
    </div>
    <?php endwhile; endif; ?>
</div>
		
			</div>
			
	    </div>
	    
	</div>

</div>
<br class="clear"/>
<!-- End each blog post -->



	    	<?php /*?><?php 
				if(!isset($paged) OR empty($paged))
				{
					$paged = 1;
				}
			?>
			
	    	<div class="pagination"><div class="pagination_page"><?php echo esc_html($paged); ?></div><?php posts_nav_link(' ', '<i class="fa fa-angle-double-left"></i>'.esc_html__('Newer Posts', 'grandnews' ), esc_html__('Older Posts', 'grandnews' ).'<i class="fa fa-angle-double-right"></i>'); ?></div><?php */?>
    		
			</div>
    		
    	</div>
    <!-- End main content -->
	</div>
</div>
</div>
</div>
</div>

<?php

    echo       '</div>
        </div>';

// end of the loop.

get_footer();