<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$job_id                        = $args['p_id'] ?? get_the_ID();
$author_id                     = get_post_field( 'post_author', $job_id );
$profile_type                  = gigneo_profile_type( $author_id );
$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 );
$tagline                       = gigneo_post_meta( $author_profile_id, '_gigneo_pm_tagline' );
$profile_singular              = gigneo_employer_singular();
$tagline                       = ! empty( $tagline ) ? $tagline : $profile_singular;
$spending_data                 = gigneo_profile_stat( 'total_spending', $author_profile_id );
$hires                         = gigneo_profile_stat( 'total_orders_count', $author_profile_id );
$total_jobs                    = gigneo_profile_stat( 'completed_orders_count', $author_profile_id );
$hire_rate                     = max( 0, min( 100, $hires['lifetime'] > 0 ? round( ( $total_jobs['lifetime'] / $hires['lifetime'] ) * 100, 1 ) : 0 ) );
?>
<div class="gn-section gn-employer-section">
	<div class="gn-head">
		<div class="gn-profile">
			<figure class="gn-avatar gn-<?php echo esc_attr( $online_status ? 'online' : 'offline' ); ?>">
				<?php gigneo_attachment_image_src( $author_avatar_id, 'thumbnail', 'profile', '', '', $author_profile_id ); ?>
			</figure>
			<div class="gn-info">
				<a href="<?php echo esc_url( get_the_permalink( $author_profile_id ) ); ?>" target="_blank"
					class="gn-name"><?php echo esc_html( gigneo_profile_name( $author_profile_id ) ); ?></a>
				<p class="gn-tagline">
					<?php echo esc_html( $tagline ); ?>
				</p>
			</div>
		</div>
	</div>
	<div class="gn-body">
		<ul class="gn-employer-info">
			<li class="gn-info">
				<span class="gn-title"><?php echo esc_html__( 'Rating', 'gigneo' ); ?></span>
				<span class="gn-value"> <i class="hi hi-star-alt" aria-hidden="true"></i>
					<?php echo esc_html( $author_profile_average_rating ?? 0 ); ?>
					<span class="gn-count">(<?php echo esc_html( $author_profile_reviews ?? 0 ); ?>)</span>
				</span>
			</li>
			<li class="gn-info">
				<span class="gn-title"><?php echo esc_html__( 'Hire rate', 'gigneo' ); ?></span>
				<span class="gn-value"><?php echo esc_html( $hire_rate . '%' ); ?></span>
			</li>
			<li class="gn-info">
				<span class="gn-title"><?php echo esc_html__( 'Total hires', 'gigneo' ); ?></span>
				<span class="gn-value"><?php echo esc_html( $hires['lifetime'] ); ?></span>
			</li>
			<li class="gn-info">
				<span class="gn-title"><?php echo esc_html__( 'Total spent', 'gigneo' ); ?></span>
				<span
					class="gn-value"><?php echo esc_html( gigneo_price_format( $spending_data['lifetime'], true ) ); ?></span>
			</li>
		</ul>
	</div>
	<div class="gn-footer">
		<div class="gn-actions">
			<a href="<?php echo esc_url( get_the_permalink( $author_profile_id ) ); ?>"
				class="gn-btn gn-btn-outline-grey gn-btn-md">
				<?php echo esc_html__( 'View profile', 'gigneo' ); ?>
				<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
			</a>
		</div>
	</div>
</div>