<?php

/**
 * The Template for displaying product archives, including the main shop page which is a post type archive.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 */
defined('ABSPATH') || exit;

$layout          = isset($_GET['bs']) ? sanitize_text_field($_GET['bs']) : glamer_options('glamer_shop_layout', 'right-sidebar');
$column_class    = ($layout === 'left-sidebar' || $layout === 'right-sidebar') ? 'col-12 col-sm-12 col-md-12 col-lg-12 col-xl-8' : 'col-12 col-sm-12 col-md-12 col-lg-12 col-xl-12';

// Check the current view: default to 'grid' if not set
$current_view = isset($_GET['view']) ? sanitize_text_field($_GET['view']) : 'grid';

// Set active classes based on the current view
$grid_active_class = ($current_view === 'grid') ? 'show active' : '';
$list_active_class = ($current_view === 'list') ? 'show active' : '';

get_header('shop');

do_action('woocommerce_before_main_content'); ?>

<div class="page-layout woo-layout <?php echo esc_attr($layout . ' shop-grid-' . (($layout === 'left-sidebar' || $layout === 'right-sidebar') ? 'has-sidebar' : 'no-sidebar')); ?>">
    <div class="row">
        <?php if ($layout === 'left-sidebar' && is_active_sidebar('glamer-shop')) : ?>
            <div id="secondary" class="col-xl-4 col-lg-12 col-md-12 col-sm-12 col-12 sidebar-widget-area woo-widget-area">
                <?php dynamic_sidebar('glamer-shop'); ?>
            </div>
        <?php endif; ?>

        <div class="<?php echo esc_attr($column_class . ' gl-shop-wrapper'); ?>">
            <div class="woo-shop-topbar">
                <div class="woo-grid-menu">
                    <ul class="nav nav-pills" id="shop-grid-tabs" role="tablist">
                        <li class="nav-item">
                            <button class="nav-link active bar-icon" id="shop-grid-tab" data-bs-toggle="pill" data-bs-target="#shop-grid-tab-view" type="button" aria-controls="shop-grid-tab-view" aria-selected="true">
                                <i class="fas fa-th"></i>
                            </button>
                        </li>
                        <li class="nav-item">
                            <button class="nav-link bar-icon" id="shop-list-tab" data-bs-toggle="pill" data-bs-target="#shop-list-tab-view" type="button" aria-controls="shop-list-tab-view" aria-selected="false">
                                <i class="fas fa-list"></i>
                            </button>
                        </li>
                    </ul>
                </div>
                <div class="woo-result-count-wrapper">
                    <?php woocommerce_result_count(); ?>
                </div>
                <div class="woo-sort-list">
                    <?php woocommerce_catalog_ordering(); ?>
                </div>
            </div>

            <div class="all-posts-wrapper woo-all-post">
                <div class="tab-content" id="shop-grid-tabsContent">
                    <div class="tab-pane fade <?php echo esc_attr($grid_active_class); ?>" id="shop-grid-tab-view" role="tabpanel" aria-labelledby="shop-grid-tab">
                        <?php if (woocommerce_product_loop()) :
                            do_action('woocommerce_before_shop_loop');
                            woocommerce_product_loop_start();
                            while (have_posts()) : the_post();
                                wc_get_template_part('content', 'product');
                            endwhile;
                            woocommerce_product_loop_end();
                            do_action('woocommerce_after_shop_loop');
                        else :
                            do_action('woocommerce_no_products_found');
                        endif; ?>
                    </div>

                    <div class="tab-pane fade <?php echo esc_attr($list_active_class); ?>" id="shop-list-tab-view" role="tabpanel" aria-labelledby="shop-list-tab">
                        <?php if (woocommerce_product_loop()) :
                            do_action('woocommerce_before_shop_loop');
                            woocommerce_product_loop_start();
                            while (have_posts()) : the_post();
                                wc_get_template_part('content', 'product-list');
                            endwhile;
                            woocommerce_product_loop_end();
                            do_action('woocommerce_after_shop_loop');
                        else :
                            do_action('woocommerce_no_products_found');
                        endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <?php if ($layout === 'right-sidebar' && is_active_sidebar('glamer-shop')) : ?>
            <div id="secondary" class="col-xl-4 col-lg-12 col-md-12 col-sm-12 col-12 sidebar-widget-area woo-widget-area">
                <?php dynamic_sidebar('glamer-shop'); ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php
do_action('woocommerce_after_main_content');
get_footer('shop');
