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

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

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

?>
<div id="gn-license" class="gn-section gn-licenses-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Licenses', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-licenses">
			<ul class="gn-info-list">
				<?php
				foreach ( $licenses as $key => $license_item ) {
					$license          = ! empty( $license_item['license'] ) ? $license_item['license'] : '';
					$license_obj      = get_term_by( 'slug', $license, 'gigneo_license' );
					$license_name     = ! empty( $license_item['license'] ) ? ( $license_obj ? $license_obj->name : $license_item['license'] ) : '';
					$provider         = ! empty( $license_item['provider'] ) ? $license_item['provider'] : '';
					$license_number   = ! empty( $license_item['license_number'] ) ? $license_item['license_number'] : '';
					$verification_url = ! empty( $license_item['verification_url'] ) ? $license_item['verification_url'] : '';
					$issue_date       = ! empty( $license_item['issue_date'] ) ? date_i18n( $date_format, strtotime( $license_item['issue_date'] ), true ) : '';
					$expiry_date      = ! empty( $license_item['expiry_date'] ) ? date_i18n( $date_format, strtotime( $license_item['expiry_date'] ), true ) : esc_html__( 'Active', 'gigneo' );

					if ( empty( $license ) ) {
						continue;
					}
					?>
					<li class="gn-info-list-item">
						<span class="gn-icon">
							<i class="hi hi-identification" aria-hidden="true"></i>
						</span>
						<div class="gn-info">
							<h4 class="gn-title"><?php echo esc_html( $license_name ); ?></h4>
							<ul class="gn-meta">
								<li class="gn-provider">
									<i class="hi hi-building-office" aria-hidden="true"></i>
									<?php echo esc_html( $provider ); ?>
								</li>
								<?php
								if ( ! empty( $license_item['issue_date'] ) ) {
									?>
									<li class="gn-date">
										<i class="hi hi-calendar" aria-hidden="true"></i>
										<?php
										printf(
										// translators: %1$s: Issue date. %2$s: Expiry date.
											esc_html__( '%1$s - %2$s', 'gigneo' ),
											esc_html( $issue_date ),
											esc_html( $expiry_date )
										);
										?>
									</li>
									<?php
								}
								?>
							</ul>
							<?php if ( ! empty( $verification_url ) ) { ?>
								<a href="<?php echo esc_url( $verification_url ); ?>" target="_blank" rel="nofollow" class="gn-link gn-link-primary">
									<?php echo esc_html__( 'Verify license', 'gigneo' ); ?>
									<i class="hi hi hi-arrow-up-right" aria-hidden="true"></i>
								</a>
							<?php } ?>
						</div>
					</li>
				<?php } ?>
			</ul>
		</div>
	</div>
</div>
