<?php

/**
 * The file for header all actions
 *
 *
 * @package Affiliate Eye
 */

function affiliate_eye_header_top_output()
{
	$affiliate_eye_latest_news = get_theme_mod('affiliate_eye_latest_news', 1);
	$affiliate_eye_date_show = get_theme_mod('affiliate_eye_date_show', 1);
	if ($affiliate_eye_latest_news && $affiliate_eye_date_show) {
		$affiliate_eye_break_col = '10';
		$affiliate_eye_date_col = '2';
	} elseif ($affiliate_eye_latest_news) {
		$affiliate_eye_break_col = '12';
		$affiliate_eye_date_col = ' ';
	} elseif ($affiliate_eye_date_show) {
		$affiliate_eye_break_col = ' ';
		$affiliate_eye_date_col = '12';
	}
?>
	<div class="header-top">
		<div class="container">
			<div class="header-top-all-items">
				<div class="row">
					<?php if ($affiliate_eye_latest_news) : ?>
						<div class="col-lg-<?php echo esc_attr($affiliate_eye_break_col); ?>">
							<div class="breaking-news">
								<div class="breaking-news-title">
									<i class="fas fa-wifi"></i>
									<h5 class="breaking-title"><?php esc_html_e('Latest', 'affiliate-eye'); ?></h5>
								</div>
								<div class="news-update ticker news-noload">
									<?php
									$news_update_args = array(
										'post_type'  		=>	'post',
										'post_status'  		=>	'publish',
										'posts_per_page' 	=> 5,
										'ignore_sticky_posts' 	    =>	1,
									);
									$news_update_loop = new WP_Query($news_update_args);
									while ($news_update_loop->have_posts()) :  $news_update_loop->the_post(); ?>
										<div> <?php the_title(); ?>&nbsp; | &nbsp;</div>
									<?php endwhile;
									wp_reset_postdata(); ?>
								</div>
							</div>
						</div>
					<?php endif; ?>
					<?php if ($affiliate_eye_date_show) : ?>
						<div class="col-lg-<?php echo esc_attr($affiliate_eye_date_col); ?> ml-auto">
							<div class="web-date d-flex">
								<i class="far fa-calendar-check"></i>
								<p><?php echo date(get_option('date_format')); ?></p>
							</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>

<?php
}
add_action('affiliate_eye_header_top', 'affiliate_eye_header_top_output');

function affiliate_eye_header_logo_output()
{
	$affiliate_eye_mlogo_show = get_theme_mod('affiliate_eye_mlogo_show', 1);
	$affiliate_eye_search_show = get_theme_mod('affiliate_eye_search_show', 1);
	$affiliate_eye_header_social_show = get_theme_mod('affiliate_eye_header_social_show', 1);
	$affiliate_eye_hfacebook_link = get_theme_mod('affiliate_eye_hfacebook_link');
	$affiliate_eye_htwitter_link = get_theme_mod('affiliate_eye_htwitter_link');
	$affiliate_eye_hlinkedin_link = get_theme_mod('affiliate_eye_hlinkedin_link');
	$affiliate_eye_hyoutube_link = get_theme_mod('affiliate_eye_hyoutube_link');
	$affiliate_eye_hpinterest_link = get_theme_mod('affiliate_eye_hpinterest_link');
	$affiliate_eye_hinstagram_link = get_theme_mod('affiliate_eye_hinstagram_link');

	if ($affiliate_eye_search_show && $affiliate_eye_header_social_show) {
		$affiliate_eye_logo_col = '4';
		$affiliate_eye_search_col = '5';
		$affiliate_eye_social_col = '3';
	} elseif ($affiliate_eye_header_social_show) {
		$affiliate_eye_logo_col = '8';
		$affiliate_eye_search_col = ' ';
		$affiliate_eye_social_col = '4';
	} elseif ($affiliate_eye_search_show) {
		$affiliate_eye_logo_col = '7';
		$affiliate_eye_search_col = '5';
		$affiliate_eye_social_col = ' ';
	} else {
		$affiliate_eye_logo_col = '12';
		$affiliate_eye_search_col = ' ';
		$affiliate_eye_social_col = ' ';
	}

?>
	<div class="header-middle">
		<div class="container">
			<div class="header-middle-all-content">
				<div class="row">
					<div class="col-lg-<?php echo esc_attr($affiliate_eye_logo_col); ?>">
						<?php
						if ($affiliate_eye_mlogo_show) {
							affiliate_eye_logo_output();
						}
						?>
					</div>
					<?php if ($affiliate_eye_search_show) : ?>
						<div class="col-lg-<?php echo esc_attr($affiliate_eye_search_col); ?>">
							<div class="npaper search-box">
								<?php echo get_search_form(); ?>

							</div>
						</div>
					<?php endif; ?>
					<?php if ($affiliate_eye_header_social_show) : ?>
						<div class="col-lg-<?php echo esc_attr($affiliate_eye_social_col); ?>">
							<div class="header-links">
								<div class="social-links">
									<?php if ($affiliate_eye_hfacebook_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_hfacebook_link); ?>"><i class="fab fa-facebook-f"></i></a>
									<?php endif; ?>
									<?php if ($affiliate_eye_htwitter_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_htwitter_link); ?>"><i class="fab fa-twitter"></i></a>
									<?php endif; ?>
									<?php if ($affiliate_eye_hlinkedin_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_hlinkedin_link); ?>"><i class="fab fa-linkedin-in"></i></a>
									<?php endif; ?>
									<?php if ($affiliate_eye_hyoutube_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_hyoutube_link); ?>"><i class="fab fa-youtube"></i></a>
									<?php endif; ?>
									<?php if ($affiliate_eye_hpinterest_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_hpinterest_link); ?>"><i class="fab fa-pinterest"></i></a>
									<?php endif; ?>
									<?php if ($affiliate_eye_hinstagram_link) : ?>
										<a href="<?php echo esc_url($affiliate_eye_hinstagram_link); ?>"><i class="fab fa-instagram"></i></a>
									<?php endif; ?>
								</div>
							</div>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
<?php
}
add_action('affiliate_eye_header_logo', 'affiliate_eye_header_logo_output');

// Affiliate Eye mene style
function affiliate_eye_main_menu_output()
{
	$affiliate_eye_menubar_show = get_theme_mod('affiliate_eye_menubar_show', 1);
	if (empty($affiliate_eye_menubar_show)) {
		return;
	}

	$affiliate_eye_menubarlogo_show = get_theme_mod('affiliate_eye_menubarlogo_show', 1);
	$affiliate_eye_mainmenu_show = get_theme_mod('affiliate_eye_mainmenu_show', 1);
	$affiliate_eye_menusearch_show = get_theme_mod('affiliate_eye_menusearch_show', 1);

?>
	<div class="menu-bar ">
		<div class="container">
			<div class="menubar-content">
				<?php
				if ($affiliate_eye_menubarlogo_show) {
					affiliate_eye_logo_output();
				}
				?>
				<div class="affiliate-eye-container menu-inner">
					<?php if ($affiliate_eye_mainmenu_show) : ?>
						<nav id="site-navigation" class="main-navigation">
							<?php
							wp_nav_menu(array(
								'theme_location' => 'main-menu',
								'menu_id'        => 'affiliate-eye-menu',
								'menu_class'     => 'affiliate-eye-menu',
								'fallback_cb'    => 'affiliate_eye_fallback_menu',
							));
							?>
						</nav><!-- #site-navigation -->
					<?php endif; ?>
					<?php if ($affiliate_eye_menusearch_show) : ?>
						<div class="serach-show">
							<div class="besearch-icon">
								<a href="#" id="besearch"><i class="fas fa-search"></i></a>
							</div>
							<div id="bspopup" class="soff">
								<div id="affsearch" class="sopen">
									<button data-widget="remove" id="removeClass" class="sclose" type="button">×</button>
									<?php get_search_form(); ?>
									<small class="beshop-cradit"><?php esc_html_e('Affiliate Eye Theme By', 'affiliate-eye') ?> <a target="_blank" title="<?php esc_attr_e('Affiliate Eye Theme', 'affiliate-eye') ?>" href="<?php echo esc_url('https://wpthemespace.com/product/beshop/'); ?>"><?php esc_html_e('Wp Theme Space', 'affiliate-eye') ?></a></small>
								</div>
							</div>
						</div>
				</div>
			<?php endif; ?>

			</div>

		</div>
	</div>

<?php
}
add_action('affiliate_eye_main_menu', 'affiliate_eye_main_menu_output');



// Affiliate Eye mobile menu
function affiliate_eye_mobile_menu_output()
{
?>
	<div id="wsm-menu" class="mobile-menu-bar wsm-menu">
		<div class="container">
			<div class="mobile-topbar">
				<div class="mobile-topbar-logo">
					<?php
					if (has_custom_logo()) {
						the_custom_logo();
					} else {
					?>
						<a href="<?php echo esc_url(home_url('/')); ?>" class="mobile-site-title" rel="home">
							<?php bloginfo('name'); ?>
						</a>
					<?php } ?>
				</div>
				<button id="mmenu-btn" class="menu-btn" aria-expanded="false" aria-controls="mobile-menu-panel" aria-label="<?php esc_attr_e('Open mobile menu', 'affiliate-eye'); ?>">
					<span class="mopen" aria-hidden="true">
						<svg class="hamburger-icon" width="26" height="26" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" focusable="false">
							<path d="M3 12h18M3 6h18M3 18h18" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
						</svg>
					</span>
					<span class="mclose" aria-hidden="true">
						<svg class="close-icon" width="26" height="26" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" focusable="false">
							<path d="M18 6L6 18M6 6l12 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" />
						</svg>
					</span>
					<span class="sr-only"><?php esc_html_e('Menu', 'affiliate-eye'); ?></span>
				</button>
			</div>
		</div>

		<!-- Overlay backdrop -->
		<div id="mobile-menu-overlay" class="mobile-menu-overlay" aria-hidden="true"></div>

		<!-- Slide-in menu panel -->
		<nav id="mobile-menu-panel" class="mobile-menu-panel" role="navigation" aria-label="<?php esc_attr_e('Mobile Menu', 'affiliate-eye'); ?>" aria-hidden="true">
			<div class="mobile-panel-header">
				<div class="mobile-panel-logo">
					<?php
					if (has_custom_logo()) {
						the_custom_logo();
					} else {
					?>
						<a href="<?php echo esc_url(home_url('/')); ?>" class="mobile-site-title" rel="home">
							<?php bloginfo('name'); ?>
						</a>
					<?php } ?>
				</div>
				<button id="mmenu-close-btn" class="menu-close-btn" aria-label="<?php esc_attr_e('Close mobile menu', 'affiliate-eye'); ?>">
					<svg width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" focusable="false">
						<path d="M18 6L6 18M6 6l12 12" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" />
					</svg>
				</button>
			</div>
			<div id="mobile-navigation" class="mobile-navigation">
				<?php
				wp_nav_menu(array(
					'theme_location' => 'main-menu',
					'menu_id'        => 'wsm-menu-ul',
					'menu_class'     => 'wsm-menu-has',
					'container'      => false,
					'fallback_cb'    => 'affiliate_eye_fallback_menu',
				));
				?>
			</div>
		</nav>
	</div>

<?php
}
add_action('affiliate_eye_mobile_menu', 'affiliate_eye_mobile_menu_output');

/**
 * Fallback menu for main navigation
 * Displays Home link and up to 6 pages (max 7 items total)
 */
function affiliate_eye_fallback_menu($args = array())
{
	// Extract arguments
	$menu_id = isset($args['menu_id']) ? $args['menu_id'] : 'affiliate-eye-menu';
	$menu_class = isset($args['menu_class']) ? $args['menu_class'] : 'affiliate-eye-menu';

	// Get available pages (max 6 to include Home as first)
	$pages = get_pages(array(
		'sort_column'  => 'menu_order, post_title',
		'number'       => 6,
		'post_status'  => 'publish',
	));

	// Start menu output
	echo '<ul id="' . esc_attr($menu_id) . '" class="' . esc_attr($menu_class) . '">';

	// First item: Home
	$home_class = (is_front_page()) ? ' class="current-menu-item"' : '';
	echo '<li' . $home_class . '><a href="' . esc_url(home_url('/')) . '">' . esc_html__('Home', 'affiliate-eye') . '</a></li>';

	// Add available pages (max 6)
	if (!empty($pages)) {
		foreach ($pages as $page) {
			$current_class = (is_page($page->ID)) ? ' class="current-menu-item"' : '';
			echo '<li' . $current_class . '><a href="' . esc_url(get_permalink($page->ID)) . '">' . esc_html($page->post_title) . '</a></li>';
		}
	}

	echo '</ul>';
}


function affiliate_eye_logo_output()
{
	$affiliate_eye_hide_tagline = get_theme_mod('affiliate_eye_hide_tagline');
?>
	<div class="head-logo-sec">
		<?php if (has_custom_logo()) : ?>
			<div class="site-branding brand-logo">
				<?php the_custom_logo(); ?>
			</div>
		<?php else: ?>
			<?php
			if (display_header_text() == true || (display_header_text() == true && is_customize_preview())) : ?>
				<div class="site-branding brand-text">
					<?php if (display_header_text() == true || (display_header_text() == true && is_customize_preview())) : ?>
						<h1 class="site-title"><a href="<?php echo esc_url(home_url('/')); ?>" rel="home"><?php bloginfo('name'); ?></a></h1>
						<?php
						$affiliate_eye_description = get_bloginfo('description', 'display');
						if (($affiliate_eye_description || is_customize_preview()) && empty($affiliate_eye_hide_tagline)) :
						?>
							<p class="site-description"><?php echo $affiliate_eye_description; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 
														?></p>
						<?php endif; ?>
					<?php endif; ?>
				</div><!-- .site-branding -->
			<?php endif; ?>
		<?php endif; ?>
	</div>
<?php
}
