<?php
get_header();

$globalFields = get_fields('option');

$pageList = $globalFields['page_list_architect_stone'];

$object = get_queried_object();

$terms = get_terms(array(
    'taxonomy' => $object->taxonomy,
    'hide_empty' => false,
    'parent' => 0
));
$gallery_tax = get_field('gallery_tax', $object->taxonomy . '_' . $object->term_id);
$title = get_field('custom_title', $object->taxonomy . '_' . $object->term_id);
$content = get_field('description', $object->taxonomy . '_' . $object->term_id);
?>
<div class="container">
    <div class="wrapper-single">
        <div class="sidebar">
            <?php
            if (!empty($pageList)) {
                echo '<div class="list-category sssss">';
                foreach ($pageList as $key => $term) {
                    $child_terms = get_terms(array(
                        'taxonomy' => $term->taxonomy,
                        'parent' => $term->term_id,
                        'hide_empty' => false,
                    ));
                    
                    $active_toggle = !empty($child_terms) ? 'toggle' : '';

                    echo '<div class="item">';
                    echo '<h4 class="' . $active_toggle . '">';
                    echo '<a href="' . get_term_link($term->term_id) . '">' . $term->name . '</a>';
                    echo !empty($child_terms) ? '<i class="ti-arrow-circle-right"></i>' : '';
                    echo '</h4>';
                    if (!empty($child_terms)) {
                        echo '<ul class="list-terms">';
                        foreach ($child_terms as $key => $c_term) {
                            echo '<li><a href="' . get_term_link($c_term->term_id) . '">' . $c_term->name . '</a></li>';
                        }
                        echo '</ul>';
                    }
                    echo '</div>';
                }
                echo '</div>';
            }
            ?>
        </div>

        <div class="main-content">
            <?php if (!empty($gallery_tax)) { ?>
                <div class="sliders">
                    <div class="swiper big">
                        <div class="swiper-wrapper">
                            <?php foreach ($gallery_tax 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_tax 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 !empty($title) ? $title : $object->name; ?></h1>

            <?php
            $per_page = 6;
            echo '<div class="posts-list">';
            if ($object->parent != 0) {

                $param = array(
                    'post_type' => 'architect-stones',
                    'posts_per_page' => $per_page,
                    'paged' => 1,
                    'tax_query' => array(
                        array(
                            'taxonomy' => $object->taxonomy,
                            'terms' => array($object->slug),
                            'field' => 'slug',
                        )
                    ),
                );
                
                $query = new WP_Query( $param );
              
                if ($query->have_posts()) {
                    echo '<div class="items-list">';
                    while ($query->have_posts()) {
                        $query->the_post();
                        $post_id = get_the_ID();
                        tdg_render_post_and_term_items('post', '', $post_id);
                    }
                    echo '</div>';
                    wp_reset_postdata();

                    if ($query->max_num_pages > 1) {
                        echo '<div class="wrap-btn">
                            <button id="load_more_post" 
                            data-post-type="architect-stones"
                            data-taxonomy="' . $object->taxonomy . '" 
                            data-term="' . $object->slug . '" 
                            data-total="' . $query->max_num_pages . '" 
                            data-per-page="' . $per_page . '">Load more</button>
                        </div>';
                    }

                }

            } else {
                $child_terms_query = get_terms(array(
                    'taxonomy' => $object->taxonomy,
                    'parent' => $object->term_id,
                    'hide_empty' => false,
                    'number' => $per_page,
                    'offset' => 0,
                ));
                $count_terms = wp_count_terms(array(
                    'taxonomy' => $object->taxonomy,
                    'parent' => $object->term_id,
                    'hide_empty' => false,
                ));

                if (!empty($child_terms_query)) {
                    echo '<div class="items-list">';
                    foreach ($child_terms_query as $key => $child) {
                        tdg_render_post_and_term_items('term', $child->taxonomy, $child->term_id);
                    }
                    echo '</div>';
                }

                if ($count_terms > $per_page) {
                    echo '<div class="wrap-btn"><button id="load_more_term" data-taxonomy="' . $object->taxonomy . '" data-parent-id="' . $object->term_id . '" data-total="' . $count_terms . '" data-per-page="' . $per_page . '">Load more</button></div>';
                }

            }
            echo '</div>';

            if (!empty($content)) {
                echo '<div class="content-common">' . apply_filters('the_content', $content) . '</div>';
            }

            echo '<div class="bottom-form">';
            $title_form = 'CONTACT US TO GET MORE INFORMATION ON ' . $object->name;
            echo '<h3 class="title-form">' . $title_form . '</h3>';
            echo do_shortcode('[gravityform id="3" title="false" description="false" ajax="true"]');
            echo '</div>';
            ?>

        </div>

    </div>
</div>

<?php
get_footer();