<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

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

?>
<div id="gn-certificate" class="gn-section gn-certifications-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Certifications', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-certifications">
			<ul class="gn-info-list">
				<?php
				foreach ( $certificates as $key => $certificate_item ) {
					$cert_title        = ! empty( $certificate_item['title'] ) ? $certificate_item['title'] : '';
					$cert_organisation = ! empty( $certificate_item['organisation'] ) ? $certificate_item['organisation'] : '';
					$cert_start_date   = ! empty( $certificate_item['start_date'] ) ? date_i18n( $date_format, strtotime( $certificate_item['start_date'] ), true ) : '';
					$cert_end_date     = ! empty( $certificate_item['end_date'] ) ? date_i18n( $date_format, strtotime( $certificate_item['end_date'] ), true ) : esc_html__( 'Continue', 'gigneo' );
					$cert_image        = ! empty( $certificate_item['image'] ) ? $certificate_item['image'] : '';

					if ( empty( $cert_title ) || empty( $cert_organisation ) ) {
						continue;
					}
					?>
					<li class="gn-info-list-item">
						<?php if ( ! empty( $cert_image ) ) { ?>
							<figure class="gn-image-box">
								<?php gigneo_attachment_image_src( $cert_image, 'thumbnail' ); ?>
								<a href="<?php echo esc_url( wp_get_attachment_url( $cert_image ) ); ?>" data-gn-fancybox class="gn-btn">
									<i class="hi hi-eye" aria-hidden="true"></i>
								</a>
							</figure>
						<?php } else { ?>
							<span class="gn-icon-box">
								<i class="hi hi-cube" aria-hidden="true"></i>
							</span>
						<?php } ?>
						<div class="gn-info">
							<?php if ( ! empty( $cert_title ) ) { ?>
								<h4 class="gn-title"><?php echo esc_html( $cert_title ); ?></h4>
							<?php } ?>
							<ul class="gn-meta">
								<li class="gn-organization">
									<i class="hi hi-building-office" aria-hidden="true"></i>
									<?php echo esc_html( $cert_organisation ); ?>
								</li>
								<?php
								if ( ! empty( $cert_start_date ) ) {
									?>
								<li class="gn-date">
									<i class="hi hi-calendar" aria-hidden="true"></i>
									<?php
									printf(
										// translators: 1: Start date, 2: End date.
										esc_html__( '%1$s - %2$s', 'gigneo' ),
										esc_html( $cert_start_date ),
										esc_html( $cert_end_date )
									);
									?>
								</li>
									<?php
								}
								?>
							</ul>
						</div>
					</li>
				<?php } ?>
			</ul>
		</div>
	</div>
</div>
