<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

use Gigneo\Modules;

$order_id                 = isset( $args['order_id'] ) ? intval( $args['order_id'] ) : 0;
$service_detail_url       = isset( $args['service_detail_url'] ) ? ( $args['service_detail_url'] . '&tab=messages' ) : '';
$profile_id               = gigneo_active_profile_id();
$profile_type             = gigneo_profile_type();
$employer_profile_id      = get_post_meta( $order_id, '_gigneo_pm_profile_id', true );
$freelancer_profile_id    = get_post_meta( $order_id, '_gigneo_pm_freelancer_profile_id', true );
$participant_profile_id   = 'employer' === $profile_type ? $freelancer_profile_id : $employer_profile_id;
$participant_profile_type = 'employer' === $profile_type ? 'freelancer' : 'employer';
$employer_profile_access  = gigneo_employer_profile_access();

if ( empty( $participant_profile_id ) ) {
	return;
}

$participant_post   = get_post( $participant_profile_id );
$participant_online = gigneo_online_status( $participant_profile_id );

$participant_profile_name              = gigneo_profile_name( $participant_profile_id );
$participant_profile_type              = gigneo_profile_type( $participant_profile_id );
$country_state_list                    = gigneo_countries_with_states( 'active' );
$participant_profile_image             = gigneo_profile_avatar_url( $participant_profile_id, $participant_profile_type );
$participant_profile_country_state_raw = gigneo_post_meta( $participant_profile_id, '_gigneo_pm_country_state' );
$participant_profile_tagline           = gigneo_post_meta( $participant_profile_id, '_gigneo_pm_tagline' );
$participant_profile_country_state     = $country_state_list[ $participant_profile_country_state_raw ] ?? '—';
$date_format                           = get_option( 'date_format' );
?>
<div class="gn-order-section gn-order-participent-info">
	<div class="gn-head">
		<div class="gn-participent-profile-info">
			<figure class="gn-avatar gn-<?php echo esc_attr( $participant_online ? 'online' : 'offline' ); ?>">
				<img src="<?php echo esc_url( $participant_profile_image ); ?>"
					alt="<?php echo esc_attr( $participant_profile_name ); ?>" />
			</figure>
			<div class="gn-info">
				<div class="gn-name-wrapper">
					<h6 class="gn-name"><?php echo esc_html( $participant_profile_name ); ?></h6>
					<?php gigneo_verification_badge( $participant_profile_id, true ); ?>
				</div>
				<p class="gn-tagline">
					<?php echo esc_html( ! empty( $participant_profile_tagline ) ? $participant_profile_tagline : ( 'freelancer' === $participant_profile_type ? gigneo_freelancer_singular() : gigneo_employer_singular() ) ); ?>
				</p>
			</div>
		</div>
	</div>
	<div class="gn-body">
		<ul class="gn-additional-info">
			<li class="gn-ratings">
				<span class="gn-label"><?php echo esc_html__( 'Ratings', 'gigneo' ); ?></span>
				<span class="gn-value">
					<i class="hi hi-star-alt gn-filled" aria-hidden="true"></i>
					<?php echo esc_html( gigneo_profile_rating( $participant_profile_id, 'average' ) ); ?>
					<em
						class="gn-count">(<?php echo esc_html( gigneo_profile_rating( $participant_profile_id ) ); ?>)</em>
				</span>
			</li>
			<li class="gn-orders-completed">
				<span class="gn-label"><?php echo esc_html__( 'Orders completed', 'gigneo' ); ?></span>
				<?php $orders_data = gigneo_profile_stat( 'completed_orders_count', $participant_profile_id ); ?>
				<span class="gn-value"><?php echo esc_html( $orders_data['lifetime'] ); ?></span>
			</li>
			<?php if ( 'freelancer' === $participant_profile_type ) { ?>
				<li class="gn-total-earn">
					<span class="gn-label"><?php echo esc_html__( 'Total earns', 'gigneo' ); ?></span>
					<?php $earnings_data = gigneo_profile_stat( 'total_earning', $participant_profile_id ); ?>
					<span
						class="gn-value"><?php echo esc_html( gigneo_price_format( $earnings_data['lifetime'], true ) ); ?></span>
				</li>
			<?php } else { ?>
				<li class="gn-total-earn">
					<span class="gn-label"><?php echo esc_html__( 'Total spent', 'gigneo' ); ?></span>
					<?php $earnings_data = gigneo_profile_stat( 'total_spending', $participant_profile_id ); ?>
					<span
						class="gn-value"><?php echo esc_html( gigneo_price_format( $earnings_data['lifetime'], true ) ); ?></span>
				</li>
			<?php } ?>
			<li class="gn-country-state">
				<span class="gn-label"><?php echo esc_html__( 'Country / State', 'gigneo' ); ?></span>
				<span class="gn-value"><?php echo esc_html( $participant_profile_country_state ); ?></span>
			</li>
		</ul>
		<div class="gn-actions">
			<?php do_action( 'gigneo_action_buttons', array( 'member_id' => $participant_profile_id, 'profile_id' => $profile_id, 'context' => 'order_detail' ) ); ?>
			<?php
			if ( 'freelancer' === $participant_profile_type || ( 'employer' === $participant_profile_type && 'public' === $employer_profile_access ) ) {
				?>
				<a href="<?php echo esc_url( get_permalink( $profile_id ) ); ?>"
					class="gn-btn gn-btn-outline-grey gn-btn-md gn-view-profile-btn">
					<?php if ( ! in_array( 'live_chat' , gigneo_active_modules(), true  ) ) { ?>
						<?php echo esc_html__( 'View profile', 'gigneo' ); ?>
					<?php } ?>
					<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
				</a>
			<?php } ?>
		</div>
	</div>
</div>