<?php

// Prevent Direct Access
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$portfolio_suptitle = get_theme_mod('portfolio_suptitle');
$portfolio_title = get_theme_mod('portfolio_title');
$section_id = get_theme_mod('portfolio_menu_id', 'portfolio');
?>

<section id="<?php echo $section_id ?>" class="padding">
	<div class="container">
		
		<div class="row">
		
			<?php if( $portfolio_title != '' || $portfolio_suptitle != '' ){ ?>
			<div class="col-sm-6 title_block heading_space">
				<?php
				if( $portfolio_suptitle != '' ){ echo '<p class="title bottom0">'.$portfolio_suptitle.'</p>'; }
				if( $portfolio_title != '' ){ echo '<h2 class="darkcolor">'.$portfolio_title.'</h2>'; }
				?>
			</div>
			<?php } ?>
			
			<div class="col-sm-6 text-<?php echo is_rtl() ? 'left' : 'right' ?>">
				<div id="work-filters" class="cbp-l-filters heading_space">
					<div data-filter="*" class="cbp-filter-item">
						<span><?php esc_html_e('All', 'maxo-fa') ?></span>
					</div>
					<?php
					$terms = get_terms('portfolio_categories');
					foreach($terms as $term){
						echo '<div data-filter=".term-'.$term->term_id.'" class="cbp-filter-item"><span>'.$term->name.'</span></div>';
					}
					?>
				</div>
			</div>
			
		</div>
		
		<?php
		$portfolio = new WP_Query(array(
			'post_type' => 'portfolio',
			'posts_per_page' => -1
		));

		if( $portfolio -> have_posts() ){
		?>
		<div id="work-gallery" class="cbp">

			<?php
			while( $portfolio -> have_posts() ){

				$portfolio -> the_post();

				if( has_post_thumbnail() ){

					$post_terms = get_the_terms($post, 'portfolio_categories');

					$classes = array();
					$cats = array();

					if($post_terms){
						foreach($post_terms as $post_term){
							$classes[] = 'term-'.$post_term -> term_id;
							$cats[] = $post_term -> name;
						}
					}

					$classes = implode(' ', $classes);
					$cats = implode(esc_html_x(',', 'separator', 'maxo-fa').' ', $cats);

					$portfolio_link = rwmb_meta('portfolio_link');

					if($portfolio_link != ''){
						$link = $portfolio_link;
						$target = 'target="_blank"';
						$data = '';
					}
					else{
						$link = get_the_post_thumbnail_url(get_the_ID(), 'full');
						$target = '';
						$data = 'data-fancybox="gallery"';
					}
			?>

			<div class="cbp-item <?php echo $classes ?>">

				<a href="<?php echo $link ?>" <?php echo $target ?> <?php echo $data ?>>
					<?php the_post_thumbnail('portfolio-thumb') ?>
					<div class="overlay whitecolor">
						<?php
						if( get_the_title() != '' ){ echo '<h4>'.get_the_title().'</h4>'; }
						if( $cats != '' ){ echo '<p class="heading">'.$cats.'</p>'; }
						?>
					</div>
				</a>

			</div>

			<?php
				}
			}
			?>

		</div>
		<?php
		}

		wp_reset_postdata();
		?>
		
	</div>
</section>