<?php
global $post;

get_header();

$globalFields = get_fields('option');
$pageFields = get_fields();

$terms = wp_get_object_terms(get_the_ID(), 'collections');

$parent_term = '';
$child_term = '';
$tax = '';
$slug = '';
foreach ($terms as $term) {
    if ($term->parent == 0) {
        $parent_term = $term->slug;
    } else {
        $child_term = $term->slug;
        $tax = $term->taxonomy;
        $slug = $term->slug;
    }
}

$per_page = 6;
$args = array(
    'post_type' => 'collection-product',
    'posts_per_page' => $per_page,
    'paged' => 1,
    'post__not_in' => array( get_the_ID() )
);
if ($tax && $slug) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => $tax,
            'terms' => $slug,
            'field' => 'slug',
        )
    );
}
$query_related = new WP_Query($args);

$gallery = $pageFields['gallery'];
$custom_title = $pageFields['custom_title'] ? $pageFields['custom_title'] : get_the_title(get_the_ID());
?>

<div class="container">
    <div class="wrapper-single">
        <div class="main-content">
            <?php if (!empty($gallery)) { ?>
                <div class="sliders">
                    <div class="swiper big">
                        <div class="swiper-wrapper">
                            <?php foreach ($gallery as $key => $item) {
                                echo '<div class="swiper-slide"><img src="' . $item['url'] . '" alt="' . $item['alt'] . '"/></div>';
                            } ?>
                        </div>
                    </div>

                    <div class="bottom">
                        <div class="swiper mini">
                            <div class="swiper-wrapper">
                                <?php foreach ($gallery as $key => $item) {
                                    echo '<div class="swiper-slide"><img src="' . $item['url'] . '" alt="' . $item['alt'] . '"/></div>';
                                } ?>
                            </div>
                        </div>
                    </div>
                </div>
            <?php } ?>

            <h1 class="title"><?php echo $custom_title; ?></h1>

            <div class="content-common"><?php the_content(); ?></div>

            <?php
            if ($query_related->have_posts()) {

                echo '<input type="hidden" id="taxSingle" value="' . $tax . '"/>';
                echo '<input type="hidden" id="termSingle" value="' . $slug . '"/>';
                echo '<input type="hidden" id="currentSingle" value="' . get_the_ID() . '"/>';

                echo '<div class="posts-list">';
                echo '<div class="items-list">';
                while ($query_related->have_posts()) {
                    $query_related->the_post();
                    $post_id = get_the_ID();
                    tdg_render_post_and_term_items('post', '', $post_id);
                }
                echo '</div>';
                echo '<span class="loading-scroll">Loading...</span>';
                echo '</div>';

                wp_reset_postdata();
            }
            ?>

        </div>
        <div class="sidebar">
            <?php render_social_single(get_the_ID()); ?>

            <?php if (!empty($globalFields['email'])) { ?>
                <a class="request" href="mailto:<?php echo $globalFields['email']; ?>">
                    <span><?php echo esc_html('Request more info'); ?></span>
                </a>
            <?php } ?>

            <a class="request trade" href="<?php echo home_url('/trade-register/?product=' . $child_term . '/' . $post->post_name); ?>">
                <span><?php echo esc_html('Registered as Trade'); ?></span>
            </a>

            <!-- action down load file -->
            <?php 
                echo do_shortcode( '[file_download_popup]' ); 
            ?>

        </div>
    </div>
</div>

<?php
get_footer();