<?php
/**
 * @package Betheme
 * @author Muffin group
 * @link https://muffingroup.com
 */

global $woocommerce;

$has_shop = false;
$has_user = false;
$has_cart = false;
$has_wishlist = false;

// has shop

if( isset( $woocommerce ) && function_exists('is_woocommerce') ){
	$has_shop = true;
}

// shop icons hide

$shop_icons_hide = mfn_opts_get('shop-icons-hide');

// shop user

if( $has_shop && empty( $shop_icons_hide['user'] ) ){
	$has_user = true;
}

$user_icon = trim( mfn_opts_get('shop-user') );

// shop wishlist

if( $has_shop && empty( $shop_icons_hide['wishlist'] ) && mfn_opts_get('shop-wishlist') && mfn_opts_get('shop-wishlist-page') ){
	$has_wishlist = true;
}

$wishlist_icon = trim( mfn_opts_get('shop-icon-wishlist') );

// shop cart

if( $has_shop && empty( $shop_icons_hide['cart'] ) ){
	$has_cart = true;
}

$cart_icon = trim( mfn_opts_get('shop-cart') );

// search

$header_search = mfn_opts_get('header-search');

// action button

$action_link = mfn_opts_get('header-action-link');

// WPML icon

if (has_nav_menu('lang-menu')) {
	$wpml_icon = true;
} elseif (function_exists('icl_get_languages') && mfn_opts_get('header-wpml') != 'hide') {
	$wpml_icon = true;
} else {
	$wpml_icon = false;
}

// class

$class = [];

$shop_cart_total_hide = mfn_opts_get('shop-cart-total-hide');
if( is_array( $shop_cart_total_hide ) ){
	unset( $shop_cart_total_hide['post-meta'] );
	foreach( $shop_cart_total_hide as $val=>$k ){
		$class[] = 'hide-total-'. $val;
	}
}

$class = implode(' ', $class);

// output -----

if ( $has_user || $has_wishlist || $has_cart || $header_search || $action_link || $wpml_icon ) {
	echo '<div class="top_bar_right '. esc_attr( $class ).'">';
		echo '<div class="top_bar_right_wrapper">';

			// shop user

			if( $has_user ){

				echo '<a id="myaccount_button" class="top-bar-right-icon top-bar-right-icon-user toggle-login-modal '. ( is_user_logged_in() ? 'logged-in' : 'logged-out' ) .'" href="'. get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) .'">';
					if( is_user_logged_in() ){
  					$current_user = wp_get_current_user();
  					echo get_avatar( $current_user->ID, 32 );
					} else {
						if( $user_icon ){
							echo '<i class="'. $user_icon .'"></i>';
						} else {
							echo '<svg width="26" viewBox="0 0 26 26"><defs><style>.path{fill:none;stroke:#333333;stroke-width:1.5px;}</style></defs><circle class="path" cx="13" cy="9.7" r="4.1"/><path class="path" d="M19.51,18.1v2.31h-13V18.1c0-2.37,2.92-4.3,6.51-4.3S19.51,15.73,19.51,18.1Z"/></svg>';
						}
					}
				echo '</a>';

				echo '<div class="mfn-header-login woocommerce '. ( is_user_logged_in() ? "mfn-header-modal-nav" : "mfn-header-modal-login" ) .'">';
					echo '<span class="mfn-close-icon toggle-login-modal close-login-modal"><span class="icon">&#10005;</span></span>';
					if( ! is_user_logged_in()){
						echo '<h4>'; esc_html_e( 'Login', 'woocommerce' ); echo '</h4>';
						echo woocommerce_login_form();
						$opt_reg = get_option( 'woocommerce_enable_myaccount_registration' );
						if( $opt_reg == 'yes' ){
							echo '<p class="create_account"><a href="'.get_permalink( get_option('woocommerce_myaccount_page_id') ).'">';
								esc_html_e( 'Create an account?', 'woocommerce' );
							echo '</a></p>';
						}
					} else {
						echo '<h4>';
							printf( __( 'Hello %s,', 'woocommerce' ), esc_html( wp_get_current_user()->user_login ) );
						echo '</h4>';
						woocommerce_account_navigation();
					}
				echo '</div>';

			}

			// shop wishlist

			if( $has_wishlist ){
				echo '<a id="wishlist_button" class="top-bar-right-icon top-bar-right-icon-wishlist" href="'. get_permalink( mfn_opts_get('shop-wishlist-page') ). '">';
					if( $wishlist_icon ){
						echo '<i class="'. $wishlist_icon .'"></i>';
					} else {
						echo '<svg width="26" viewBox="0 0 26 26"><defs><style>.path{fill:none;stroke:#333;stroke-width:1.5px;}</style></defs><path class="path" d="M16.7,6a3.78,3.78,0,0,0-2.3.8A5.26,5.26,0,0,0,13,8.5a5,5,0,0,0-1.4-1.6A3.52,3.52,0,0,0,9.3,6a4.33,4.33,0,0,0-4.2,4.6c0,2.8,2.3,4.7,5.7,7.7.6.5,1.2,1.1,1.9,1.7H13a.37.37,0,0,0,.3-.1c.7-.6,1.3-1.2,1.9-1.7,3.4-2.9,5.7-4.8,5.7-7.7A4.3,4.3,0,0,0,16.7,6Z"/></svg>';
					}
					echo '<span class="header-wishlist-count">0</span>';
				echo '</a>';
			}

			// shop cart

			if ( $has_cart ) {

				if( mfn_opts_get('shop-sidecart') && ! is_cart() && ! is_checkout() ){
					$class = 'toggle-mfn-cart';
				} else {
					$class = false;
				}

				echo '<a id="header_cart" class="top-bar-right-icon top-bar-right-icon-cart '. esc_attr( $class ) .'" href="'. esc_url( wc_get_cart_url() ) .'">';

					if( $cart_icon ){
						echo '<i class="'. $cart_icon .'"></i>';
					} else {
						echo '<svg width="26" viewBox="0 0 26 26"><defs><style>.path{fill:none;stroke:#333;stroke-miterlimit:10;stroke-width:1.5px;}</style></defs><polygon class="path" points="20.4 20.4 5.6 20.4 6.83 10.53 19.17 10.53 20.4 20.4"/><path class="path" d="M9.3,10.53V9.3a3.7,3.7,0,1,1,7.4,0v1.23"/></svg>';
					}

					echo '<span class="header-cart-count">'. esc_html( $woocommerce->cart->cart_contents_count ) .'</span>';
					echo '<p class="header-cart-total">'. wp_strip_all_tags( $woocommerce->cart->get_cart_total() ) .'</p>';

				echo '</a>';

			}

			// search icon

			$search_icon = '<svg width="26" viewBox="0 0 26 26"><defs><style>.path{fill:none;stroke:#000;stroke-miterlimit:10;stroke-width:1.5px;}</style></defs><circle class="path" cx="11.35" cy="11.35" r="6"/><line class="path" x1="15.59" y1="15.59" x2="20.65" y2="20.65"/></svg>';

			if ( 'input' == $header_search ) {

				$translate['search-placeholder'] = mfn_opts_get('translate') ? mfn_opts_get('translate-search-placeholder', 'Enter your search') : __('Enter your search', 'betheme');

				echo '<a id="search_button" class="top-bar-right-input has-input">';
					echo '<form method="get" id="searchform" action="'. esc_url(home_url('/')) .'">';

						echo $search_icon;
						echo '<input type="text" class="field" name="s" placeholder="'. esc_html($translate['search-placeholder']) .'" />';

						do_action('wpml_add_language_form_field');

						echo '<input type="submit" class="submit" value="" style="display:none;" />';

					echo '</form>';
				echo '</a>';

			} elseif ( $header_search ) {

				echo '<a id="search_button" class="top-bar-right-icon top-bar-right-icon-search" href="#">';
					echo $search_icon;
				echo '</a>';

			}

			// languages menu

			get_template_part('includes/include', 'wpml');

			// action button

			if ($action_link) {
				$action_options = mfn_opts_get('header-action-target');

				if (isset($action_options['target'])) {
					$action_target = 'target="_blank"';
				} else {
					$action_target = false;
				}

				if (isset($action_options['scroll'])) {
					$action_class = ' scroll';
				} else {
					$action_class = false;
				}

				echo '<a href="'. esc_url($action_link) .'" class="action_button top-bar-right-button '. esc_attr($action_class) .'" '. wp_kses_data($action_target) .'>'. wp_kses(mfn_opts_get('header-action-title'), mfn_allowed_html('button')) .'</a>';
			}

		echo '</div>';
	echo '</div>';
}

?>
