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

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

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

$active_profile_id  = gigneo_active_profile_id();
$country_state_list = gigneo_countries_with_states( 'active' );

$job_save_items = gigneo_post_meta( $active_profile_id, '_gigneo_pm_saved_job' );
$job_save_items = ! empty( $job_save_items ) ? $job_save_items : array();
?>
<div id="gn-jobs" class="gn-section gn-jobs-section">
	<div class="gn-head">
		<h3 class="gn-title"><?php esc_html_e( 'Open jobs', 'gigneo' ); ?></h3>
	</div>
	<div class="gn-body">
		<div class="gn-job-list">
			<?php
			foreach ( $listing as $job_id ) {
				$job_title                     = get_the_title( $job_id );
				$job_link                      = get_permalink( $job_id );
				$job_status                    = get_post_status( $job_id );
				$author_id                     = get_post_field( 'post_author', $job_id );
				$price                         = gigneo_post_meta( $job_id, '_gigneo_pm_price' );
				$description                   = gigneo_post_meta( $job_id, '_gigneo_pm_description' );
				$duration                      = gigneo_post_meta( $job_id, '_gigneo_pm_duration' );
				$duration_type                 = gigneo_post_meta( $job_id, '_gigneo_pm_duration_type' );
				$work_mode                     = gigneo_post_meta( $job_id, '_gigneo_pm_type' );
				$author_profile_id             = gigneo_profile_id( $author_id, 'employer' );
				$author_avatar_id              = get_post_thumbnail_id( $author_profile_id ) ?? 0;
				$online_status                 = gigneo_online_status( $author_profile_id );
				$author_profile_average_rating = gigneo_profile_rating( $author_profile_id, 'average' );
				$author_profile_reviews        = gigneo_profile_rating( $author_profile_id );

				// Location
				$country_state_list = gigneo_countries_with_states( 'active' );
				$country_state_raw  = gigneo_post_meta( $job_id, '_gigneo_pm_country_state' );
				$country_label      = ! empty( $country_state_list[ $country_state_raw ] ) ? $country_state_list[ $country_state_raw ] : '';
				$city_term          = ! empty( $city ) ? get_term_by( 'slug', $city, 'gigneo_city' ) : false;
				$city_label         = ( $city_term && ! is_wp_error( $city_term ) ) ? $city_term->name : '';
				$location_label     = implode( ', ', array_filter( array( $city_label, $country_label ) ) );

				// Save state
				$active_class = ( ! empty( $job_save_items ) && in_array( $job_id, $job_save_items, true ) ) ? 'gn-saved' : '';
				?>
				<div class="gn-job-card">
					<div class="gn-head">
						<div class="gn-heading-wrapper">
							<h2 class="gn-title">
								<a href="<?php echo esc_url( get_the_permalink( $job_id ) ); ?>">
									<?php echo esc_html( $job_title ); ?>
								</a>
							</h2>
							<span class="gn-budget"><?php echo esc_html( gigneo_price_format( $price, true ) ); ?></span>
						</div>
						<div class="gn-info-wrapper">
							<ul class="gn-meta">
								<?php if ( $work_mode ) { ?>
									<li class="gn-mode">
										<i class="hi hi-map-pin" aria-hidden="true"></i>
										<?php echo esc_html( ucfirst( $work_mode ) ); ?>
									</li>
								<?php } ?>
								<?php
								if ( ! empty( $duration ) && ! empty( $duration_type ) ) {
									?>
									<li class="gn-dueration">
										<i class="hi hi-clock" aria-hidden="true"></i>
										<?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?>
									</li>
								<?php } ?>
								<?php if ( ! empty( $location_label ) ) { ?>
									<li class="gn-location">
										<i class="hi hi-map" aria-hidden="true"></i>
										<?php echo esc_html( $location_label ); ?>
									</li>
								<?php } ?>
							</ul>
						</div>
					</div>
					<div class="gn-body">
						<p class="gn-description">
							<?php
							echo wp_kses( wp_trim_words( $description, 25, '...' ), gigneo_allowed_html_tags() );
							?>
						</p>
					</div>
					<div class="gn-footer">
						<div class="gn-actions">
							<a href="<?php echo esc_url( get_the_permalink( $job_id ) ); ?>"
								class="gn-btn gn-btn-outline-grey gn-btn-sm">
								<?php esc_html_e( 'View job', 'gigneo' ); ?>
								<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
							</a>
							<button class="gn-save-btn gn-btn gn-btn-outline-grey gn-btn-sm <?php echo esc_attr( $active_class ); ?>"
									data-gn-id="<?php echo intval( $job_id ); ?>"
									data-gn-type="<?php echo esc_attr( 'job' ); ?>"
									aria-label="<?php esc_attr_e( 'Save', 'gigneo' ); ?>">
								<?php gigneo_saved_icon( true ); ?>
							</button>
						</div>
					</div>
				</div>
			<?php } ?>
		</div>
	</div>
</div>
