<?php
/*** The template for displaying portfolio archive. ***/

get_header();

$health_layout = health_get_option('portfolio_settings_sidebar_type', '2');
$health_sidebar = health_get_option('portfolio_settings_sidebar_content', 'sidebar-1');

if ( ! is_active_sidebar($health_sidebar) ) $health_layout = '1';

$health_portfolio_perrrow = health_get_option('portfolio_settings_perrow', '2');
if ( $health_portfolio_perrrow == '3' ) {
	$health_add_class_port_col = 'col-md-4 col-sm-4 col-xs-6';
}
elseif ( $health_portfolio_perrrow == '4' ) {
	$health_add_class_port_col = 'col-md-3 col-sm-4 col-xs-6';
}
else {
	$health_add_class_port_col = 'col-md-6 col-sm-6 col-xs-6';
}

$health_portfolio_perrow = health_get_option('portfolio_settings_perrow', '2');
$health_portfolio_css_animation = ( health_get_option('css_animation_settings_portfolio', '') != '' ) ? ' wow '.health_get_option('css_animation_settings_portfolio', '') : '';
$health_portfolio_type = health_get_option('portfolio_settings_type', 'type_without_icons');
$health_portfolio_loadmore = health_get_option('portfolio_settings_loadmore', esc_html__('Load more', 'health' ) );

?>

<!-- ========================== -->
<!-- BLOG - CONTENT -->
<!-- ========================== -->
<section class="page-section">
	<div class="container">
		<div class="row">

			<?php health_show_sidebar( 'left', $health_layout, $health_sidebar ); ?>

			<div class="<?php if ( $health_layout == 1 ) : ?>col-lg-12 col-md-12<?php else : ?>col-lg-9 col-md-8<?php endif; ?> col-sm-12 col-xs-12 left-column sidebar-type-<?php echo esc_attr($health_layout == 2 ? 'right' : ($health_layout == 3 ? 'left' : 'hide')); ?>">

				<div id="portfolio-category-section" class="portfolio-list-section portfolio-perrow-<?php echo esc_attr($health_portfolio_perrow); ?>">

				<?php

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

					$health_portfolio_perpage = health_get_option('portfolio_settings_perpage');
					if ( is_numeric( $health_portfolio_perpage ) && $health_portfolio_perpage > 0 ) {
						$health_archive_perpage = $health_portfolio_perpage;
					}
					else {
						$health_archive_perpage = -1;
					}

					$args = array(
								'post_type' => 'portfolio',
								'orderby' => array( 'menu_order' => 'ASC', 'date' => 'DESC' ),
								'paged' => $paged,
								'posts_per_page' => $health_archive_perpage
							);

					$wp_query = new WP_Query( $args );

					if ( $wp_query->have_posts() ) : ?>

						<div class="row portfolio-masonry-holder list-works clearfix">
						<?php
						while ( $wp_query->have_posts() ) :
							$wp_query->the_post();

							$health_portfolio_post_type = ( class_exists( 'RW_Meta_Box' ) && rwmb_meta('post_types_select') != '' ) ? rwmb_meta('post_types_select') : 'image';

							$cats = wp_get_object_terms(get_the_id(), 'portfolio_category');
							$health_cat_slugs = '';
							if ( ! empty($cats) ) {
								foreach ( $cats as $cat ) {
									$health_cat_slugs .= $cat->slug . " ";
								}
							}
							$health_portfolio_thumbnail = get_the_post_thumbnail(get_the_id(), 'health-portfolio-thumb', array('class' => 'img-responsive'));

							// potfolio category list linked
							$health_portfolio_linked_list_cats = health_get_post_terms( array( 'taxonomy' => 'portfolio_category', 'items_wrap' => '%s' ) );

							if ( $health_portfolio_type == 'type_without_icons' || $health_portfolio_type == 'type_without_space' ) : ?>
								<?php $health_no_space_class = $health_portfolio_type == 'type_without_space' ? 'pix-no-space' : ''; ?>
									<div class="<?php echo esc_attr($health_add_class_port_col); ?> item <?php echo esc_attr($health_no_space_class); ?> <?php echo esc_attr($health_portfolio_css_animation); ?> <?php echo esc_attr($health_cat_slugs); ?>" id="post-<?php echo esc_attr(get_the_ID()); ?>">
										<div class="portfolio-item">
											<div class="portfolio-image">
												<a href="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>"><?php echo wp_kses_post($health_portfolio_thumbnail); ?></a>
												<div class="portfolio-item-body">
													<div class="name"><?php echo wp_kses_post( get_the_title() ); ?></div>
													<div class="under-name"><?php echo wp_kses_post( $health_portfolio_linked_list_cats ); ?></div>
												</div>
											</div>
										</div>
									</div>

							<?php
							else : ?>

									<div class="<?php echo esc_attr($health_add_class_port_col); ?> item <?php echo esc_attr($health_portfolio_css_animation); ?> <?php echo esc_attr($health_cat_slugs); ?>" id="post-<?php echo esc_attr(get_the_ID()); ?>">
										<div class="portfolio-item">
											<div class="portfolio-image">
												<a href="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>"><?php echo wp_kses_post($health_portfolio_thumbnail); ?></a>
												<div class="portfolio-item-body center-body">
													<ul>
														<?php
														if ( $health_portfolio_post_type == 'image' ) :
															$health_portfolio_gallery = ( class_exists( 'RW_Meta_Box' ) ) ? rwmb_meta('portfolio_images', 'type=image&size=full') : '';
															$health_portfolio_full_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'full', false);
															$health_portfolio_full_image_link = $health_portfolio_full_image[0];
															?>
															<li><a href="<?php echo esc_url($health_portfolio_full_image_link); ?>"  rel="prettyPhoto[pp_gal_<?php echo esc_attr(get_the_id());?>]"><span class="theme-fonts-Search"></span></a></li>
															<?php
															if ( $health_portfolio_gallery ) :
																foreach ( $health_portfolio_gallery as $key => $slide ) :
																	if ( $key > 0 ) :
																	?>
																		<div class="portfolio-gallery-none">
																			<a href="<?php echo esc_url($slide['url']); ?>" rel="prettyPhoto[pp_gal_<?php echo esc_attr($post->ID); ?>]" ><img src="<?php echo esc_url($slide['url']); ?>" width="<?php echo esc_attr($slide['width']); ?>" height="<?php echo esc_attr($slide['height']); ?>" alt="<?php echo esc_attr($slide['alt']); ?>" title="<?php echo esc_attr($slide['title']); ?>"/></a>
																		</div>
																	<?php
																	endif;
																endforeach;
															endif;
														 ?>
														<?php
														endif; ?>
														<?php
														if ( $health_portfolio_post_type == 'video' ) :
															$health_portfolio_video_href = ( class_exists( 'RW_Meta_Box' ) ) ? get_post_meta( get_the_ID(), 'portfolio_video_href', true ) : '';
															if ( $health_portfolio_video_href != '' ) :
																$health_portfolio_video_width = ( class_exists( 'RW_Meta_Box' ) ) ? rwmb_meta('portfolio_video_width') : '';
																$health_portfolio_video_height = ( class_exists( 'RW_Meta_Box' ) ) ? rwmb_meta('portfolio_video_height') : '';
																?>
																<li><a href="<?php echo esc_url($health_portfolio_video_href.'?width='.esc_attr($health_portfolio_video_width).'&amp;height='. esc_attr($health_portfolio_video_height)) ?>" rel="prettyPhoto[pp_video_<?php echo esc_attr(get_the_id());?>]"><span class="theme-fonts-Media"></span></a></li>
															<?php
															endif;
														endif;
														?>
															<li><a href="<?php echo esc_url( get_permalink( get_the_ID() ) ); ?>"><span class="theme-fonts-Info"></span></a></li>
														<?php



														?>
													</ul>
												</div>
											</div>
											<div class="portfolio-item-footer">
												<div class="name"><?php echo wp_kses_post( get_the_title() ); ?></div>
												<div class="under-name"><?php echo wp_kses_post($health_portfolio_linked_list_cats); ?></div>
											</div>
										</div>
									</div>

							<?php
							endif;

						endwhile; ?>
						</div>

						<?php
						if ( get_next_posts_link( '', $wp_query->max_num_pages ) ) {

							echo '
								<div class="row">
									<div class="col-md-12 text-center">
										<div class="portfolio-pagination">
											<span data-current="'.esc_attr($paged).'" data-max-pages="'.esc_attr($wp_query->max_num_pages).'" class="load-more">' . get_next_posts_link( wp_kses_post($health_portfolio_loadmore), $wp_query->max_num_pages) . '</span>
										</div>
										<div class="portfolio-pagination-loading">
											<a href="javascript: void(0)" class="btn btn-default">'. esc_html__("Loading...", "health") .'</a>
										</div>
									</div>
								</div>
							';
						}
						?>

					<?php
					endif;
				?>
				</div>

			</div>

			<?php health_show_sidebar( 'right', $health_layout, $health_sidebar ); ?>

		</div>
	</div>
</section>

<?php get_footer(); ?>