<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
global $current_user;

$profile_id   = gigneo_active_profile_id();
$profile_type = gigneo_profile_type( $profile_id );
$profile_name = gigneo_profile_name( $profile_id );
$user_menu    = gigneo_user_menu_list( $profile_type );
$thumbnail_id = get_post_thumbnail_id( $profile_id );

$profile_type_text = 'freelancer' === $profile_type ? gigneo_freelancer_singular() : gigneo_employer_singular();
?>
<div class="gn-user-menu-dropdown">
	<div class="gn-user-menu-header">
		<div class="gn-user-profile">
			<?php gigneo_attachment_image_src( $thumbnail_id, 'medium', 'profile' ); ?>
			<div class="gn-profile-info">
				<h6 class="gn-name"><?php echo esc_html( $profile_name ); ?></h6>
				<p class="gn-type"><?php echo esc_html( $profile_type_text ); ?></p>
			</div>
		</div>
	</div>
	<?php if ( ! empty( $user_menu ) ) { ?>
		<span class="gn-separator" ></span>
		<ul class="gn-user-menu-list">
			<?php
			foreach ( $user_menu as $menu_key => $menu_item ) {
				$menu_url = gigneo_dashboard_url( $menu_item['source'] ?? '', $menu_item['ref'] ?? '' );
				$menu_url = ! empty( $menu_item['url'] ) ? $menu_item['url'] : $menu_url;
				$attr_id  = ! empty( $menu_item['id'] ) ? ' id="' . esc_attr( $menu_item['id'] ) . '"' : '';
				$classes  = "gn-menu-$menu_key";
				$classes .= ! empty( $menu_item['class'] ) ? ' ' . esc_attr( $menu_item['class'] ) : '';
				?>
				<li class="gn-item">
					<a<?php echo wp_kses_post( $attr_id ); ?> class="<?php echo esc_attr( $classes ); ?>" href="<?php echo esc_url( $menu_url ); ?>" target="<?php echo esc_attr( $menu_item['target'] ); ?>">
						<?php if ( ! empty( $menu_item['icon'] ) ) { ?>
							<i class="<?php echo esc_attr( $menu_item['icon'] ); ?>" aria-hidden="true"></i>
						<?php } ?>
						<span class="title"><?php echo esc_html( $menu_item['title'] ); ?></span>
					</a>
				</li>
			<?php } ?>
		</ul>
		<span class="gn-separator" ></span>
	<?php } ?>
	<?php
	if ( in_array( 'switch_role', gigneo_active_modules(), true ) ) {
		if ( 'freelancer' === $profile_type || 'employer' === $profile_type ) {
			$second_profile_type      = 'freelancer' === $profile_type ? 'employer' : 'freelancer';
			$second_profile_id        = gigneo_profile_id( $current_user->ID, $second_profile_type );
			$second_profile_name      = gigneo_profile_name( $second_profile_id );
			$second_profile_name      = ! empty( $second_profile_name ) ? $second_profile_name : gigneo_profile_name( $profile_id );
			$second_profile_type_text = 'freelancer' === $second_profile_type ? gigneo_freelancer_singular() : gigneo_employer_singular();
			$second_profile_image_id  = $second_profile_id ? get_post_thumbnail_id( $second_profile_id ) : '';
			?>
			<div class="gn-switch-role-wrapper">
				<h5 class="gn-switch-role-title"><?php echo esc_html__( 'Switch Role', 'gigneo' ); ?></h5>
				<div id="gn-switch-profile" class="gn-user-profile">
					<figure class="gn-user-profile-avatar">
						<img src="<?php echo esc_url( gigneo_profile_avatar_url( $second_profile_id, $second_profile_type, 'medium' ) ); ?>" alt="">
					</figure>
					<div class="gn-profile-info">
						<h3 class="gn-name"><?php echo esc_html( $second_profile_name ); ?></h3>
						<p class="gn-type"><?php echo esc_html( $second_profile_type_text ); ?></p>
					</div>
				</div>
			</div>
			<?php
		}
	}
	?>
	<div class="gn-user-menu-footer">
		<a class="gn-btn gn-btn-outline-grey gn-btn-sm gn-btn-block" href="<?php echo esc_url( wp_logout_url( home_url() ) ); ?>">
			<i class="hi hi-arrow-right-start-on-rectangle" aria-hidden="true"></i>
			<span class="title"><?php echo esc_html__( 'Sign Out', 'gigneo' ); ?></span>
		</a>
	</div>
</div>

