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

get_header(); ?>
<?php 
$columns = themesflat_get_opt('services_grid_columns');
$themesflat_paging_style = themesflat_get_opt('services_archive_pagination_style');
$orderby = themesflat_get_opt('services_order_by');
$order = themesflat_get_opt('services_order_direction');
$exclude = themesflat_get_opt('services_exclude');
$show_filter = themesflat_get_opt('services_show_filter');
$filter_category_order = themesflat_get_opt('services_filter_category_order');	
$terms_slug = wp_list_pluck( get_terms( 'services_category','hide_empty=0'), 'slug' );
$filters =      wp_list_pluck( get_terms( 'services_category','hide_empty=0'), 'name','slug' );
$show_filter_class = '';

$class = array('services-archive');
$class[] = 'archive-'.get_post_type();
$class = apply_filters('themesflat/template/services_class',$class);

$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;

$query_args = array(
    'post_type' => 'services',
    'orderby'   => $orderby,
    'order' => $order,
    'paged' => $paged, 
    'posts_per_page' => themesflat_get_opt('services_show_number_post'),          
    'tax_query' => array(
        array(
            'taxonomy' => 'services_category',   
            'field'    => 'slug',                   
        	'terms'    => $terms_slug,
        ),
    ),
);	

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

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


?>


	<div class="row">
		<div class="col-lg-12">
			<?php
			//Build the filter navigation
			if ( $show_filter == 1 ) {	
				$show_filter_class = 'show-filter';     	
					echo '<ul class="services-filter posttype-filter"><li class="active"><a data-filter="*" href="#">' . esc_html__( 'All', 'zev' ) . '</a></li>'; 
					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 '</ul>';
	        }  
			?>
		</div>
	</div>    

    <div class="themesflat-services-taxonomy">
        <div class="container-filter wrap-services-post row column-<?php echo esc_attr($columns); ?> <?php echo esc_attr($show_filter_class); ?>">
            <?php 
            $query = new WP_Query( $query_args );
            if( $query->have_posts() ) {
                while ( $query->have_posts() ) : $query->the_post();                	
			        $id = $post->ID;
			        $termsArray = get_the_terms( $id, 'services_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="services-post services-post-<?php the_ID(); ?>">
                            <div class="featured-post">
                                <?php 
                                    if ( has_post_thumbnail() ) { 
                                        $themesflat_thumbnail = "themesflat-blog-grid";                              
                                        the_post_thumbnail( $themesflat_thumbnail );
                                    }                                           
                                ?>
                                <div class="overlay"></div>
                                <a href="<?php echo get_the_permalink(); ?>" class="overlay zoom">
                                    <span class="inner-overlay">
                                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50" width="21"><title><?php _e( 'Asset 1', 'zev' ); ?></title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path d="M26,0V24H50v1H26V50H25V25H0V24H25V0Z"></path></g></g></svg>
                                    </span>
                                </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(), 'services_category', '', ' , ', '' ); ?>
                                        </li>
                                    </ul>
                                    <h2 class="title">
                                         <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a>
                                    </h2>
                                </div>
                                <div class="inner-content-right">
                                    <div class="tf-button-container">
                                        <a href="<?php echo esc_url( get_permalink() ); ?>" class="tf-button bt_icon_after">
                                            <svg xmlns="http://www.w3.org/2000/svg" id="Layer_1" data-name="Layer 1" viewBox="0 0 69.3 47.51" width="25"><title><?php _e( 'right-arrow', 'zev' ); ?></title><path class="cls-1" d="M0,23H67.45v1.67H0Z"></path><path class="cls-1" d="M45.59,0,69.3,23.67l-1.18,1.18L44.41,1.18Z"></path><path class="cls-1" d="M44.35,46.33,68,22.64l1.19,1.18L45.53,47.51Z"></path></svg>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>                    
                    <?php 
                endwhile;
            } else {
                get_template_part( 'template-parts/content', 'none' );
            }

            ?>            
        </div>
    </div><!-- /.themesflat-services-taxonomy --> 

    <div class="row">
    	<div class="col-lg-12">           
            <?php 
            global $themesflat_paging_style, $themesflat_paging_for;
            $themesflat_paging_for = 'services';
            $themesflat_paging_style = themesflat_get_opt('services_archive_pagination_style');             
            get_template_part( 'tpl/pagination' );
            wp_reset_postdata();
            ?>                
		</div>
    </div>

<?php get_footer(); ?>