<?php
/**
 * The template for displaying archive portfolios.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package zev
 */

get_header(); ?>
<?php 
$columns = themesflat_get_opt('portfolio_grid_columns');
$themesflat_paging_style = themesflat_get_opt('portfolio_archive_pagination_style');
$orderby = themesflat_get_opt('portfolio_order_by');
$order = themesflat_get_opt('portfolio_order_direction');
$exclude = themesflat_get_opt('portfolio_exclude');
$show_filter = themesflat_get_opt('portfolio_show_filter');
$filter_category_order = themesflat_get_opt('portfolio_filter_category_order');		
$terms_slug = wp_list_pluck( get_terms( 'portfolios_category','hide_empty=0'), 'slug' );
$filters =      wp_list_pluck( get_terms( 'portfolios_category','hide_empty=0'), 'name','slug' );
$show_filter_class = '';

if ( get_query_var('paged') ) {
	$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
	$paged = get_query_var('page');
} else {
	$paged = 1;
}

$args = array(
    'post_type' => 'portfolios',
    'orderby'   => $orderby,
    'order' => $order,
    'paged' => $paged, 
    'posts_per_page' => themesflat_get_opt('portfolios_show_number_post'),      
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolios_category',   
            'field'    => 'slug',                   
        	'terms'    => $terms_slug,
        ),
    ),
);	

if ( ! empty( $exclude ) ) {				
	if ( ! is_array( $exclude ) )
		$exclude = explode( ',', $exclude );

	$args['post__not_in'] = $exclude;
}

$query = new WP_Query( $args );
?>

	<div class="row">
        <div class="col-lg-6">
            <div id="tf-title-section" class="tf-title-section"> 
                <div class="title-section ">
                    <div class="image">
                        <div class="separator-vertical"></div>
                        <div class="inner-image">
                            <span>
                                <img width="85" height="29" src="<?php echo esc_url(THEMESFLAT_LINK.'images/img-title-section.png') ?>" class="attachment-full size-full" alt="image" loading="lazy">
                            </span>
                        </div>
                    </div> 
                    <div class="sub-title"><?php echo esc_html__('Awesome Works.','zev'); ?></div>
                    <h2 class="wrap-title">
                        <span class="text-gradient"><?php echo esc_html__('Zev','zev'); ?> </span><span class="text-last"><?php echo esc_html__('Portfolio.','zev'); ?></span>
                    </h2>
                </div>                
            </div>
        </div>
		<div class="col-lg-6">
            <div class="wrap-portfolio-filter">
    			<?php
    			//Build the filter navigation
    			if ( $show_filter == 1 ) {	
    				$show_filter_class = 'show-filter';     	
    					echo '<ul class="portfolio-filter posttype-filter">'; 
    					if ($filter_category_order == '') { 

    						foreach ($filters as $key => $value) {
    							echo '<li><a data-filter=".' . esc_attr( strtolower($key)) . '" href="#" title="' . esc_attr( $value ) . '">' . esc_html( $value ) . '</a></li>'; 
    						}
    					
    					}
    					else {
    						$filter_category_order = explode(",", $filter_category_order);
    						foreach ($filter_category_order as $key) {
    							$key = trim($key);
    							echo '<li><a data-filter=".' . esc_attr( strtolower($key)) . '" href="#" title="' . esc_attr( $filters[$key] ) . '">' . esc_html( $filters[$key] ) . '</a></li>'; 
    						}
    					}
    	                echo '<li class="active"><a data-filter="*" href="#">' . esc_html__( 'All', 'zev' ) . '</a></li></ul>';
    	        }  
    			?>
            </div>
		</div>
	</div>    

    <div class="themesflat-portfolios-taxonomy">
        <div class="container-filter wrap-portfolios-post row column-<?php echo esc_attr($columns); ?> <?php echo esc_attr($show_filter_class); ?>">
            <?php 
            if( $query->have_posts() ) {
                while ( $query->have_posts() ) : $query->the_post(); 
                	global $post;
			        $id = $post->ID;
			        $termsArray = get_the_terms( $id, 'portfolios_category' );
			        $termsString = "";

			        if ( $termsArray ) {
			        	foreach ( $termsArray as $term ) {
			        		$itemname = strtolower( $term->slug ); 

			        		$itemname = str_replace( ' ', '-', $itemname );
			        		$termsString .= $itemname.' ';

			        	}
			        }
                	?>           
                    <div class="item <?php echo esc_attr( $termsString ); ?>">
                        <div class="portfolios-post portfolios-post-<?php the_ID(); ?>">
                            <div class="featured-post">
                                <?php 
                                    if ( has_post_thumbnail() ) { 
                                        $themesflat_thumbnail = "full";                              
                                        the_post_thumbnail( $themesflat_thumbnail );
                                    }                                           
                                ?>
                                <a href="<?php echo get_the_permalink(); ?>" class="overlay zoom"></a>
                            </div>
                            <div class="content">
                                <div class="inner-content-left">
                                    <ul class="post-meta">
                                        <li class="post-category">
                                            <?php echo the_terms( get_the_ID(), 'portfolios_category', '', ', ', '' ); ?>
                                        </li>
                                    </ul>
                                    <h2 class="title">
                                         <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a>
                                    </h2>
                                </div>
                            </div>
                        </div>
                    </div>                    
                    <?php 
                endwhile; 
            } else {
                get_template_part( 'template-parts/content', 'none' );
            }
            ?>            
        </div>
    </div><!-- /.themesflat-portfolios-taxonomy --> 

    <div class="row">
        <div class="col-lg-12">           
            <?php             
            $big = 999999999;
            $numeric_links = paginate_links( array(
                'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                'format' => '?paged=%#%',
                'total'    => $query->max_num_pages,
                'current'  => $paged,
                'mid_size' => 1,
                'prev_next' => false
            ) );           
            ?> 
            <nav class="navigation paging-navigation numeric portfolios" role="navigation">
                <div class="pagination loop-pagination">
                    <?php echo wp_kses( $numeric_links, themesflat_kses_allowed_html() ) ?>
                </div>
            </nav>
            <?php wp_reset_postdata(); ?>               
        </div>
    </div>

<?php get_footer(); ?>