<?php get_header(); ?>
<?php
$dates_tree_all = $gallery_posts = [];
$queried_object = get_queried_object();

$gallery_types = get_terms('gallery-types'); // $queried_object->slug

$gallery_type = get_term($queried_object->slug);

$back_link = get_post_type_archive_link('gallery');

if ( $gallery_type->parent ) {
    $parent = get_term($gallery_type->parent);
    $back_link = get_term_link($parent->term_id, $parent->taxonomy);
}

//only image types with no parent 
$parent_image_types = get_terms([
    'taxonomy' => 'gallery-types',
    'hide_empty' => true,
    'parent'   => 0
]);


$gallery_types_tree = [];




// URL params/ ///---------------------------------
$urltypes = isset($_GET['type']) ? $_GET['type'] : '';
$urltypestring = isset($_GET['type']) ? $_GET['type'] : '';
$pureurltypes = isset($_GET['type']) ? $_GET['type'] : '';
$urldates = isset($_GET['date']) ? $_GET['date'] : '';

$current_term_slug = $queried_object->slug;

if ($urltypes == "") {

    if (strpos($urltypes, $current_term_slug) == false){
        $urltypes = $urltypes . "," . $current_term_slug;
    } else {
        // $urltypes = $urltypes . ",addition";
    }
}



$urltypesarray = explode(",", $urltypes);
$urldatesarray = explode(",", $urldates);


$urltypesarray = array_filter($urltypesarray);
$urldatesarray = array_filter($urldatesarray);

// get original parent term ID here instead
// get origin parent ID
if ( $gallery_type->parent ) {
    $ancestors = get_ancestors($queried_object->term_id, 'gallery-types');
    // $root_term = end($ancestors);
} else {
    $root_term = $queried_object->term_id;
}

$current_term = $queried_object->term_id;
$current_term_slug = $queried_object->slug;


$gallery_types_tree_all = get_taxonomy_hierarchy('gallery-types', $root_term, $current_term, 0, $urltypesarray);
//pr($gallery_types_tree_all);
if ($queried_object->slug != 'concept-images' && $queried_object->slug != 'videos') {
    $dates_tree_all = get_taxonomy_hierarchy('gallery-dates', 0, $current_term, 0, $urldatesarray);
		//pr($dates_tree_all);
}

//$context['pagination'] = get_pagination();


//$gallery_types_tree = gallery_types_walker($queried_object->term_id);

$download_form = get_field('download_form', 'options');


//filter based on URL params ------------------------------------------------------------------------------------------------------


//baseterm from url is $current_term

//if not using default query    global $paged;
if (!isset($paged) || !$paged){
    $paged = 1;
}

if(!empty($urltypesarray) OR !empty($urldatesarray)) {

    $args = array(
        'post_type' => 'gallery',
        'posts_per_page' => 48,
        'paged' => $paged,
        );


    if(!empty($urltypesarray) && !empty($urldatesarray)) {
       $args['tax_query'] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gallery-types',
                'field'    => 'slug',
                'terms'    => $urltypesarray
            ), 
            array(
               'taxonomy' => 'gallery-dates',
               'field'    => 'slug',
               'terms'    => $urldatesarray
            )
        );
    } else if (!empty($urltypesarray)) {
       $args['tax_query'] = array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'gallery-types',
                'field'    => 'slug',
                'terms'    => $urltypesarray
            )
        );
    } else if (!empty($urldatesarray)){
        $args['tax_query'] = array(
             'relation' => 'AND',
             array(
								'taxonomy' => 'gallery-dates',
								'field'    => 'slug',
								'terms'    => $urldatesarray
             )
         );
    } 
    //$gallery_posts = get_posts($args);
		$gallery_posts = new WP_Query( $args );
		//pr($gallery_posts);
}
$image_types = get_terms([
    'taxonomy' => 'gallery-types',
    'hide_empty' => true,
    'parent'   => 0
]);
 ?>
<article id="post-gallery" class="gallery page panel panel-tall panel-full">


	<div class="panel-content panel-content-100">

		<header class="entry-header">
			<div class="container">
			<h1 class="entry-title">Gallery</h1>



            <div class="archive-filters entry-navigation control-group-filters">
                <?php if($parent_image_types) { ?>
                    <div class="control-facetwp-tabs gallery-tabs button-filters">            
                        <div class="" data-name="gallery-types" data-type="radio">
							<?php foreach($image_types as  $image_type) { ?>
									<a href="<?=get_term_link($image_type);?>" class="filter btn btn-outline-gray <?=$image_type->slug==$queried_object->slug ? 'btn-active' : '';?>" ><?=$image_type->name;?></a>

							<?php } ?>
                        </div>
                    </div>
                <?php } ?>
            </div>



            
        </div>
		</header><!-- .entry-header -->
		<section class="page-section archive-content">
			<div class="section-inner">
				<div class="container">

		<div class="entry-content ajax-load-more-wrap infinite">


            <div class="row">


                <div class="col s12 m8 l2 gallery-sidebar">
									<?php if($gallery_types_tree_all){ ?>
									<h4>Categories</h4>
									<ul>
										<li>All</li>
										<li>
											<ul>
											<?php foreach($gallery_types_tree_all as $gallery_type){ ?>
												<li><a href="?type=<?=$gallery_type->slug;?>" class="<?=($urltypes==$gallery_type->slug) ? 'active': '';?>">
													<?=$gallery_type->name;?>
												</a></li>
												<?php } ?>
											</ul>
										</li>
									</ul>                

								<?php } ?>
<?php if($dates_tree_all){ ?>
	<h4>Date</h4>
	<ul class="m-category-list">
		<li class="<?=!$urldates ? 'active': '';?>"> 
			<a class="<?=!$urldates ? 'active': '';?>" 	href="">All</a>
			<ul>
				<?php
				foreach($dates_tree_all as $child){
					$url= '?date='.$child->slug.($urltypes ? '&type='.$urltypes : '');?>
					<li class="<?=($urldates ==$child->slug) ? 'active': '';?>"> 
						<a class="<?=($urldates ==$child->slug) ? 'active': '';?>" 
						href="<?=$url;?>">
							<?=$child->name;?>
						</a>
						<?php if(isset($child->children) && sizeof($child->children))	{ ?>
							<div class="toggle-arrow chevron"></div>
							<ul>
									<?php foreach($child->children as $child)	{ 
									$url= '?date='.$child->slug.($urltypes ? '&type='.$urltypes : '');?>
									<li class="<?=($urldates ==$child->slug) ? 'active': '';?>">
										<a class="<?=($urldates ==$child->slug) ? 'active': '';?>" 
										href="<?=$url;?>">
										<?=$child->name;?>
										</a>
											<?php if(isset($child->children) && sizeof($child->children))	{ ?>
											<div class="toggle-arrow chevron"></div>
											<ul>
												<?php foreach($child->children as $child){
												$url= '?date='.$child->slug.($urltypes ? '&type='.$urltypes : '');?>
												<li class="<?=($urldates ==$child->slug) ? 'active': '';?>">
												<a class="<?=($urldates ==$child->slug) ? 'active': '';?>" 
												href="<?=$url;?>">
												<?=$child->name;?>
												</a>

												<?php
												if(isset($child->children) && sizeof($child->children))	{
													$url= '?date='.$child->slug.($urltypes ? '&type='.$urltypes : '');?>
													<div class="toggle-arrow chevron"></div>
													<ul>
														<?php foreach($child->children as $child){ ?>
														<li class="<?=($urldates ==$child->slug) ? 'active': '';?>">
															<a class="<?=($urldates ==$child->slug) ? 'active': '';?>" 
															href="<?=$url;?>">
															<?=$child->name;?>
															</a>
														</li>
														<?php
														} ?>
													</ul>
												<?php
													} ?>
												</li>
												<?php
												} ?>
											</ul>
											<?php
										} ?>
									</li>
									<?php
									} ?>
							</ul>
						<?php
							} ?>
					</li>
				<?php
				} ?>
			</ul>
		</li>
	</ul>
<?php } ?>
                </div><!-- end col -->



                <div class="col s12 m8 l10">



            <div class="gallery customgallery">

            <?php if ( $gallery_posts->have_posts() ) { //pr($queried_object->slug);?>

                <div class="galleries gallery-grid" data-photoswipe >
								<?php while ( $gallery_posts->have_posts() ) { $gallery_posts->the_post();

									//pr(wp_get_attachment_image_src(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-large'));
											$video = '';
											if($queried_object->slug == 'videos')	{
												$video = get_post_meta($gallery_posts->post->ID ,'video',true);
												//pr(get_post_meta($gallery_post->post->ID ,'video',true));
												//pr($video);
											}
									?>
                    <figure>
										<a dala-link="<?=get_permalink($gallery_posts->post->ID);?>" class="<?=($queried_object->slug == 'videos') ? 'video_thumbnail' :  'image_thumbnail';?>"
											href="<?=wp_get_attachment_image_src(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-large') ? wp_get_attachment_image_src(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-large')[0] : '';?>"
											data-size="<?= ($queried_object->slug == 'videos') ? '1024x576' : wp_get_attachment_image_src(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-large')[1].'x'.wp_get_attachment_image_src(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-large')[2];?>"
											<?php if($queried_object->slug == 'videos') { ?> data-html="<video width=&quot;640&quot; class=&quot;video-js vjs-default-skin&quot; data-setup='{ &quot;techOrder&quot;: [&quot;youtube&quot;], &quot;sources&quot;: [{ &quot;type&quot;: &quot;video/youtube&quot;, &quot;src&quot;: &quot;<?=$video;?>&quot;}] }'></video>" <?php } ?> >
											<?=wp_get_attachment_image(get_post_thumbnail_id($gallery_posts->post->ID), 'gallery-thumbnail');?>

										<?php if($gallery_posts->post_title){ ?>
											<div class="post-title dfd">
												<span><?=$gallery_posts->post_title;?></span>
											</div>
										<?php } ?>

										</a>
										<figcaption>
											<div class="row">
												<div class="col-md-9">
													<p class="text-white">
														<?=get_the_excerpt($gallery_posts->post->ID);?>
													</p>
												</div>
												<div class="col-md-3">
													<button class="btn btn-outline-gray btn-transparent" data-toggle="modal" data-target="#download" data-media="<?=get_edit_post_link($gallery_posts->post->ID);?>" >Download High Quality</button>
												</div>
											</div>
										</figcaption>
									</figure>
                   <?php } ?>
                </div>
								<div class="pagination-nav">
									<?php
									$big = 999999999; // need an unlikely integer
									echo paginate_links( array(
									'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
									'format' => '?paged=%#%',
									'current' => max( 1, get_query_var('paged') ),
									'total' => $gallery_posts->max_num_pages,
									'show_all'      => false,
									'end_size'      => 3,
									'mid_size'      => 3,
									'prev_next'     => true,
									'prev_text'     => '&larr;',
									'next_text'     => '&rarr;',
									'type'          => 'list',
									'add_args'      => false,
									'add_fragment'  => ''
									) ); 
									?> 										
                </div>
            <?php } else { ?>
                <p>No results</p>
            <?php } ?>
						
                    </div><!--end col -->

                </div><!-- rvow -->

		</div><!-- .entry-content -->
	</div>
</div>
</div>
</section>
	</div><!-- .panel-content -->

</article><!-- #post-## -->
<?php get_footer(); ?>