<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$profile_id          = $args['p_id'] ?? '';
$active_profile_id   = $args['profile_id'] ?? '';
$active_profile_type = $args['profile_type'] ?? '';
$saved_items         = $args['saved_items'] ?? array();
$country_state_list  = $args['country_state_list'] ?? array();

$name           = gigneo_profile_name( $profile_id );
$online         = gigneo_online_status( $profile_id );
$profile_url    = get_permalink( $profile_id );
$author_id      = get_post_field( 'post_author', $profile_id );
$profile_access = gigneo_employer_profile_access();

// Avatar & cover
$thumbnail_id   = get_post_thumbnail_id( $profile_id );
$cover_image_id = gigneo_post_meta( $profile_id, '_gigneo_pm_cover_image' );

// Profile details
$tagline    = gigneo_post_meta( $profile_id, '_gigneo_pm_tagline' );
$city       = gigneo_post_meta( $profile_id, '_gigneo_pm_city' );
$country    = gigneo_post_meta( $profile_id, '_gigneo_pm_country' );
$categories = gigneo_post_meta( $profile_id, '_gigneo_pm_categories' );

// Rating
$average_rating = gigneo_profile_rating( $profile_id, 'average' );
$rating_count   = gigneo_profile_rating( $profile_id );

$total_spent  = gigneo_profile_stat( 'total_spending', $profile_id );
$success_rate = gigneo_profile_stat( 'success_rate', $profile_id );
$total_orders = gigneo_profile_stat( 'total_orders_count', $profile_id );

// Location
$country_state_raw = gigneo_post_meta( $profile_id, '_gigneo_pm_country_state' );
$city_term         = ! empty( $city ) ? get_term_by( 'slug', $city, 'gigneo_city' ) : array();
$location_label    = implode( ', ', array_filter( array( $city_term->name ?? '', $country_state_list[ $country_state_raw ] ?? '' ) ) );
?>
<div class="gn-employer-card gn-employer-card-layout-1"
	data-item-id="<?php echo intval( $profile_id ); ?>">
	<div class="gn-head">
		<div class="gn-cover-wrapper">
			<figure class="gn-cover">
				<?php gigneo_attachment_image_src( $cover_image_id, 'large', 'cover' ); ?>
			</figure>
			<button
				class="gn-save-btn gn-btn gn-<?php echo esc_attr( in_array( $profile_id, $saved_items, true ) ? 'saved' : 'unsaved' ); ?>"
				data-gn-id="<?php echo intval( $profile_id ); ?>" data-gn-type="employer"
				aria-label="<?php esc_attr_e( 'Save', 'gigneo' ); ?>">
				<?php gigneo_saved_icon( true ); ?>
			</button>
		</div>
	</div>
	<div class="gn-body">
		<figure class="gn-avatar gn-<?php echo esc_attr( $online ? 'online' : 'offline' ); ?>">
			<img src="<?php echo esc_url( gigneo_profile_avatar_url( $profile_id, 'employer' ) ); ?>"
				alt="<?php echo esc_attr( gigneo_profile_name( $profile_id ) ); ?>">
		</figure>
		<div class="gn-info-wrapper">
			<div class="gn-name-wrapper">
				<a href="<?php echo esc_url( 'private' === $profile_access ? '#' : $profile_url ); ?>" class="gn-name">
					<?php echo esc_html( $name ); ?>
				</a>
				<?php gigneo_verification_badge( $profile_id, true ); ?>
				<?php if ( $average_rating > 0 ) { ?>
					<span class="gn-rating">
						<i class="hi hi-star-alt" aria-hidden="true"></i>
						<?php echo esc_html( number_format( $average_rating, 1 ) ); ?>
						<span class="gn-count">
							(<?php echo esc_html( $rating_count ); ?>)
						</span>
					</span>
				<?php } ?>
			</div>
			<?php if ( ! empty( $location_label ) ) { ?>
				<p class="gn-location">
					<i class="hi hi-map-pin" aria-hidden="true"></i>
					<?php echo esc_html( $location_label ); ?>
				</p>
			<?php } ?>
			<?php if ( ! empty( $tagline ) ) { ?>
				<p class="gn-description gn-two-line-ellipsis">
					<?php echo esc_html( $tagline ); ?>
				</p>
			<?php } ?>
		</div>
		<div class="gn-stats-wrapper">
			<div class="gn-stat">
				<span class="gn-value">
					<?php echo esc_html( $total_orders['lifetime'] ); ?>
				</span>
				<span class="gn-label">
					<?php esc_html_e( 'Orders', 'gigneo' ); ?>
				</span>
			</div>
			<?php if ( ! empty( $total_spent ) ) { ?>
				<div class="gn-stat">
					<span class="gn-value">
						<?php echo esc_html( gigneo_price_format( $total_spent['lifetime'], true ) ); ?>
					</span>
					<span class="gn-label">
						<?php esc_html_e( 'Spent', 'gigneo' ); ?>
					</span>
				</div>
			<?php } ?>
			<div class="gn-stat">
				<span class="gn-value">
					<?php echo esc_html( $success_rate['lifetime'] ); ?>%
				</span>
				<span class="gn-label">
					<?php esc_html_e( 'Success rate', 'gigneo' ); ?>
				</span>
			</div>
		</div>
	</div>
	<div class="gn-footer">
		<?php if ( 'private' !== $profile_access ) { ?>
			<div class="gn-actions">
				<a href="<?php echo esc_url( $profile_url ); ?>"
					class="gn-view-profile gn-btn gn-btn-outline-grey gn-btn-sm">
					<?php esc_html_e( 'View profile', 'gigneo' ); ?>
					<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
				</a>
			</div>
		<?php } ?>
	</div>
</div>