<?php
/**
 * Template Name: phototheque
 *
 */
$ext_ref = externe_referrer();
if ($ext_ref) {
    get_header();
}
$current_page = 'phototheque';
if (!$ext_ref) {
    $class_back = "int-link";
}
?>
<div id="phototheque" class="clearfix container ">
    <div class="display--only-mobile">
        <div class="container-bg abs-full"></div>
        <?php get_sidebar(); ?>
    </div>
    <div class="phototheque-content">

        <div class="nav-phototeque container gray-bg row fixed">

            <div class="header">
                <div class = "btn-wrapper row close-panel <?php echo $class_back ?>">
                    <div class="pink-btn circle-btn"><a href="<?php echo esc_url(home_url('/')); ?>"><span class = "abs-center backlink">Retour</span></a></div>
                </div>
                <nav class = "row">
                    <h2 class = "crete big-title">Photothèque</h2>

                    <?php include('nav-list.php'); ?>
                </nav>  
            </div>
        </div>


        <?php
        $taxonomy = 'product_cat';
        $orderby = 'id';
        $show_count = 0;      // 1 for yes, 0 for no
        $pad_counts = 0;      // 1 for yes, 0 for no
        $hierarchical = 1;      // 1 for yes, 0 for no  
        $title = '';
        $empty = 0;

        $args = array(
            'taxonomy' => $taxonomy,
            'orderby' => $orderby,
            'show_count' => $show_count,
            'pad_counts' => $pad_counts,
            'hierarchical' => $hierarchical,
            'title_li' => $title,
            'hide_empty' => $empty
        );
        $all_categories = get_categories($args);
        ?>
        <div class="gallery-wrapper right  row">
            <div class="panel panel-content product-list transition clearfix list-animation"> 
                <?php
                foreach ($all_categories as $cat) {
                    if ($cat->category_parent == 0) {
                        $category_id = $cat->term_id;

                        $args2 = array(
                            'taxonomy' => $taxonomy,
                            'child_of' => 0,
                            'parent' => $category_id,
                            'orderby' => $orderby,
                            'show_count' => $show_count,
                            'pad_counts' => $pad_counts,
                            'hierarchical' => $hierarchical,
                            'title_li' => $title,
                            'hide_empty' => $empty
                        );
                        $sub_cats = get_categories($args2);
                        ?> 
                        <?php
                        if ($sub_cats) {
                            $i = 0;
                            foreach ($sub_cats as $sub_category) :
                                ?>
                                            <section id = "cat<?php echo $sub_category->term_id ?>"> <!--cat<?php // echo $i  ?>-<?php // echo $current_page;  ?>-->
                                    <h2 class="crete small-title"><?php echo $sub_category->name ?></h2>

                                    <div class="product-list">

                                        <?php
                                        $ambiance = array();
                                        if (get_field('images_ambiance', 'product_cat_' . $sub_category->term_id)):

                                            while (has_sub_field('images_ambiance', 'product_cat_' . $sub_category->term_id)) :
                                                $image_amb = get_sub_field('img_ab');
                                                $ambiance [] = $image_amb;

                                            endwhile;
                                        endif;
                                        foreach ($ambiance as $key => $value) :
                                            $attachment = wp_get_attachment_image_src($value, 'medium');
                                            $hd = wp_get_attachment_image_src($value, 'full');
                                            ?>  
                                            <article class=" visible row ">
                                                <a href="#"></a>
                                                <figure data-bg="" class="big transition">    
                                                    <div class="abs-full article-bg transition"></div> 
                                                    <a class="circle-btn download-btn hidden-elem" download href="<?php echo $hd[0]; ?>"></a>
                                                    <img class="transition animated fadeIn visible" src=" <?php echo $attachment[0]; ?>" alt="<?php echo $sub_category->name; ?>" title="<?php echo $sub_category->name; ?>" >
                                                </figure>

                                                <h3 class="font-small">Ambiance <?php echo $sub_category->name; ?></h3>
                                            </article>
                                        <?php
                                        endforeach;

                                        $args = array('post_type' => 'product', 'stock' => 1, 'posts_per_page' => -1, 'product_cat' => $sub_category->name, 'orderby' => 'title', 'order' => 'ASC', 'hide_empty' => 0);
                                        $naps_posts = new WP_Query($args);
                                        $options = array();

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


                                            $options['size'] = 'big';
                                            $options['image'] = $args = array(
                                                'post_type' => array('product', 'product_variation'),
                                                'post_status' => array('private', 'publish'),
                                                'numberposts' => -1,
                                                'orderby' => 'menu_order',
                                                'order' => 'asc',
                                                'post_parent' => $id
                                            );


                                            echo nap_get_item($id, $args, $current_page, $options);

                                        endwhile;
                                        wp_reset_query();
                                        ?>
                                    </div> 
                                </section>
                                <?php
                                $i++;
                            endforeach;
                        }
                        ?> 
                        <?php
                    }
                } //END foreach
                ?>
            </div><!-- /product-list -->
        </div><!-- /product-list -->  
    </div>
</div>
<?php
if ($ext_ref) {
    get_footer();
}
?>