<?php
/**
 * The header for our theme.
 *
 * This is the template that displays all of the <head> section
 * 
 * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
 *
 * @package shopbar
 */

?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<?php wp_head(); ?>
</head>

<body <?php body_class();?>>
	<?php 
      wp_body_open();
    ?>

<!-- Skip to main content -->
<a class="skip-link" href="#main-content">
    <?php esc_html_e( 'Skip to content', 'shopbar' ); ?>
</a>

<!-- Search Overlay -->
<div id="searchOverlay" class="search-overlay">
    <div class="search-overlay-backdrop"></div>
    <div class="search-overlay-inner">
        <div class="search-overlay-header">
            <div class="search-overlay-heading">
                <span class="search-overlay-label"><?php esc_html_e( 'Search', 'shopbar' ); ?></span>
            </div>
            <button id="search-close" class="search-close-btn" aria-label="<?php esc_attr_e( 'Close search', 'shopbar' ); ?>">
                <span class="search-close-text"><?php esc_html_e( 'Close', 'shopbar' ); ?></span>
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M18 6L6 18M6 6l12 12" stroke="currentColor" stroke-width="1.5" stroke-linecap="round"/>
                </svg>
            </button>
        </div>
        <div class="search-input-wrapper">
            <?php get_search_form(); ?>
        </div>
        <div class="search-trending">
            <p class="search-trending-label"><?php esc_html_e( 'Trending', 'shopbar' ); ?></p>
            <div class="search-tags">
                <?php
                $trending_searches = array();
                if ( function_exists( 'shopbar_get_trending_searches' ) ) {
                    $trending_searches = apply_filters( 'shopbar_trending_searches', shopbar_get_trending_searches( 6 ) );
                }
                if ( ! empty( $trending_searches ) && is_array( $trending_searches ) ) :
                    foreach ( $trending_searches as $search_term ) :
                ?>
                <a href="<?php echo esc_url( home_url( '/?s=' . urlencode( $search_term ) . ( shopbar_is_active_woocommerce() ? '&post_type=product' : '' ) ) ); ?>" class="search-tag"><?php echo esc_html( $search_term ); ?></a>
                <?php
                    endforeach;
                endif;
                ?>
            </div>
        </div>
    </div>
</div>

<!-- Mobile Menu Overlay -->
<div id="mobileMenuOverlay" class="mobile-menu-overlay"></div>
<!-- Mobile Menu -->
<div id="mobileMenuDrawer" class="mobile-menu-fullscreen" role="dialog" aria-modal="true" aria-label="<?php esc_attr_e( 'Mobile Menu', 'shopbar' ); ?>">
    <button id="mobileMenuClose" class="mobile-menu-close-btn" aria-label="<?php esc_attr_e( 'Close menu', 'shopbar' ); ?>">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
            <line x1="18" y1="6" x2="6" y2="18"/>
            <line x1="6" y1="6" x2="18" y2="18"/>
        </svg>
    </button>
    <nav class="mobile-menu-nav">
        <?php 
        wp_nav_menu(array(
            'theme_location' => 'primary',
            'container' => false,
            'menu_class' => 'mobile-menu-links',
            'fallback_cb' => 'shopbar_default_menu_fallback',
            'depth' => 3,
        ));
        ?>
    </nav>
    <div class="mobile-menu-footer">
        <?php if ( shopbar_is_active_woocommerce() ) : 
            $myaccount_page_id = get_option('woocommerce_myaccount_page_id');
            if ($myaccount_page_id) :
        ?>
        <a href="<?php echo esc_url(get_permalink($myaccount_page_id)); ?>" class="mobile-menu-footer-link">
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round">
                <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/>
                <circle cx="12" cy="7" r="4"/>
            </svg>
            <?php esc_html_e( 'Account', 'shopbar' ); ?>
        </a>
        <?php 
            endif;
        endif; 
        ?>
        <?php do_action( 'shopbar_mobile_menu_footer' ); ?>
    </div>
</div>

<?php
// Account URL.
$shopbar_myaccount_url = '#';
if ( shopbar_is_active_woocommerce() ) {
    $shopbar_myaccount_id = get_option( 'woocommerce_myaccount_page_id' );
    if ( $shopbar_myaccount_id ) {
        $shopbar_myaccount_url = get_permalink( $shopbar_myaccount_id );
    }
}

// Shop / deal link.
$shopbar_shop_url = ( shopbar_is_active_woocommerce() && function_exists( 'wc_get_page_permalink' ) ) ? wc_get_page_permalink( 'shop' ) : home_url( '/' );
?>

<?php if ( is_active_sidebar( 'topbar' ) ) : ?>
<!-- TOP BAR -->
<div class="announcement-bar">
    <?php dynamic_sidebar( 'topbar' ); ?>
</div>
<?php else : ?>
<!-- TOP BAR-->
<div class="sb-topbar">
    <div class="sb-topbar-inner">
        <div class="sb-topbar-left">
            <span class="sb-topbar-item"><?php shopbar_icon( 'truck', 14 ); ?><span><?php esc_html_e( 'Free Shipping on Orders Over $49', 'shopbar' ); ?></span></span>
            <span class="sb-topbar-item"><?php shopbar_icon( 'rotate', 14 ); ?><span><?php esc_html_e( '30-Day Returns', 'shopbar' ); ?></span></span>
            <span class="sb-topbar-item"><?php shopbar_icon( 'lock', 14 ); ?><span><?php esc_html_e( 'Secure Payment', 'shopbar' ); ?></span></span>
        </div>
        <div class="sb-topbar-right">
            <a href="<?php echo esc_url( $shopbar_myaccount_url ); ?>" class="sb-topbar-link"><?php esc_html_e( 'Track Order', 'shopbar' ); ?></a>
            <a href="<?php echo esc_url( $shopbar_myaccount_url ); ?>" class="sb-topbar-link"><?php esc_html_e( 'Help Center', 'shopbar' ); ?></a>
            <div class="sb-topbar-social">
                <a href="#" class="sb-social-icon" aria-label="<?php esc_attr_e( 'Facebook', 'shopbar' ); ?>"><?php shopbar_icon( 'facebook', 14 ); ?></a>
                <a href="#" class="sb-social-icon" aria-label="<?php esc_attr_e( 'Instagram', 'shopbar' ); ?>"><?php shopbar_icon( 'instagram', 14 ); ?></a>
                <a href="#" class="sb-social-icon" aria-label="<?php esc_attr_e( 'X (Twitter)', 'shopbar' ); ?>"><?php shopbar_icon( 'twitter', 14 ); ?></a>
                <a href="#" class="sb-social-icon" aria-label="<?php esc_attr_e( 'Pinterest', 'shopbar' ); ?>"><?php shopbar_icon( 'pinterest', 14 ); ?></a>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<!-- HEADER -->
<header id="mainHeader" class="site-header">

    <!-- Main Row: Logo + Search + Actions -->
    <div class="header-main">
        <div class="header-inner header-main-inner">

            <!-- Logo -->
            <div class="site-branding">
                <?php
                $shopbar_show_brand_text = get_theme_mod( 'shopbar_display_site_title_tagline', true );
                $shopbar_site_desc       = get_bloginfo( 'description', 'display' );

                if ( has_custom_logo() ) :
                    if ( $shopbar_show_brand_text ) :
                        $shopbar_custom_logo_id = get_theme_mod( 'custom_logo' );
                        ?>
                        <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="site-logo-link">
                            <img src="<?php echo esc_url( wp_get_attachment_image_url( $shopbar_custom_logo_id, 'full' ) ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" class="custom-logo">
                            <span class="site-title-text">
                                <span class="site-title-name"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></span>
                                <?php if ( $shopbar_site_desc ) : ?>
                                    <span class="site-description"><?php echo esc_html( $shopbar_site_desc ); ?></span>
                                <?php endif; ?>
                            </span>
                        </a>
                        <?php
                    else :
                        shopbar_custom_logo();
                    endif;
                elseif ( $shopbar_show_brand_text ) :
                    ?>
                    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="site-logo-text">
                        <span class="site-title-text">
                                <span class="site-title-name"><?php echo esc_html( get_bloginfo( 'name' ) ); ?><span class="period">.</span></span>
                                <?php if ( $shopbar_site_desc ) : ?>
                                    <span class="site-description"><?php echo esc_html( $shopbar_site_desc ); ?></span>
                                <?php endif; ?>
                            </span>
                    </a>
                    <?php
                endif;
                ?>
            </div>

            <!-- Inline Search Bar -->
            <div class="sb-header-search">
                <?php get_search_form( array( 'context' => 'header' ) ); ?>
            </div>

            <!-- Header Actions -->
            <div class="nav-actions">
                <!-- Account -->
                <a href="<?php echo esc_url( $shopbar_myaccount_url ); ?>" class="nav-action-item nav-action-desktop" aria-label="<?php esc_attr_e( 'My Account', 'shopbar' ); ?>">
                    <span class="nav-action-icon"><?php shopbar_icon( 'user', 20 ); ?></span>
                    <span class="nav-action-label">
                        <span class="label-sm"><?php esc_html_e( 'Login / Register', 'shopbar' ); ?></span>
                        <span class="label-bold"><?php esc_html_e( 'My Account', 'shopbar' ); ?></span>
                    </span>
                </a>

                <?php do_action( 'shopbar_nav_after_search' ); ?>

                <!-- Wishlist -->
                <?php do_action( 'shopbar_action_header_wishlist' ); ?>

                <!-- Cart -->
                <?php if ( shopbar_is_active_woocommerce() && function_exists( 'wc_get_cart_url' ) ) : ?>
                <a href="<?php echo esc_url( wc_get_cart_url() ); ?>" class="nav-action-item cart-icon-wrap" id="cart-toggle" aria-label="<?php esc_attr_e( 'Cart', 'shopbar' ); ?>">
                    <span class="nav-action-icon">
                        <?php shopbar_icon( 'cart', 20 ); ?>
                        <span class="cart-count"><?php echo esc_html( WC()->cart ? WC()->cart->get_cart_contents_count() : '0' ); ?></span>
                    </span>
                    <span class="nav-action-label">
                        <span class="label-sm"><?php esc_html_e( 'My Cart', 'shopbar' ); ?></span>
                        <span class="label-bold"><?php echo ( WC()->cart && WC()->cart->get_cart_contents_count() > 0 ) ? wp_kses_data( WC()->cart->get_cart_subtotal() ) : esc_html__( '$0.00', 'shopbar' ); ?></span>
                    </span>
                </a>
                <?php endif; ?>

                <!-- Mobile search trigger -->
                <button class="nav-action-btn search-btn" id="search-toggle" aria-label="<?php esc_attr_e( 'Search', 'shopbar' ); ?>">
                    <?php shopbar_icon( 'search', 20 ); ?>
                </button>

                <!-- Hamburger Menu -->
                <button class="hamburger" id="hamburger" aria-label="<?php esc_attr_e( 'Menu', 'shopbar' ); ?>">
                    <span></span>
                    <span></span>
                    <span></span>
                </button>
            </div>
        </div>
    </div>

    <!-- Navbar Row -->
    <div class="sb-navbar">
        <div class="header-inner sb-navbar-inner">

            <!-- Browse Categories -->
            <div class="sb-browse">
                <button type="button" class="sb-browse-toggle" aria-haspopup="true" aria-expanded="false">
                    <span class="sb-browse-icons"><?php shopbar_icon( 'menu', 18 ); ?></span>
                    <span class="sb-browse-text"><?php echo esc_html( get_theme_mod( 'shopbar_header_browse_categories_text', __( 'Browse Categories', 'shopbar' ) ) ); ?></span>
                    <span class="sb-browse-caret"><?php shopbar_icon( 'chevron-down', 14 ); ?></span>
                </button>
                <?php if ( has_nav_menu( 'category-header' ) ) : ?>
                    <div class="sb-browse-panel">
                        <?php
                        wp_nav_menu( array(
                            'theme_location' => 'category-header',
                            'container'      => false,
                            'menu_class'     => 'sb-browse-list',
                            'depth'          => 1,
                            'fallback_cb'    => false,
                        ) );
                        ?>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Desktop Navigation -->
            <nav class="main-navigation">
                <ul class="nav-links">
                    <?php
                    wp_nav_menu(array(
                        'theme_location' => 'primary',
                        'container' => false,
                        'items_wrap' => '%3$s',
                        'fallback_cb' => 'shopbar_default_menu_fallback',
                        'depth' => 3,
                    ));
                    ?>
                </ul>
            </nav>

            <!-- Today's Deal -->
            <?php if ( get_theme_mod( 'shopbar_deal_show_header', true ) ) : ?>
                <?php if ( get_theme_mod( 'shopbar_deal_enable', true ) ) : ?>
                <button type="button" class="sb-todays-deal" id="todays-deal-toggle" aria-haspopup="dialog" aria-expanded="false" aria-controls="todaysDealModal">
                    <?php shopbar_icon( 'zap', 16 ); ?>
                    <span><?php echo esc_html( get_theme_mod( 'shopbar_deal_button_label', __( "Today's Deal", 'shopbar' ) ) ); ?></span>
                </button>
                <?php else : ?>
                <a href="<?php echo esc_url( $shopbar_shop_url ); ?>" class="sb-todays-deal">
                    <?php shopbar_icon( 'zap', 16 ); ?>
                    <span><?php echo esc_html( get_theme_mod( 'shopbar_deal_button_label', __( "Today's Deal", 'shopbar' ) ) ); ?></span>
                </a>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</header>

<?php get_template_part( 'template-parts/header/todays-deal-modal' ); ?>
