<?php
/**
 * Displays main header
 *
 * @package Store Eshop
 */
?>
<?php
$store_eshop_sticky_header = get_theme_mod('store_eshop_sticky_header');
    $store_eshop_data_sticky = "false";
    if ($store_eshop_sticky_header) {
    $store_eshop_data_sticky = "true";
    }
?>
<div class="main-header text-center text-md-start" data-sticky="<?php echo esc_attr($store_eshop_data_sticky); ?>">
    <div class="container">
        <div class="row nav-box">
            <div class="col-xl-2 col-lg-3 col-md-4 col-sm-5 col-12 align-self-center header-categories-box">
                <?php if(class_exists('woocommerce')){ ?>
                    <div class="all-categories">
                        <button class="cat-btn"><i class="fas fa-bars me-2"></i><?php esc_html_e('All Categories','store-eshop'); ?></button>
                        <div class="home_product_cat">
                            <?php $store_eshop_args = array(
                                'number'     => '',
                                'orderby'    => 'title',
                                'order'      => 'ASC',
                                'hide_empty' => '',
                                'include'    => ''
                            );
                            $store_eshop_product_categories = get_terms('product_cat', $store_eshop_args);
                            $store_eshop_count = count($store_eshop_product_categories);

                            if ($store_eshop_count > 0) {
                                foreach ($store_eshop_product_categories as $product_category) {
                                    // Get category image ID
                                    $thumbnail_id = get_term_meta($product_category->term_id, 'thumbnail_id', true);
                                    // Get the image URL
                                    $store_eshop_image_url = wp_get_attachment_image_url($thumbnail_id, 'medium'); // Change 'medium' to desired image size
                                    
                                    // Display category image and name
                                    echo '<div class="product-category">';
                                        if ($store_eshop_image_url) {
                                            echo '<img src="' . esc_url($store_eshop_image_url) . '" alt="' . esc_attr($product_category->name) . '" />';
                                        } else {
                                            echo '<img src="' . esc_url(get_template_directory_uri() . '/assets/img/icon.png') . '" alt="Default Image" />';
                                        }
                                        echo '<h4><a href="' . esc_url(get_term_link($product_category)) . '">' . esc_html($product_category->name) . '</a></h4>';
                                        echo '</div>';

                                    $store_eshop_args = array(
                                        'posts_per_page' => -1,
                                        'tax_query' => array(
                                            'relation' => 'AND',
                                            array(
                                                'taxonomy' => 'product_cat',
                                                'field'    => 'slug',
                                                'terms'    => $product_category->slug
                                            )
                                        ),
                                        'post_type' => 'product',
                                        'orderby'   => 'title'
                                    );
                                }
                            }
                            ?>

                        </div>
                    </div>
                <?php }?>
            </div>
            <div class="col-xl-8 col-lg-6 col-md-4 col-sm-2 col-12 align-self-center header-box">
                <?php get_template_part('template-parts/navigation/nav'); ?>
            </div>
            <div class="col-xl-2 col-lg-3 col-md-4 col-sm-5 col-12 align-self-center header-right-box">
                <?php if ( get_theme_mod('store_eshop_topbar_phone_number') != "" ) {?>
			        <div class="text-end py-2">
			            <p class="location m-0"><a href="tell:<?php echo esc_attr(get_theme_mod('store_eshop_topbar_phone_number')); ?>"><i class="fas fa-phone-volume me-2"></i><?php echo esc_html(get_theme_mod('store_eshop_topbar_phone_number')); ?></a></p>  
			        </div>
		        <?php }?>
            </div>
        </div>
    </div>
</div>
