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

$profile_id = $args['profile_id'] ?? '';
$listing    = $args['listing'] ?? array();

if ( empty( $profile_id ) || empty( $listing ) ) {
	return;
}

$country_state_list = gigneo_countries_with_states( 'active' );
?>
<div id="gn-services" class="gn-section gn-services-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Active services', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-service-list">
			<?php
			foreach ( $listing as $service_id ) {
				$service_title  = get_the_title( $service_id );
				$service_link   = get_permalink( $service_id );
				$service_status = get_post_status( $service_id );

				$service_price        = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_price' );
				$service_type         = gigneo_post_meta( $service_id, '_gigneo_pm_type' );
				$service_thumbnail_id = get_post_thumbnail_id( $service_id );
				$service_gallery      = gigneo_post_meta( $service_id, '_gigneo_pm_gallery' );
				$service_video_type   = gigneo_post_meta( $service_id, '_gigneo_pm_video_type' );

				//Video
				$service_video_link = '';
				if ( 'link' === $service_video_type ) {
					$service_video_link = gigneo_post_meta( $service_id, '_gigneo_pm_video_link' );
				} else {
					$service_video      = gigneo_post_meta( $service_id, '_gigneo_pm_video' );
					$service_video_link = wp_get_attachment_url( $service_video ) ?? '';
				}

				$is_featured = gigneo_post_meta( $service_id, '_gigneo_pm_featured' );

				$delivery_time       = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time' );
				$delivery_time_type  = gigneo_post_meta( $service_id, '_gigneo_pm_price_plan_one_delivery_time_type' );
				$delivery_time_label = gigneo_delivery_time( $delivery_time, $delivery_time_type );

				$locations = gigneo_post_meta( $service_id, '_gigneo_pm_locations' );
				$locations = ! empty( $locations ) && is_array( $locations ) ? $locations : array();
				?>
			<div class="gn-service-card">
				<div class="gn-media">
					<div class="gn-slick-slider gn-slick-theme gn-service-media-slider">
						<div class="gn-slick-slide">
							<figure class="gn-service-media">
								<?php gigneo_attachment_image_src( $service_thumbnail_id, 'large' ); ?>
								<?php if ( ! empty( $service_video_link ) ) { ?>
									<a href="<?php echo esc_url( $service_video_link ); ?>" data-gn-fancybox
										class="gn-play-video-toggle">
										<i class="hi hi-play" aria-hidden="true"></i>
									</a>
								<?php } ?>
							</figure>
						</div>
						<?php
						if ( ! empty( $service_gallery ) && is_array( $service_gallery ) ) {
							foreach ( $service_gallery as $gallery_item ) {
								if ( intval( $gallery_item ) === intval( $service_thumbnail_id ) ) {
									continue;
								}
								?>
								<div class="gn-slick-slide">
									<figure class="gn-service-media"><?php gigneo_attachment_image_src( $gallery_item, 'large' ); ?>
									</figure>
								</div>
								<?php
							}
						}
						?>
					</div>
					<?php if ( $is_featured ) { ?>
						<div class="gn-tags">
							<span class="gn-status-tag-fill gn-warning"><?php echo esc_html__( 'Featured', 'gigneo' ); ?></span>
						</div>
					<?php } ?>
					<button class="gn-save-btn gn-btn" data-gn-id="<?php echo intval( $service_id ); ?>" data-gn-type="<?php echo esc_attr( 'service' ); ?>" aria-label="<?php esc_attr_e( 'Save', 'gigneo' ); ?>">
						<?php gigneo_saved_icon( true ); ?>
					</button>
				</div>
				<div class="gn-content">
					<div class="gn-info">
						<a href="<?php echo esc_url( $service_link ); ?>" class="gn-title"><?php echo esc_html( $service_title ); ?></a>
						<ul class="gn-meta">
							<li class="gn-delivery">
								<i class="hi hi-clock" aria-hidden="true"></i>
								<?php echo esc_html( $delivery_time_label ); ?>
							</li>
							<li class="gn-type">
								<i class="hi hi-map-pin" aria-hidden="true"></i>
								<?php echo 'onsite' === $service_type ? esc_html__( 'Onsite', 'gigneo' ) : esc_html__( 'Remote', 'gigneo' ); ?>
							</li>
							<?php if ( count( $locations ) === 1 ) { ?>
								<li class="gn-location">
									<i class="hi hi-map" aria-hidden="true"></i>
									<?php
									$loc       = $locations[0] ?? array();
									$city      = get_term_by( 'slug', $loc['city'] ?? '', 'gigneo_city' );
									$city_name = ( $city && ! is_wp_error( $city ) ) ? $city->name : '';

									$country_state_key = $loc['country_state'] ?? '';
									$label             = implode( ', ', array_filter( array( $city_name, $country_state_list[ $country_state_key ] ?? '' ) ) );
									echo esc_html( $label );
									?>
								</li>
								<?php
							} elseif ( count( $locations ) > 1 ) {
								?>
								<li class="gn-location">
									<i class="hi hi-map" aria-hidden="true"></i>
									<?php
									printf(
									// translators: %d is the number of locations.
										esc_html( _n( '%d Location', '%d Locations', count( $locations ), 'gigneo' ) ),
										count( $locations )
									);

									?>
								</li>
							<?php } ?>
						</ul>
						<div class="gn-actions">
							<a href="<?php echo esc_url( $service_link ); ?>" class="gn-btn gn-btn-outline-grey gn-btn-md">
								<?php echo esc_html__( 'View service', 'gigneo' ); ?>
								<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
							</a>
						</div>
					</div>
					<div class="gn-price">
						<span class="gn-label"><?php echo esc_html__( 'Starting from', 'gigneo' ); ?></span>
						<strong class="gn-value"><?php echo esc_html( gigneo_price_format( $service_price, true ) ); ?></strong>
					</div>
				</div>
			</div>
			<?php } ?>
		</div>
	</div>
</div>
