<?php
/**
* Template hooks
* @package store-expert
*/


/**
* Loads in header.php
* Opens the header tag
* do_action( 'store_expert_wrapopen' );
*/
if ( ! function_exists( 'store_expert_wrap_open' ) ) {
	function store_expert_wrap_open() {
		/**
		 * Pre Loader
		 */
		if( get_theme_mod( 'store_expert_preloader_control', 1 ) == 1 ) : ?>
			<div class="loader-wrapper">
				<div id="pre-loader" class="loading">
					<span></span>
				</div>
			</div>
		<?php endif;

		/**
		 * Content start for boxed layout
		 */
		if( get_theme_mod( 'store_expert_website_layout', 'wide' ) == 'boxed' ) : ?>
			<div class="boxed-content">			
		<?php 
		endif; ?>

		<header id="site-head header" class="site-head header" role="banner">
		<a class="skip-link screen-reader-text" href="#content">
			<?php 
				/**
				* Skip Link
				* This text will appear whel you hit tab or spacebar
				*/
				esc_html_e( 'Skip to content', 'store-expert' );
			?>
		</a><!-- .skip-link screen-reader-text  #content-->
		<div class="wrap">
		<?php 
	}
}

/**
* Loads in header.php
* Closes the header tag
* do_action( 'store_expert_wrapclose' ); in header.php
*/
if ( ! function_exists( 'store_expert_wrap_close' ) ) {
	function store_expert_wrap_close() {
		?>
		</div><!-- .container-fluid -->
		</header><!-- id="site-head" class="site-head" role="banner" -->
		<?php 
	}
}

/**
* Top bar
* All the top bar settings goes here
* do_action( 'store_expert_topbar' ); in header.php
*/
if ( ! function_exists( 'store_expert_topbar_settings' ) ) {
	function store_expert_topbar_settings() {
		/**
		* Check if top bar is enabled in customizer
		*/
		if( get_theme_mod( 'store_expert_top_header_enable', 1 ) == 1 ) :
		?>
		<div class="top-bar">
			<div class="container">
				<div class="row align-items-center">					
					<div class="top-bar-text col-sm">
						<?php 
							/**
							* Get default value of top left content
							*/
							$top_header_left = get_theme_mod( 'store_expert_top_left_text' );

							if ( !empty( $top_header_left ) ):
							/**
							* output Top left content
							*/
							$customizer_repeater_content_decoded = json_decode($top_header_left);
							//Foreach starts
							foreach($customizer_repeater_content_decoded as $repeaters_item) : ?>
								<i class="<?php echo esc_attr( $repeaters_item->icon_value ); ?>"></i>
								<p><?php echo esc_html( $repeaters_item->text ); ?></p>
							<?php 
								endforeach; //foreach ends
								endif;
							?>

					</div>
					<?php
						/**
						* Check if menu is enabled in customizer
						*/
						if( get_theme_mod( 'store_expert_top_header_menu_enable', 1 ) == 1 ) :							
							wp_nav_menu( array(
								'theme_location' => 'topnav',
								'menu_id' => 'menu',
								'container_class' => 'col-md-auto',
								'menu_class' => 'navi  top-bar-navigation',
								'fallback_cb' => false,
								'depth' => -1,
								'items_wrap' => '<ul id="menu" class="%2$s">%3$s</ul>',
							) ) ;
						endif;
					?>
					<?php
						/**
						* Check if social icon is enabled in customizer
						*/
						if( get_theme_mod( 'store_expert_top_header_social_enable', 1 ) == 1 ) : ?>
						<div class="col-md-auto social-links">
							<?php 
								/**
								* Get default value of Social links
								*/
								$top_header_social = get_theme_mod( 'store_expert_social_icons_repeaters', json_encode( array(
									/*The content from your default parameter or delete this argument if you don't want a default*/
									/*Repeater's first item*/
									array( "icon_value" => esc_html__( 'fab fa-whatsapp', 'store-expert' ), "link" => __( 'https://www.facebook.com/', 'store-expert' ) ),
									/*Repeater's second item*/
									array( "icon_value" => esc_html__( 'fab fa-twitter', 'store-expert' ), "link" => __( 'https://www.twitter.com/', 'store-expert' ) ),
									/*Repeater's third item*/
									array( "icon_value" => esc_html__( 'fab fa-vimeo', 'store-expert' ), "link" => __( 'https://www.facebook.com/', 'store-expert' ) ),
								)) );

								/**
								* output social links
								*/
								$customizer_repeater_social_decoded = json_decode($top_header_social);
								//Foreach starts
								foreach($customizer_repeater_social_decoded as $repeater_item) : ?>
									<a href="<?php echo esc_html( $repeater_item->link ); ?>">
										<i class="<?php echo esc_attr( $repeater_item->icon_value ); ?>"></i>
									</a>
								<?php endforeach; //foreach ends ?>
						</div>
						<?php endif; ?>
				</div>
			</div>
		</div>
		<?php 
		endif;
	}
}


if ( ! function_exists( 'store_expert_custom_logo' ) ) :
/**
 * Displays the optional custom logo.
 * Does nothing if the custom logo is not available.
 */
function store_expert_custom_logo() {
	if ( function_exists( 'the_custom_logo' ) ) {
		the_custom_logo();
	}
}
endif;


/* -- Logo */
if ( ! function_exists( 'store_expert_logo_settings' ) ) {
	function store_expert_logo_settings() {
		?>
		<div class="logo-area">
			<div class="container">
				<div class="row align-items-center">
					<div class="logo col-md-3 col-lg-3 col-sm-6 col">
						<?php 
							/**
							 * Displays the optional custom logo.
							 * Does nothing if the custom logo is not available.
							 */
							store_expert_custom_logo();
							// Check if it is front page
							if ( is_front_page() && is_home() ) : 
						?>		
						<h1 class="site-title">
							<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
						</h1>
						<?php else : ?>
							<p class="site-title">
								<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
							</p>
						<?php endif;
						// Show site description 
						$description = get_bloginfo( 'description', 'display' );
						if ( $description || is_customize_preview() ) : ?>
							<p class="site-description"><?php echo $description; ?></p>
						<?php endif; ?>
					</div>					
					<div class="logo-search col-md-6 col-lg-6 ">
						<?php if( get_theme_mod( 'store_expert_middle_header_search_enable', 1 ) == 1 ) : //Search box enable ?>
						<?php
							/**
							 * Fetch the TH advance product search
							 */
							if ( get_theme_mod( 'store_expert_middle_header_search_options', 'wide' ) == 'live' and class_exists( "TH_Advance_Product_Search" ) ) {
	       						echo do_shortcode( '[th-aps layout="bar_style"]' );
	    					}

	    					else { ?>
							<form class="search-form" role="search" method="get" autocomplete="off" action="<?php echo esc_url( home_url( '/' ) ); ?>">
								<div class="search-field align-items-center">
									<input type="hidden" name="post_type" value="product" />
									<select class="search-category" name="product_cat">
										<option value=""><?php esc_html_e( 'All Categories', 'store-expert' ); ?></option> 
										<?php
										$categories = get_categories( 'taxonomy=product_cat' );
										foreach ( $categories as $category ) {
											$option = '<option value="' . esc_attr( $category->category_nicename ) . '">';
											$option .= esc_html( $category->cat_name );
											$option .= ' (' . absint( $category->category_count ) . ')';
											$option .= '</option>';
											echo $option;
										}
										?>
									</select>
									
									<input name="s" type="text" placeholder="<?php esc_attr_e( 'Search products...', 'store-expert' ); ?>"/>
									<span class="search-button">
										<button class="search-icon" type="submit"><i class="fa fa-search"></i></button>
									</span>
								</div>
							</form>
						<?php }; ?>	
						<?php endif; ?>					
					</div>
					
					<div class="logo-icons col-md-3 col-lg-3 col-sm-6 col">
						<?php

							if( get_theme_mod( 'store_expert_middle_header_wishlist_enable', 1 ) == 1 )  {
								/**
								 * display heart icon and wishlist count in header
								 * Refer file inc/woocommerce-functions.php
								 */
								do_action( 'store_expert_wishlist' );
							}

							if( get_theme_mod( 'store_expert_middle_header_cart_enable', 1 ) == 1 )  {
								/**
								 * display cart button and cart total in header
								 * Refer file inc/woocommerce-functions.php
								 */
								do_action( 'store_expert_header_cart' );
							}
						?>
					</div>
				</div>
			</div>
		</div>

		<?php
	}
}


if ( ! function_exists( 'store_expert_main_navigation' ) ) {
/**
 * Display Main Navigation
 */
function store_expert_main_navigation() {
	?>
	<div id="site-header" class="navigation-area">
		<div class="container">
			<div id="site-header" class="align-items-center clearfix">
				<nav id="site-navigation" class="main-navigation">
					<button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">
						<span></span>
						<span></span>
						<span></span>
					</button>
					<div class="main-navigation-links">
						<?php 
							/**
							* Main Navigation menu
							*/						
							wp_nav_menu( array(
								'theme_location' => 'catnav',
								'fallback_cb'    => false, // Do not fall back to wp_page_menu()

							) );
						?>
					</div>
				</nav><!-- #site-navigation -->
			</div>
		</div>
	</div>
	<?php 
	}
}

/* -- post meta for single post and single page */
if ( ! function_exists( 'store_expert_post_meta' ) ) {
	function store_expert_post_meta() {
		?>
		<div class="single-post-meta">				
			<h2>
				<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
			</h2>
			<div class="blog-index-meta">
				<span class="icon-user blog-index-icon"></span>
				<span><?php the_author_posts_link(); ?></span>
				<span class="icon-clock blog-index-icon"></span>
				<span><?php the_time( get_option( 'date_format' ) ); ?></span>
				<span class="icon-bubble blog-index-icon"></span>
				<span><?php comments_popup_link( __( 'post a comment', 'store-expert' ), __( '1 Comment', 'store-expert' ), __( '% Comments', 'store-expert' ),'', __( 'Comments Off', 'store-expert' ));?></span>
			</div>
		</div>
	<?php 
	}
}

/* -- previous / next post navigation for single post and single page */
if ( ! function_exists( 'store_expert_post_navigation' ) ) {
	function store_expert_post_navigation() {
		?>
	<div class="single-postnav clearfix">
		<hr>
			<div class="row clearfix">	
				<div class="next-post col-md-6 col-sm-6 col-xs-6"><?php next_post_link( '<i class="fa fa-chevron-circle-left"></i> %link'); ?></div>
				<div class="previous-post col-md-6 col-sm-6 col-xs-6"><?php previous_post_link( '%link <i class="fa fa-chevron-circle-right"></i>'); ?></div>
			</div>
		<hr>
	</div>
	<?php 
	}
}

/* -- post meta for index and archive lists */
if ( ! function_exists( 'store_expert_index_post_meta' ) ) {
	function store_expert_index_post_meta() {
		?>
		<div class="row blog-index-single">						
			<div class="blog-index-image col-md-5">
				<?php 
					if ( has_post_thumbnail() ) {
						the_post_thumbnail('store-expert-index');
					} 
					else {
					?>
						<img src="<?php echo STORE_EXPERT_THEME_URI . 'img/no-image.png'; // Echo Category Image ?>">
					<?php
					}
				?><!-- thumbnail picture -->
				<span class="blog-index-category"><?php the_category( ' ', 'single' ); ?></span>
			</div>
			<div class="blog-index-content col-md-7">
				<h3>
					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
				</h3>
				<div class="blog-index-meta">
					<span class="icon-user blog-index-icon"></span>
					<span><?php the_author_posts_link(); ?></span>
					<span class="icon-clock blog-index-icon"></span>
					<span><?php the_time( get_option( 'date_format' ) ); ?></span>
					<span class="icon-bubble blog-index-icon"></span>
					<span><?php comments_popup_link( __( 'post a comment', 'store-expert' ), __( '1 Comment', 'store-expert' ), __( '% Comments', 'store-expert' ),'', __( 'Comments Off', 'store-expert' ));?></span>
				</div>
				<?php the_excerpt(); ?>
			</div>
		</div>
	<?php 
	}
}

/* -- pagination for index & archives */
if ( ! function_exists( 'store_expert_index_pagination' ) ) {
	function store_expert_index_pagination() {
		?>
		<div class="index-pagination">
			<?php echo the_posts_pagination(); ?>
		</div>
		<?php 
	}
}


/**
 * Enable Boxed layout based on customizer settings
 */
if( get_theme_mod( 'store_expert_website_layout', 'wide' ) == 'boxed' ) {
	function store_expert_box_layout( $classes ) {
		return array_merge( $classes, array( 'container' ) );
	}
	// Filter to add 'container' class into body class
	add_filter( 'body_class', 'store_expert_box_layout' );
}


/**
 * Content end for boxed layout
 */
function store_expert_box_layout_content_end() {
	if( get_theme_mod( 'store_expert_website_layout', 'wide' ) == 'boxed' ) : ?>
		</div>
	<?php 
	endif;	
}

/**
 * Apply primary colours from Customizer
 */
function store_expert_colours_customize_css(){
	?>
	<style type="text/css">
		:root {
			--primary-colour: <?php echo esc_html( get_theme_mod( 'store_expert_primary_colour', '#007bc4' ) ); ?>;
		}
		.top-bar {
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_top_header_colour', '#007bc4' ) ); ?>;
		}
		.top-bar,
		.top-bar-navigation a,
		.social-links a{
			color: <?php echo esc_html( get_theme_mod( 'store_expert_top_header_text_colour', '#ffffff' ) ); ?>;
		}
		.logo-area {
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_colour', '#fff' ) ); ?>;
		}
		.logo p,
		.site-title a,
		.site-description,
		.logo h1,
		.logo h2,
		.logo h3,
		.love-icon,
		.cart-icon,
		.head-cart-count p,
		.wishlist-count {
			color: <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_text_colour', '#333' ) ); ?>;
		}
		.wishlist-count{
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_icon_text_colour', '#ffd700' ) ); ?>;
		}
		.search-field button{
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_icon_text_colour', '#ffd700' ) ); ?>;
			border: 1px solid <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_icon_text_colour', '#ffd700' ) ); ?>;
		}
		.logo-search .search-form {
			border: 1px solid <?php echo esc_html( get_theme_mod( 'store_expert_middle_header_icon_text_colour', '#ffd700' ) ); ?>;
		}
		.navigation-area,
		ul.ms-product-menu, 
		ul.ms-product-menu ul{
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_navigation_header_colour', '#fff' ) ); ?>;
		}
		.main-navigation a,
		ul.ms-product-menu,
		ul.ms-product-menu li a {
			color: <?php echo esc_html( get_theme_mod( 'store_expert_navigation_link_colour', '#333' ) ); ?>;
		}
		.main-navigation a:focus, 
		.main-navigation a:hover,
		ul.ms-product-menu > li:hover > a, 
		ul.ms-product-menu > li:not(.menu-item-megamenu) ul li:hover > a {
			color: <?php echo esc_html( get_theme_mod( 'store_expert_navigation_link_hover_colour', '#ffffff' ) ); ?>;
			background-color: <?php echo esc_html( get_theme_mod( 'store_expert_navigation_link_bg_hover_colour', '#007bc4' ) ); ?>;
		}
	</style>
	

	
	<?php
}
add_action( 'wp_head', 'store_expert_colours_customize_css');


add_action( 'store_expert_wrapopen', 'store_expert_wrap_open', 0 );
add_action( 'store_expert_wrapclose', 'store_expert_wrap_close', 0 );
add_action( 'store_expert_topbar', 'store_expert_topbar_settings', 0 );
add_action( 'store_expert_header', 'store_expert_logo_settings', 0 );
add_action( 'store_expert_navigation', 'store_expert_main_navigation', 0 );
add_action( 'store_expert_slider', 'store_expert_slider_content', 0 );
add_action( 'store_expert_recent_properties', 'store_expert_home_properties', 0 );
add_action( 'store_expert_recent_posts', 'store_expert_home_posts', 0 );
add_action( 'store_expert_postmeta', 'store_expert_post_meta', 0 );
add_action( 'store_expert_postnavigation', 'store_expert_post_navigation', 0 );
add_action( 'store_expert_index_postmeta', 'store_expert_index_post_meta', 0 );
add_action( 'store_expert_index_pagination', 'store_expert_index_pagination', 0 );
add_action( 'store_expert_boxed_content_end', 'store_expert_box_layout_content_end', 0 );