<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}
global $current_user;

$profile_id    = $args['profile_id'] ?? '';
$profile_type  = $args['profile_type'] ?? '';
$platform_type = $args['platform_type'] ?? '';
$credit_system = $args['credit_system'] ?? false;

$is_online            = gigneo_online_status( $profile_id );
$profile_thumbnail_id = get_post_thumbnail_id( $profile_id );
?>
<div id="gn-overview-stats" class="gn-dashboard-section gn-overview-stats">
	<div class="gn-head">   
		<div class="gn-profile">
			<figure class="gn-avatar gn-<?php echo esc_attr( $is_online ? 'online' : 'offline' ); ?>">
				<?php gigneo_attachment_image_src( $profile_thumbnail_id ?? 0, 'small', 'profile' ); ?>
			</figure>
			<div class="gn-info">
				<div class="gn-name-wrapper">
					<h5 class="gn-name"><?php echo esc_html( gigneo_profile_name( $profile_id ) ); ?></h5>
					<?php gigneo_verification_badge( $profile_id, true ); ?>
				</div>
				<p class="gn-email"><?php echo esc_html( $current_user->user_email ); ?></p>
			</div>
		</div>
		<div class="gn-actions">
			<a href="<?php echo esc_url( gigneo_dashboard_url( 'account', 'freelancer' === $profile_type ? 'earning' : 'spending' ) ); ?>"
				class="gn-btn gn-btn-outline-grey gn-btn-md">
				<?php echo 'freelancer' === $profile_type ? esc_html__( 'View earnings', 'gigneo' ) : esc_html__( 'View spending', 'gigneo' ); ?>
				<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
			</a>
		</div>
	</div>
	<div class="gn-body">
		<div class="gn-stats-wrapper">
			<h3 class="gn-title">
				<i class="hi hi-banknotes" aria-hidden="true"></i>
				<?php echo 'freelancer' === $profile_type ? esc_html__( 'Earnings', 'gigneo' ) : esc_html__( 'Spending', 'gigneo' ); ?>
			</h3>
			<div class="gn-stats">
				<?php
				$wallet_balance = gigneo_profile_stat( 'wallet_balance', $profile_id );
				$active_orders  = gigneo_profile_stat( 'active_orders_total', $profile_id );

				//Earning / Spending
				if ( 'freelancer' === $profile_type ) {
					$total_earning = gigneo_profile_stat( 'total_earning', $profile_id );
					?>
					<div class="gn-stat gn-last-thirty-days-earning">
						<h6 class="gn-label"><?php echo esc_html__( 'Last 30 days', 'gigneo' ); ?></h6>
						<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $total_earning['last_30'], true ) ); ?></h4>
						<p class="gn-trend-change">
							<span class="gn-trend gn-<?php echo esc_attr( $total_earning['trend'] ); ?>">
								<?php echo esc_html( number_format( $total_earning['percentage'] ) . '%' ); ?>
							</span>
							<?php echo esc_html__( 'vs prev 30 days', 'gigneo' ); ?>
						</p>
					</div>
					<div class="gn-stat gn-total-earning">
						<h6 class="gn-label"><?php echo esc_html__( 'Total earning', 'gigneo' ); ?></h6>
						<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $total_earning['lifetime'], true ) ); ?></h4>
						<p class="gn-trend-change">
							<span class="gn-trend gn-<?php echo esc_attr( $total_earning['trend'], ); ?>">
								<?php echo esc_html( number_format( $total_earning['percentage'] ) . '%' ); ?>
							</span>
							<?php echo esc_html__( 'vs prev 30 days', 'gigneo' ); ?>
						</p>
					</div>
					<?php
				} else {
					$total_spending = gigneo_profile_stat( 'total_spending', $profile_id );
					?>
					<div class="gn-stat gn-last-thirty-days-speding">
						<h6 class="gn-label"><?php echo esc_html__( 'Last 30 days', 'gigneo' ); ?></h6>
						<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $total_spending['last_30'], true ) ); ?></h4>
						<p class="gn-trend-change">
							<span class="gn-trend gn-<?php echo esc_attr( $total_spending['trend'] ); ?>">
								<?php echo esc_html( number_format( $total_spending['percentage'] ) . '%' ); ?>
							</span>
							<?php echo esc_html__( 'vs prev 30 days', 'gigneo' ); ?>
						</p>
					</div>
					<div class="gn-stat gn-total-speding">
						<h6 class="gn-label"><?php echo esc_html__( 'Total spending', 'gigneo' ); ?></h6>
						<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $total_spending['lifetime'], true ) ); ?></h4>
						<p class="gn-trend-change">
							<span class="gn-trend gn-<?php echo esc_attr( $total_spending['trend'] ); ?>">
								<?php echo esc_html( number_format($total_spending['percentage']) . '%' ); ?>
							</span>
							<?php echo esc_html__( 'vs prev 30 days', 'gigneo' ); ?>
						</p>
					</div>
				<?php } ?>
				<div class="gn-stat gn-wallet-balance">
					<h6 class="gn-label"><?php echo esc_html__( 'Wallet balance', 'gigneo' ); ?></h6>
					<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $wallet_balance['lifetime'], true ) ); ?></h4>
					<p class="gn-trend-change">
						<span class="gn-trend gn-<?php echo esc_attr( $wallet_balance['trend'] ); ?>">
								<?php echo esc_html( number_format( $wallet_balance['percentage'] ) . '%' ); ?>
							</span>
						<?php echo esc_html__( 'vs prev 30 days', 'gigneo' ); ?>
					</p>
				</div>
				<div class="gn-stat gn-earning-pending">
					<h6 class="gn-label"><?php echo esc_html__( 'In escrow', 'gigneo' ); ?></h6>
					<h4 class="gn-value"><?php echo esc_html( gigneo_price_format( $active_orders['lifetime'], true ) ); ?></h4>
					<p class="gn-trend-change">
						<?php echo esc_html__( 'From active orders', 'gigneo' ); ?>
					</p>
				</div>
			</div>
		</div>
	</div>
</div>
