<?php

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

global $gigneo_args, $current_user;

$home_url       = home_url( '/' );
$profile_id     = gigneo_active_profile_id();
$profile_type   = gigneo_profile_type( $profile_id );
$profile_avatar = gigneo_profile_avatar_url();
$user_name      = gigneo_profile_name( $profile_id );
$icon_id        = gigneo_option( '_gigneo_ts_main_icon' );
$logo_id        = gigneo_option( '_gigneo_ts_main_logo' );
$menu_scheme    = gigneo_option( '_gigneo_ts_dashboard_menu_scheme', 'default' );
$layout_color   = gigneo_option( '_gigneo_ts_dashboard_layout_background' );
$thumbnail_id   = get_post_thumbnail_id( $profile_id );

//Params
$source = isset( $gigneo_args['source'] ) ? sanitize_text_field( wp_unslash( $gigneo_args['source'] ) ) : '';
$ref    = isset( $gigneo_args['ref'] ) ? sanitize_text_field( wp_unslash( $gigneo_args['ref'] ) ) : '';

?>
<div class="gn-dashboard-side-menu-panel">
	<div class="gn-dashboard-side-menu-inner">
		<!--Menu Header-->
		<div class="gn-dashboard-side-menu-top">
			<a href="<?php echo esc_url( $home_url ); ?>" class="gn-dashboard-logo">
				<?php
				$icon_url = wp_get_attachment_image_url( $icon_id, 'full' );
				$icon_url = ! empty( $icon_url ) ? $icon_url : GIGNEO_DIR_URL . 'assets/img/gradient-icon.png';

				$logo_url = wp_get_attachment_image_url( $logo_id, 'full' );
				$logo_url = ! empty( $logo_url ) ? $logo_url : ( 'light' === $menu_scheme ? GIGNEO_DIR_URL . 'assets/img/logo-light.png' : GIGNEO_DIR_URL . 'assets/img/logo-dark.png' );
				?>
				<img class="gn-dash-icon" src="<?php echo esc_url( $icon_url ); ?>" alt="<?php echo esc_attr__( 'icon', 'gigneo' ); ?>" />
				<img class="gn-dash-logo" src="<?php echo esc_url( $logo_url ); ?>" alt="<?php echo esc_attr__( 'logo', 'gigneo' ); ?>" />
			</a>
			<button type="button" class="gn-side-menu-toggle">
				<i class="hi hi-x-mark" aria-hidden="true"></i>
			</button>
		</div>
		<!--Menu Lists-->
		<div class="gn-dashboard-side-menu-body">
			<?php
			//Platform Menu
			$platform_menu_list = gigneo_dashboard_menu_list( $profile_type, 'platform' );
			if ( ! empty( $platform_menu_list ) ) {
				?>
			<div class="gn-dashboard-menu gn-dashboard-menu-platform">
				<h6 class="gn-menu-label"><?php echo esc_html__( 'Platform', 'gigneo' ); ?></h6>
				<ul class="gn-menu-list">
				<?php
				foreach ( $platform_menu_list as $slug => $menu_item ) {
					$menu_url    = gigneo_dashboard_url( $menu_item['source'] ?? '', $menu_item['ref'] ?? '' );
					$menu_class  = 'gn-menu-' . $slug;
					$active      = ( 'dashboard' === $slug && empty( $source ) ) || $source === $menu_item['source'];
					$menu_class .= $active ? ' gn-active' : '';

					// Check if any submenu
					$has_submenu        = ! empty( $menu_item['submenu'] );
					$has_active_submenu = false;
					$menu_class        .= $has_submenu ? ' gn-has-submenu' : '';

					if ( $has_submenu ) {
						foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
							if ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] ) {
								$has_active_submenu = true;
								break;
							}
						}
					}

					?>
					<li class="<?php echo esc_attr( $menu_class ); ?>">
						<a 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="gn-title"><?php echo esc_html( $menu_item['title'] ); ?></span>
							<?php if ( isset( $menu_item['count'] ) ) { ?>
								<small class="gn-count"><?php echo esc_html( $menu_item['count'] ?? '' ); ?></small>
							<?php } ?>
						</a>
						<?php
						if ( $has_submenu ) {
							?>
							<ul class="gn-submenu-list<?php echo esc_attr( $has_active_submenu ? ' gn-submenu-expand' : '' ); ?>">
								<?php
								foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
									$sub_menu_url = gigneo_dashboard_url( $sub_menu_item['source'] ?? '', $sub_menu_item['ref'] ?? '' );
									$sub_active   = ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] );

									$sub_menu_class  = 'gn-submenu-' . $sub_slug;
									$sub_menu_class .= $sub_active ? ' gn-active' : '';
									$menu_class     .= $sub_active ? ' gn-submenu-expand' : '';
									?>
										<li class="<?php echo esc_attr( $sub_menu_class ); ?>">
											<a href="<?php echo esc_url( $sub_menu_url ); ?>" target="<?php echo esc_attr( $sub_menu_item['target'] ); ?>">
												<?php if ( ! empty( $sub_menu_item['icon'] ) ) { ?>
													<i class="<?php echo esc_attr( $sub_menu_item['icon'] ); ?>" aria-hidden="true"></i>
												<?php } ?>
												<span class="gn-title"><?php echo esc_html( $sub_menu_item['title'] ); ?></span>
												<?php if ( ! empty( $sub_menu_item['count'] ) ) { ?>
													<small class="gn-count"><?php echo esc_html( $sub_menu_item['count'] ); ?></small>
												<?php } ?>
											</a>
										</li>
								<?php } ?>
							</ul>
							<?php
						}
						?>
					</li>
				<?php } ?>
			</ul>
			</div>
			<?php } ?>
			<?php
			//Account Menu
			$account_menu_list = gigneo_dashboard_menu_list( $profile_type, 'account' );
			if ( ! empty( $account_menu_list ) ) {
				?>
				<div class="gn-dashboard-menu gn-dashboard-menu-account">
					<h6 class="gn-menu-label"><?php echo esc_html__( 'Account', 'gigneo' ); ?></h6>
					<ul class="gn-menu-list">
						<?php
						foreach ( $account_menu_list as $slug => $menu_item ) {
							$menu_url    = gigneo_dashboard_url( $menu_item['source'] ?? '', $menu_item['ref'] ?? '' );
							$menu_class  = 'gn-menu-' . $slug;
							$active      = ( 'dashboard' === $slug && empty( $source ) ) || $source === $menu_item['source'];
							$menu_class .= $active ? ' gn-active' : '';

							// Check if any submenu
							$has_submenu        = ! empty( $menu_item['submenu'] );
							$has_active_submenu = false;
							$menu_class        .= $has_submenu ? ' gn-has-submenu' : '';

							if ( $has_submenu ) {
								foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
									if ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] ) {
										$has_active_submenu = true;
										break;
									}
								}
							}
							?>
							<li class="<?php echo esc_attr( $menu_class ); ?>">
								<a 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="gn-title"><?php echo esc_html( $menu_item['title'] ); ?></span>
									<?php if ( isset( $menu_item['count'] ) ) { ?>
										<small class="gn-count"><?php echo esc_html( $menu_item['count'] ?? '' ); ?></small>
									<?php } ?>
								</a>
								<?php
								if ( $has_submenu ) {
									?>
									<ul class="gn-submenu-list<?php echo esc_attr( $has_active_submenu ? ' gn-submenu-expand' : '' ); ?>">
										<?php
										foreach ( $menu_item['submenu'] as $sub_slug => $sub_menu_item ) {
											$sub_menu_url = gigneo_dashboard_url( $sub_menu_item['source'] ?? '', $sub_menu_item['ref'] ?? '' );
											$sub_active   = ( $source === $sub_menu_item['source'] && $ref === $sub_menu_item['ref'] );

											$sub_menu_class  = 'gn-submenu-' . $sub_slug;
											$sub_menu_class .= $sub_active ? ' gn-active' : '';
											$menu_class     .= $sub_active ? ' gn-submenu-expand' : '';
											?>
											<li class="<?php echo esc_attr( $sub_menu_class ); ?>">
												<a href="<?php echo esc_url( $sub_menu_url ); ?>" target="<?php echo esc_attr( $sub_menu_item['target'] ); ?>">
													<?php if ( ! empty( $sub_menu_item['icon'] ) ) { ?>
														<i class="<?php echo esc_attr( $sub_menu_item['icon'] ); ?>" aria-hidden="true"></i>
													<?php } ?>
													<span class="gn-title"><?php echo esc_html( $sub_menu_item['title'] ); ?></span>
													<?php if ( ! empty( $sub_menu_item['count'] ) ) { ?>
														<small class="gn-count"><?php echo esc_html( $sub_menu_item['count'] ); ?></small>
													<?php } ?>
												</a>
											</li>
										<?php } ?>
									</ul>
									<?php
								}
								?>
							</li>
						<?php } ?>
					</ul>
				</div>
			<?php } ?>
		</div>
		<!--Menu Actions-->
		<div class="gn-dashboard-side-menu-footer">
			<?php
			$profile_type_text = 'freelancer' === $profile_type ? gigneo_freelancer_singular() : gigneo_employer_singular();
			$wallet_amount     = gigneo_wallet_balance( $profile_id );
			?>
			<div class="gn-dashboard-side-menu-action-wrapper">
				<div class="gn-user-profile-action">
					<div class="gn-user-profile">
						<?php gigneo_attachment_image_src( $thumbnail_id, 'medium', 'profile' ); ?>
						<div class="gn-profile-info">
							<div class="gn-name-wrapper">
								<h3 class="gn-name"><?php echo esc_html( $user_name ); ?></h3>
								<?php gigneo_verification_badge( $profile_id, true ); ?>
							</div>
							<p class="gn-type"><?php echo esc_html( $profile_type_text ); ?></p>
						</div>
					</div>
					<i class="hi hi-chevron-up-down"></i>
				</div>
				<?php gigneo_load_template( 'dropdown/user-menu' ); ?>
			</div>
		</div>
	</div>
</div>