<?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;
$state = "";
$filter_lake_name = "";
$filter_lake_type = "";
if(isset($_GET['bystate']) && $_GET['bystate']!=""){
	$state = $_GET['bystate'];
	$search_filter = true;
}
if(isset($_GET['filter_lake_name']) && $_GET['filter_lake_name']!=""){
	$filter_lake_name = $_GET['filter_lake_name'];
	//$search_filter = true;
}
if(isset($_GET['filter_lake_type']) && $_GET['filter_lake_type']!=""){
	$filter_lake_type = $_GET['filter_lake_type'];
	$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 id="lakesmap"></div>

    			<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">
<div id="search-wrapper">
	
		<?php
		
			$args = array( 'posts_per_page' => -1, 'post_type' => 'lakes', 'orderby'    => 'post_title', 'order' => 'asc' );
			
			$args['meta_query'][] = array(
				'key'		=> 'hotel_logo',
				'compare'	=> 'EXISTS'
			);
			$args['meta_query'][] = array(
				'key'		=> 'hotel_logo',
				'value'   => array(''),
				'compare' => 'NOT IN',
			);
			$args['meta_query'][] = array(
				'key'		=> 'hotel_booking_link',
				'compare'	=> 'EXISTS',
			);
			$args['meta_query'][] = array(
				'key'		=> 'hotel_booking_link',
				'value'   => array(''),
				'compare' => 'NOT IN',
			);
			
			// 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">
			 <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='$k'";
						if(strtoupper($value)==$state){echo " selected ";}
						echo ">" . strtoupper($value) . '</option>';
						
					}
				}
				?>
			 </select>
			 <?php /* 
			 <input type="text" class="drop" name="filter_lake_name" placeholder="Search by lake name" value="<?php echo $filter_lake_name; ?>"  style="width:30%;">
			 <input type="text" class="drop" name="filter_lake_type" placeholder="Search by lake type" value="<?php echo $filter_lake_type; ?>" style="width:30%;"/>
			 */ ?>
			 <select class="drop" id="lake02" name="filter_lake_name">
			<option value="" >Select Lake</option>
			<?php 
			
			foreach ( $rand_posts as $post ) : 
				setup_postdata( $post ); 
				
					echo "<option value='$post->ID'";
					if($post->ID==$filter_lake_name){echo " selected ";}
					echo ">" .$post->post_title . '</option>';
			endforeach; 
			wp_reset_postdata(); 
			
			?>
			</select>
			<input class="button-red" type="submit" value="Search" />
		</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>Type/State</p>
        </div>
        <div class="dcell">
            <p>Size</p>
        </div>
        
    </div>
    <?php 
	
	
	$paged = ( get_query_var( 'paged') ) ? absint( get_query_var( 'paged' ) ) : 1; 
	$args = array(
		'numberposts'	=> -1,
		'post_type'		=> 'lakes',
		'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['tax_query'][] = array(
				'taxonomy'	=> 'state',
				'terms'		=> $state,
				'field'		=> 'slug'
			);
		}
		if(!empty($filter_lake_type)) {
			$args['meta_query'][] = array(
				'key'		=> 'lake_type',
				'value'		=> $filter_lake_type,
				'compare'	=> 'LIKE'
			);
		}
	}
	if(!empty($filter_lake_name)) {
		$args['page_id'] = $filter_lake_name;
	}
	
	$args['meta_query'][] = array(
		'key'		=> 'hotel_logo',
		'compare'	=> 'EXISTS'
	);
	$args['meta_query'][] = array(
		'key'		=> 'hotel_logo',
		'value'   => array(''),
		'compare' => 'NOT IN',
	);
	$args['meta_query'][] = array(
		'key'		=> 'hotel_booking_link',
		'compare'	=> 'EXISTS',
	);
	$args['meta_query'][] = array(
		'key'		=> 'hotel_booking_link',
		'value'   => array(''),
		'compare' => 'NOT IN',
	);
	 
	
	$lakes = new WP_Query( $args );
	//if (have_posts()) : while (have_posts()) : the_post();
	if ($lakes->have_posts()) : while ($lakes->have_posts()) : $lakes->the_post();
	$alternate_names= get_post_meta( get_the_ID(),'alternate_names');
	$lake_type= get_post_meta( get_the_ID(),'lake_type');
	$full_pool= get_post_meta( get_the_ID(),'full_pool');
	$size= get_post_meta( get_the_ID(),'size');
	$state= strtoupper( get_term( get_field('state') )->slug );
	
	if ( function_exists( 'get_field' ) ) {
		$hotel_logo = get_field( 'hotel_logo', get_the_ID() );
		$hotel_url = get_field( 'hotel_booking_link', get_the_ID() );
	}
	
	if ( empty( $hotel_logo ) || empty( $hotel_url ) ) {
		continue;
	}
	
	?>

    <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">
            <?php echo $lake_type[0]; ?><br /><span class="post_info_date"><?php echo $state; ?> </span>
        </div>
        <div class="dcell">
            <?php  echo $size[0]; ?><br /><span class="post_info_date"><?php echo $full_pool[0]; ?> </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();