<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$portfolio   = $args['portfolio'] ?? array();
$date_format = $args['date_format'] ?? '';

if ( empty( $portfolio ) ) {
	return;
}

?>
<div id="gn-portfolio" class="gn-section gn-portfolio-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Portfolio', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-portfolio-cards">
			<?php
			foreach ( $portfolio as $key => $portfolio_item ) {
				$portfolio_title               = ! empty( $portfolio_item['title'] ) ? $portfolio_item['title'] : '';
				$portfolio_role                = ! empty( $portfolio_item['role'] ) ? $portfolio_item['role'] : '';
				$portfolio_date                = ! empty( $portfolio_item['date'] ) ? gigneo_datetime( $portfolio_item['date'], $date_format ) : '';
				$portfolio_image               = ! empty( $portfolio_item['image'] ) ? $portfolio_item['image'] : '';
				$portfolio_type                = ! empty( $portfolio_item['type'] ) ? $portfolio_item['type'] : '';
				$portfolio_description         = ! empty( $portfolio_item['description'] ) ? $portfolio_item['description'] : '';
				$portfolio_preview['image']    = ! empty( $portfolio_item['preview_image'] ) ? wp_get_attachment_url( $portfolio_item['preview_image'] ) : '';
				$portfolio_preview['document'] = ! empty( $portfolio_item['preview_document'] ) ? wp_get_attachment_url( $portfolio_item['preview_document'] ) : '';
				$portfolio_preview['video']    = ! empty( $portfolio_item['preview_video'] ) ? $portfolio_item['preview_video'] : '';
				$portfolio_preview['link']     = ! empty( $portfolio_item['preview_link'] ) ? $portfolio_item['preview_link'] : '';
				$portfolio_preview_link        = $portfolio_preview[ $portfolio_type ] ?? '';
				if ( empty( $portfolio_title ) || empty( $portfolio_image ) ) {
					continue;
				}
				?>
				<div class="gn-portfolio-card">
					<div class="gn-item">
						<figure class="gn-image">
							<?php gigneo_attachment_image_src( $portfolio_image ); ?>
							<?php if ( ! empty( $portfolio_preview[ $portfolio_type ] ) ) { ?>
								<a href="<?php echo esc_url( $portfolio_preview[ $portfolio_type ] ); ?>"<?php echo esc_attr( 'link' !== $portfolio_type ? ' data-gn-fancybox' : ' target="_blank"' ); ?>><i
											class="hi hi-eye" aria-hidden="true"></i></a>
							<?php } ?>
						</figure>
						<?php if ( ! empty( $portfolio_role ) || ! empty( $portfolio_date ) ) { ?>
							<ul class="gn-meta">
								<?php if ( ! empty( $portfolio_role ) ) { ?>
									<li class="gn-role">
										<i class="hi hi-briefcase" aria-hidden="true"></i>
										<span><?php echo esc_html( $portfolio_role ); ?></span>
									</li>
									<li class="gn-date">
										<i class="hi hi-calendar" aria-hidden="true"></i>
										<span><?php echo esc_html( $portfolio_date ); ?></span>
									</li>
								<?php } ?>
							</ul>
						<?php } ?>
						<h4 class="gn-title"><?php echo esc_html( $portfolio_title ); ?></h4>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
</div>
