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

$job_id             = $args['p_id'] ?? '';
$active_profile_id  = $args['profile_id'] ?? '';
$saved_items        = $args['saved_items'] ?? array();
$country_state_list = $args['country_state_list'] ?? array();

$job_title     = get_the_title( $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' );
$is_featured   = gigneo_post_meta( $job_id, '_gigneo_pm_featured' );

$profile_id     = gigneo_profile_id( $author_id, 'employer' );
$avatar_id      = get_post_thumbnail_id( $profile_id ) ?? 0;
$online_status  = gigneo_online_status( $profile_id );
$average_rating = gigneo_profile_rating( $profile_id, 'average' );
$reviews        = gigneo_profile_rating( $profile_id );

// Location
$country_state_raw = gigneo_post_meta( $job_id, '_gigneo_pm_country_state' );
$city              = gigneo_post_meta( $job_id, '_gigneo_pm_city' );
$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;
$location_label    = implode( ', ', array_filter( array( $city_term->name ?? '', $country_label ?? '' ) ) );

// Save state
$active_class = ( ! empty( $saved_items ) && in_array( $job_id, $saved_items, true ) ) ? 'gn-saved' : '';
?>
<div class="gn-job-card gn-job-card-layout-1" data-item-id="<?php echo intval( $job_id ); ?>">
	<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 ); ?>
					<?php
					if ( $is_featured ) {
						?>
						<span class="gn-status-tag-fill gn-warning">
							<?php echo esc_html__( 'Featured', 'gigneo' ); ?>
						</span>
						<?php
					}
					?>
				</a>
			</h2>
			<span class="gn-budget">
				<?php
				echo esc_html( gigneo_price_format( $price, true ) );
				?>
			</span>
		</div>
		<div class="gn-info-wrapper">
			<div class="gn-meta">
				<?php if ( $work_mode ) { ?>
					<div class="gn-mode">
						<i class="hi hi-map-pin" aria-hidden="true"></i>
						<?php echo esc_html( ucfirst( $work_mode ) ); ?>
					</div>
				<?php } ?>
				<?php
				if ( ! empty( $duration ) && ! empty( $duration_type ) ) {
					?>
					<div class="gn-dueration">
						<i class="hi hi-clock" aria-hidden="true"></i>
						<?php echo esc_html( gigneo_delivery_time( $duration, $duration_type ) ); ?>
					</div>
				<?php } ?>
				<?php if ( ! empty( $location_label ) ) { ?>
					<div class="gn-location">
						<i class="hi hi-map" aria-hidden="true"></i>
						<?php echo esc_html( $location_label ); ?>
					</div>
				<?php } ?>
			</div>
		</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-profile">
			<figure class="gn-avatar gn-<?php echo esc_attr( $online_status ? 'online' : 'offline' ); ?>">
				<?php gigneo_attachment_image_src( $avatar_id, 'thumbnail', 'profile', '', '', $profile_id ); ?>
			</figure>
			<div class="gn-info">
				<a href="<?php echo esc_url( get_the_permalink( $profile_id ) ); ?>" target="_blank"
					class="gn-name"><?php echo esc_html( gigneo_profile_name( $profile_id ) ); ?></a>
				<ul class="gn-meta">
					<li class="gn-ratings">
						<i class="hi hi-star-alt" aria-hidden="true"></i>
						<?php echo esc_html( $average_rating ?? 0 ); ?>
						<span class="gn-count">(<?php echo esc_html( $reviews ?? 0 ); ?>)</span>
					</li>
				</ul>
			</div>
		</div>
		<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">
				<i class="hi hi-eye" aria-hidden="true"></i>
				<?php esc_html_e( 'View job', 'gigneo' ); ?>
			</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>
