<?php
/**
 * Template part for displaying header menu
 *
 * @package Legacy Book Club
 */

?>
<?php
    $legacy_book_club_page_val= is_front_page() ? 'home':'page' ;

?>

<header id="<?php echo esc_attr($legacy_book_club_page_val);?>-inner" class="elementer-menu-anchor theme-menu-wrapper full-width-menu style1 page" role="banner">
    <?php
        if(true===get_theme_mod('legacy_book_club_enable_highlighted area',true) && is_front_page()){
            ?><a class="skip-link screen-reader-text" href="#main"><?php esc_html_e('skip to content','legacy-book-club'); ?> </a> <?php
        }
        else{
        ?><a class="skip-link screen-reader-text" href="#main"><?php esc_html_e('skip to content','legacy-book-club');?></a> <?php
    }
    ?>
    <div id="header-main" class="header-wrapper">
        <div id="topbar">
            <div class="container-fluid px-4">
                <div class="row">
                    <div class="col-lg-6 col-md-12 col-12">                        
                    </div>
                    <div class="col-lg-6 col-md-12 col-12 align-self-center">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-6 align-self-center text-center">
                                <div class="hdr-account">
                                <?php if(class_exists('woocommerce')){ ?>
                                    <?php if ( is_user_logged_in() ) { ?>
                                        <div><a class="my-account" href="<?php echo esc_url( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); ?>" title="<?php esc_attr_e('My Account','legacy-book-club'); ?>"><p class="account-txt m-0" ><?php esc_html_e( 'My Account','legacy-book-club' );?></p></a></div>
                                    <?php }
                                    else { ?>
                                        <a class="my-account" href="<?php echo esc_url( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); ?>" title="<?php esc_attr_e('Login / Register','legacy-book-club'); ?>"><?php esc_html_e( 'Login / Register','legacy-book-club' );?></a>
                                    <?php } ?>
                                <?php }?>
                                </div>
                            </div>
                            <div class="col-lg-2 col-md-2 col-6 align-self-center text-center">
                                <div class="wishlist">
                                   <?php if (class_exists('woocommerce')) { ?>
                                        <?php echo do_shortcode('[ti_wishlist_products_counter]'); ?>
                                    <?php } ?>
                                </div>
                            </div>
                            <div class="col-lg-4 col-md-4 col-12 align-self-center text-center px-md-0">
                               <div class="follow-us my-2 my-lg-0">
                                    <?php
                                        $legacy_book_club_social_media1_heading = get_theme_mod( 'legacy_book_club_social_media1_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media1_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media1_heading ); ?>"><i class="bi bi-facebook me-4"></i></a>
                                    <?php } ?>
                                    <?php
                                        $legacy_book_club_social_media2_heading = get_theme_mod( 'legacy_book_club_social_media2_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media2_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media2_heading ); ?>"><i class="bi bi-instagram me-4"></i></a>
                                    <?php } ?>
                                    <?php
                                        $legacy_book_club_social_media3_heading = get_theme_mod( 'legacy_book_club_social_media3_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media3_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media3_heading ); ?>"><i class="bi bi-twitter-x me-4"></i></a>
                                    <?php } ?>
                                    <?php
                                        $legacy_book_club_social_media4_heading = get_theme_mod( 'legacy_book_club_social_media4_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media4_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media4_heading ); ?>"><i class="bi bi-youtube me-4"></i></a>
                                    <?php } ?>
                                    <?php
                                        $legacy_book_club_social_media5_heading = get_theme_mod( 'legacy_book_club_social_media5_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media5_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media5_heading ); ?>"><i class="bi bi-pinterest me-4"></i></a>
                                    <?php } ?>
                                    <?php
                                        $legacy_book_club_social_media6_heading = get_theme_mod( 'legacy_book_club_social_media6_heading', '' );
                                        if ( ! empty( $legacy_book_club_social_media6_heading ) ) { ?>
                                        <a href="<?php echo esc_url( $legacy_book_club_social_media6_heading ); ?>"><i class="bi bi-linkedin me-4"></i></a>
                                    <?php } ?>
                                </div> 
                            </div>
                            <div class="col-lg-2 col-md-2 col-6 align-self-center px-lg-0 px-md-0">
                                <?php
                                    $legacy_book_club_topbar_register_button_link = get_theme_mod( 'legacy_book_club_topbar_register_button_link', '' );
                                    if ( ! empty( $legacy_book_club_topbar_register_button_link ) ) { ?>
                                    <div class="topbar-button btn text-center py-2">
                                        <a href="<?php echo esc_url( $legacy_book_club_topbar_register_button_link ); ?>"><?php echo esc_html('Register','legacy-book-club'); ?></a>
                                    </div> 
                                <?php } ?>
                            </div>
                            <div class="col-lg-2 col-md-2 col-6 align-self-center px-0">
                                <?php
                                    $legacy_book_club_topbar_login_button_link = get_theme_mod( 'legacy_book_club_topbar_login_button_link', '' );
                                    if ( ! empty( $legacy_book_club_topbar_login_button_link ) ) { ?>
                                    <div class="topbar-button2 btn text-center py-2">
                                        <a href="<?php echo esc_url( $legacy_book_club_topbar_login_button_link ); ?>"><i class="bi bi-lock-fill me-1"></i><?php echo esc_html('Login','legacy-book-club'); ?></a> 
                                    </div> 
                                <?php } ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div id="custom-header">
            <?php if ( display_header_text() ) : ?>
            <div id="topbar2">
                <div class="container-fluid">
                    <div class="row py-3">
                        <div class="col-lg-2 col-md-2 col-12 align-self-center text-center text-lg-start text-md-start">
                            <div class="logo <?php echo (has_custom_logo() ? 'has-logo' : 'no-logo'); ?>" itemscope itemtype="https://schema.org/Organization">
                                <?php 
                                    // Display custom logo if available
                                    if ( has_custom_logo() ) {
                                        legacy_book_club_custom_logo();
                                    }

                                    // Display sticky header logo if enabled
                                    if ( get_theme_mod( 'legacy_book_club_enable_logo_stickyheader', false ) ) {
                                        $legacy_book_club_alt_logo = esc_url( get_theme_mod( 'legacy_book_club_logo_stickyheader' ) );
                                        if ( ! empty( $legacy_book_club_alt_logo ) ) {
                                            ?>
                                            <a id="logo-alt" class="logo-alt" href="<?php echo esc_url( home_url( '/' ) ); ?>">
                                                <img src="<?php echo esc_url( $legacy_book_club_alt_logo ); ?>" alt="<?php esc_attr_e( 'logo', 'legacy-book-club' ); ?>">
                                            </a>
                                            <?php
                                        }
                                    }

                                    // Site title and tagline settings
                                    $legacy_book_club_show_title   = get_theme_mod( 'legacy_book_club_display_site_title', true );
                                    $legacy_book_club_show_tagline = get_theme_mod( 'legacy_book_club_display_site_tagline', false );
                                    $legacy_book_club_header_class = $legacy_book_club_show_title ? 'site-title' : 'screen-reader-text';

                                    // Display site title
                                    if ( $legacy_book_club_show_title && get_bloginfo( 'name' ) ) {
                                        if ( is_front_page() ) {
                                            ?>
                                            <h1 class="<?php echo esc_attr( $legacy_book_club_header_class ); ?>">
                                                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></a>
                                            </h1>
                                            <?php
                                        } else {
                                            ?>
                                            <p class="<?php echo esc_attr( $legacy_book_club_header_class ); ?>">
                                                <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></a>
                                            </p>
                                            <?php
                                        }
                                    }

                                    // Display tagline
                                    if ( $legacy_book_club_show_tagline ) {
                                        $legacy_book_club_description = get_bloginfo( 'description', 'display' );
                                        if ( $legacy_book_club_description || is_customize_preview() ) {
                                            ?>
                                            <p class="site-description"><?php echo esc_html( $legacy_book_club_description ); ?></p>
                                            <?php
                                        }
                                    }
                                ?>
                            </div>
                        </div>
                        <div class="col-lg-7 col-md-5 col-12 align-self-center">
                            <div class="topbr-search-box px-2">
                                <form method="get" class="woocommerce-product-search" action="<?php echo esc_url(home_url('/')); ?>">
                                    <div class="search-field-wrapper">            
                                        <span>
                                            <input type="search" class="search-field px-3" placeholder="<?php echo esc_attr__('Search...', 'legacy-book-club'); ?>" value="<?php echo get_search_query(); ?>" name="s" />
                                            <input type="hidden" name="post_type" value="product" />
                                        </span>
                                        <?php if (class_exists('woocommerce')) { ?>
                                            <span>            
                                                <select name="product_cat" id="product_cat" class="search-category-dropdown">
                                                    <option value=""><?php esc_html_e('All Categories', 'legacy-book-club'); ?></option>
                                                    <?php 
                                                    $categories = get_terms(array(
                                                        'taxonomy' => 'product_cat',
                                                        'hide_empty' => true,
                                                    ));

                                                    if (!is_wp_error($categories) && !empty($categories)) {
                                                        foreach ($categories as $category) {
                                                            if (is_object($category) && isset($category->slug)) {
                                                                echo '<option value="' . esc_attr($category->slug) . '">' . esc_html($category->name) . '</option>';
                                                            }
                                                        }
                                                    }
                                                    ?>
                                                </select>
                                            </span>
                                        <?php } ?>
                                        <span>            
                                            <button type="submit" value="<?php echo esc_attr__('Search', 'legacy-book-club'); ?>" class="search-submit">
                                                <i class="bi bi-search"></i>
                                            </button>
                                        </span>
                                    </div>
                                </form>
                            </div>
                        </div>
                        <div class="col-lg-2 col-md-3 col-12 align-self-center text-lg-start text-center text-md-start">
                            <?php
                                $legacy_book_club_header_phone_number = get_theme_mod('legacy_book_club_header_phone_number', '');
                                $legacy_book_club_header_phone_head = get_theme_mod('legacy_book_club_header_phone_head', '');

                                if ( ! empty( $legacy_book_club_header_phone_number ) ) { ?>
                                    <div class="row call-detail mb-3 mb-lg-0 mb-md-0">
                                        <div class="col-lg-2 col-md-2 col-3 r8-call-icn align-self-center text-end pe-0">
                                            <i class="bi bi-telephone call-icn"></i>
                                        </div>
                                        <div class="col-lg-10 col-md-10 col-9 align-self-center r8-call">
                                            <p class="hd-call mb-0">
                                                <?php echo esc_html( $legacy_book_club_header_phone_head ); ?>
                                            </p>
                                            <p class="hd-call-no mb-0">
                                                <a href="tel:<?php echo esc_attr( preg_replace( '/[^0-9+]/', '', $legacy_book_club_header_phone_number ) ); ?>">
                                                    <?php echo esc_html( $legacy_book_club_header_phone_number ); ?>
                                                </a>
                                            </p>
                                        </div>
                                    </div>
                            <?php } ?>
                        </div>
                        <div class="col-lg-1 col-md-2 col-12 align-self-center text-lg-end text-center text-md-end hdr-icns">
                            <span class="tbr2-account">
                                <?php if(class_exists('woocommerce')){ ?>
                                    <?php if ( is_user_logged_in() ) { ?>
                                        <a class="my-account" href="<?php echo esc_url( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); ?>" title="<?php esc_attr_e('My Account','legacy-book-club'); ?>"><i class="bi bi-person"></i></a>
                                    <?php }
                                    else { ?>
                                        <a class="my-account" href="<?php echo esc_url( get_permalink( get_option('woocommerce_myaccount_page_id') ) ); ?>" title="<?php esc_attr_e('Login / Register','legacy-book-club'); ?>"><?php esc_html_e( 'Login / Register','legacy-book-club' );?></a>
                                    <?php } ?>
                                <?php }?>
                            </span>
                            <span class="topbr2-wishlist px-2">
                                <?php if (class_exists('woocommerce')) { ?>
                                    <?php echo do_shortcode('[ti_wishlist_products_counter]'); ?>
                                <?php } ?>
                            </span>
                            <span class="tbr2-cart pe-lg-3">
                                <?php legacy_book_club_custom_woocommerce_cart_icon(); ?>
                            </span>
                        </div>
                    </div>
                </div>            
            </div>
         <?php endif; ?>
        </div>
        <div id="content-header">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-lg-3 col-md-6 col-10 px-3">
                        <div class="cont-hdr-dd"> 
                            <?php if (class_exists('woocommerce')) { ?>  
                                <div class="custom-dropdown" data-base-url="<?php echo esc_url(home_url('/index.php/product-category/')); ?>">
                                    <button class="dropdown-button">
                                        <span class="bar-cat">
                                            <i class="bi bi-text-left me-2"></i>
                                        </span>
                                        <?php esc_html_e('Categories', 'legacy-book-club'); ?>
                                    </button>
                                    <ul class="dropdown-list">
                                        <?php 
                                        $categories = get_terms(array(
                                            'taxonomy' => 'product_cat',
                                            'hide_empty' => true,
                                        ));
                                        foreach ($categories as $category) {
                                            $legacy_book_club_category_link = get_term_link($category);
                                            if (!is_wp_error($legacy_book_club_category_link)) { // Ensure the link generation doesn't fail
                                                echo '<li data-value="' . esc_attr($category->slug) . '">';
                                                echo '<a href="' . esc_url($legacy_book_club_category_link) . '">' . esc_html($category->name) . '</a>';
                                                echo '</li>';
                                            }
                                        }
                                        ?>
                                    </ul>
                                </div>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="col-lg-8 col-md-5 col-2 align-self-center">
                        <div class="top-menu-wrapper">
                            <div class="navigation_header">
                                <div class="toggle-nav mobile-menu">
                                    <button onclick="legacy_book_club_openNav()"><i class="bi bi-list"></i></button>
                                </div>
                                <div id="mySidenav" class="nav sidenav">
                                    <nav id="site-navigation" class="main-navigation navbar navbar-expand-xl" aria-label="<?php esc_attr_e( 'Top Menu', 'legacy-book-club' ); ?>">
                                        <?php {
                                                wp_nav_menu(
                                                    array(
                                                        'theme_location' => 'primary',
                                                        'container_class' => 'navi clearfix navbar-nav' ,
                                                        'menu_class'     => 'menu clearfix', 
                                                        'items_wrap'     => '<ul id="%1$s" class="%2$s">%3$s</ul>',
                                                        'fallback_cb' => 'wp_page_menu',
                                                    )
                                                );
                                            } ?>
                                    </nav>
                                    <a href="javascript:void(0)" class="closebtn mobile-menu" onclick="legacy_book_club_closeNav()"><i class="bi bi-x"></i></a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-1 col-md-1 col-12">
                        
                    </div>
                </div>                
            </div>
        </div>
    </div>    
</header>

<div class="clearfix"></div>
<div id="content" class="elementor-menu-anchor"></div>

<div class="content-wrap">