<?php
//print_r($_GET);
/**
 * The Template for displaying product archives, including the main shop page which is a post type archive
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/archive-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.0.0
 */
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
$current_page = 'catalogue';
get_header();
$product;
$cate = get_queried_object();
$cateID = $cate->term_id;

//SubCats
$child_category = get_terms('product_cat', array('parent' => $cateID, 'child_of' => the_category_ID(), 'exclude' => $cateID, 'orderby' => 'title', 'order' => 'ASC'));
?>

<div class="container clearfix" id="catalogue" data-page = "<?php echo $cateID; ?>">
    <div class="container-bg abs-full"></div>
    <?php get_sidebar() ?>
    <div class="header mobile-nav display--only-mobile blue-bg fixed">
        <div class="nav-wrapper blue-bg">
            <ul></ul>
        </div>
    </div>
    <div class="right-container">
        <div class="left-panel fixed-full position--after-menu ">
            <?php if ($cateID != 20): ?>
                <div class="filter-wrapper">
                    <div class="filter-btn fixed position--after-menu">
                        <div class="btn-container"><span class="abs-center font-small">Filtres</span></div>
                    </div>
                    <div class="filter-content">
                        <div class="filter-list fixed position--after-menu">
                            <div class="top-header">
                                <span>Filtres</span>
                                <span class="close-btn"></span>
                            </div>

                            <?php
                            $attributes = array('Univers' => 'pa_univers', 'Matiére' => 'pa_matiere', 'Format' => 'pa_format', 'Couleurs' => 'pa_couleur');
                            foreach ($attributes as $key => $value) :
                                ?>
                                <div class="filter-bloc <?php if ($_GET[$value] != '') echo 'active selected-filter'; ?>">
                                    <div class="header">
                                        <p><span><?php echo $key ?></span></p>
                                    </div>

                                    <?php
                                    if ($value == 'pa_couleur') {
                                        $terms = get_terms($value);
                                    } else
                                        $terms = nap_attribute_by_category($cateID, $value);

                                    if (!empty($terms) && !is_wp_error($terms)):
                                        ?>
                                        <div class="bloc-content">
                                            <ul <?php if ($value == 'pa_couleur') echo 'class = "list-color-filter clearfix"'; ?> id="<?php echo $value; ?>">
                                                <?php
                                                $colors = array();
                                               
                                                foreach ( $terms as $term) :
                                                    
                                                    if ($value == 'pa_couleur'):
                                                        $colors[$term->name] = get_field('couleurs', 'pa_couleur_' . $term->term_id);

                                                    else :
                                                        $filter_format = nap_selected_filter($_GET['pa_format'], $term);
                                                        $filter_matiere = nap_selected_filter($_GET['pa_matiere'], $term);
                                                        $filter_univers = nap_selected_filter($_GET['pa_univers'], $term);
                                                        
                                                       ?>
                                                
                                                        <li class="<?php echo $filter_format . ' ' . $filter_matiere .' '.$filter_univers ?>"><?php echo $term; ?></li>
                                                    <?php
                                                        
                                                    endif;
                                                endforeach;
                                                foreach (array_unique($colors) as $name => $code) {
                                                    if (isset($code) && strlen($code) > 0):
                                                        $filter_class = nap_selected_filter($_GET['pa_couleur'], $code);
                                                        ?>
                                                        <li data-color = "<?php echo $code; ?>" class="<?php echo $name . ' ' . $filter_class; ?>"></li>
                                                        <?php
                                                    endif;
                                                }
                                                if ($value == 'pa_couleur'):
                                                    ?>

                                                    <li class="reinit">Tout déselectionner</li>
                                                <?php endif; ?>
                                            </ul>

                                        </div>
                                    <?php endif; ?>
                                </div>
                            <?php endforeach; ?>



                            <div class="center filter-btn-list">
                                <div class="btn pink-btn rounded" id="search-btn">Rechercher</div>
                                <span class = "reinit-all">Effacer les filtres</span>
                            </div>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php
            foreach ($child_category as $key => $value) :
                ?>
                <div class="cover-wrapper abs-full transition" data-cat = "#cat<?php echo $value->term_id ?>" style = "background-image: url('<?php echo nap_category_image($value->term_id) ?>')">
                    <div class="cover-desc-wrapper">
                    <?php if ($value->description){ ?>
                        <p><?php echo $value->description; ?></p>
                    <?php } ?>
                    </div>
                </div>   
                <?php
            endforeach;
            ?>
        </div>
        <div class="right-panel right">
            <div class="panel panel-content list-animation">
                <?php if ($_GET['pa_couleur'] != '' || $_GET['pa_format'] != '' || $_GET['pa_matiere'] != ''): ?>
                    <span  class="search_result">Résultat de votre recherche :</span>
                <?php endif; ?>

                <?php foreach ($child_category as $key => $value) : ?>

                    <section id = "cat<?php echo $value->term_id ?>">
                        <h2 class="crete small-title"><?php echo $value->name ?></h2>
                        <div class="cover-mobile display--only-mobile animated fadeInUp" style = "background-image: url('<?php echo nap_category_image($value->term_id) ?>')">
                            <div class="cover-desc-wrapper">
                                <p><?php echo $value->description; ?></p>
                            </div>
                        </div>
                        <div class="product-list">
                            <?php
                            $args = array('post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'product_cat' => $value->name, 'orderby' => 'title', 'order' => 'ASC');
                            $naps_posts = new WP_Query($args);
                            $options = array();

                            while ($naps_posts->have_posts()) : $naps_posts->the_post();
                                $id = get_the_ID();
                                $list_att = array();

                                $options['size'] = (get_field('produit_affichage', $id)) ? get_field('produit_affichage', $id) : 'small';
                                $args = array(
                                    'post_type' => array('product', 'product_variation'),
                                    'post_status' => array('private', 'publish'),
                                    'numberposts' => -1,
                                    'orderby' => 'menu_order',
                                    'order' => 'asc',
                                    'post_parent' => $id
                                );
                                $attribute_value['pa_couleur'] = get_the_terms($id, 'pa_couleur');
                                $attribute_value['pa_format'] = get_the_terms($id, 'pa_format');
                                $attribute_value['pa_matiere'] = get_the_terms($id, 'pa_matiere');
                                $attribute_value['pa_univers'] = get_the_terms($id, 'pa_univers');

                                if ($_GET) {
                                    if (is_array($attribute_value['pa_couleur'])) {
                                        foreach ($attribute_value['pa_couleur'] as $key => $value) {
                                            $list_att['pa_couleur'] = nap_color_attribute($value->slug);
                                        }
                                    }
                                    if (is_array($attribute_value['pa_format'])) {
                                        foreach ($attribute_value['pa_format'] as $key => $value) {
                                            $list_att['pa_format'] = $value->name;
                                        }
                                    }
                                    if (is_array($attribute_value['pa_matiere'])) {
                                        foreach ($attribute_value['pa_matiere'] as $key => $value) {
                                            $list_att['pa_matiere'] = $value->name;
                                        }
                                    }
                                    if (is_array($attribute_value['pa_univers'])) {
                                        foreach ($attribute_value['pa_univers'] as $key => $value) {
                                            $list_att['pa_univers'] = $value->name;
                                        }
                                    }
                                    
                                    nap_filter_attribute($id, $args, $options, $list_att, $_GET);
                                } else {
                                    echo nap_get_item($id, $args, $current_page, $options);
                                }
                            endwhile;
                            wp_reset_query();
                            ?>
                        </div> 
                    </section>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
</div>
<?php get_footer(); ?>