<?php
/*
 * Element tz Team Grid
 * */

function tzinteriart_view_team($atts)
{
    $tz_category = $tz_limit = $tz_orderby = $tz_order = $tz_col_desktop = $tz_col_tabletportrait = '';
    $tz_col_mobilelandscape = $tz_col_mobile = $tz_css_animation = $tz_slide_auto = $tz_nav_option = '';
    $tz_loop_option = $tz_margin = $css = '';
    extract(shortcode_atts(array(
        'tz_category' => '',
        'tz_limit' => 10,
        'tz_orderby' => 'date',
        'tz_order' => 'desc',
        'tz_col_desktop' => 4,
        'tz_col_tabletportrait' => 3,
        'tz_col_mobilelandscape' => 2,
        'tz_col_mobile' => 1,
        'tz_css_animation' => '',
        'tz_slide_auto' => '',
        'tz_nav_option' => '',
        'tz_loop_option' => '',
        'tz_margin' => '',
        'css' => '',
    ), $atts));
    ob_start();


    wp_enqueue_style('owl.carousel.min');
    wp_enqueue_script('owl.carousel.min');

    $tzinteriart_class = '';
    $tzinteriart_class_style = '';


    if ($tz_css_animation != '') {
        wp_enqueue_script('waypoints');
        $tzinteriart_class .= ' wpb_animate_when_almost_visible wpb_' . $tz_css_animation;
    }

    $viewteam_classes = '';
    if (isset($css) && $css != ''):
        $viewteam_classes .= ' ' . vc_shortcode_custom_css_class($css);
    endif;

    ?>
    <div class="tzElement_Team <?php echo esc_attr($viewteam_classes); ?>">
        <div class="tzTeam_slide">
            <?php
            if ($tz_category != '') {
                $cat_id = explode(",", $tz_category);
                $tzcat = array();

                if (is_array($cat_id)) {
                    sort($cat_id);
                    $count_cat = count($cat_id);

                    for ($i = 0; $i < $count_cat; $i++) {
                        $tzcat[] = (int)$cat_id[$i];
                    }
                } else {
                    $tzcat[] = (int)$cat_id;
                }

                $args = array(
                    'post_type' => 'team',
                    'posts_per_page' => $tz_limit,
                    'orderby' => $tz_orderby,
                    'order' => $tz_order,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'team-category',
                            'filed' => 'id',
                            'terms' => $tzcat,
                        )
                    )
                );
            } else {
                $args = array(
                    'post_type' => 'team',
                    'posts_per_page' => $tz_limit,
                    'orderby' => $tz_orderby,
                    'order' => $tz_order,
                );
            }

            $psl_query = new WP_Query($args);
            if ($psl_query->have_posts()):
                while ($psl_query->have_posts()): $psl_query->the_post();
                    $post_thumbnail_id = get_post_thumbnail_id($psl_query->post->ID);
                    $image_src = wp_get_attachment_image_src($post_thumbnail_id, 'feature-image'); // get info image
                    $interiart_team_position = get_post_meta(get_the_ID(), 'interiart_team_position', true);


                    ?>
                    <div class="tzTeam_slide_item">
                        <div class="tzTeamSlide_image">
                            <?php the_post_thumbnail('large'); ?>
                        </div>

                        <div class="tzTeam_hover">
                            <div class="tzTeam_hover_info">
                                <h3><a class="simple-ajax-popup-align-top-title" href="<?php the_permalink(); ?>"
                                       data-effect="mfp-zoom-in"><?php the_title(); ?></a></h3>
                                <span class="tzcat"><?php the_terms($psl_query->post->ID, 'Team-category', '', ','); ?></span>
                                <p class="tz_position">
                                    <?php
                                    echo esc_html($interiart_team_position);
                                    ?>
                                </p>
                            </div>

                        </div>

                    </div>
                <?php
                endwhile;
                wp_reset_postdata();
            endif;
            ?>
        </div>
        <script type="text/javascript">
            jQuery(document).ready(function(){
                jQuery('.tzTeam_slide').each(function(){
                    jQuery(this).interiart_owlCarousel({
                        loop:<?php if ($tz_loop_option == true) {echo 'true';} else {echo 'false';}?>,
                        margin:<?php if ($tz_margin != '') {echo esc_attr($tz_margin);} else {echo '30';}?>,
                        navText:["<i class='flaticon-left-arrow'></i>","<i class='flaticon-right-arrow'></i>"],
                        nav:<?php if ($tz_nav_option == true) {echo 'true';} else {echo 'false';}?>,
                        responsiveClass:true,
                        autoplay:<?php if ($tz_slide_auto == true) {echo 'true';} else {echo 'false';}?>,
                        autoplaySpeed: 800,
                        autoplayHoverPause: true,
                        animateOut: 'fadeOut',
                        responsive:{
                            0:{
                                items:<?php if ($tz_col_mobile != '') {echo esc_attr($tz_col_mobile);} else {echo '1';}?>,
                                nav: false,
                            },
                            480:{
                                items:<?php if ($tz_col_mobilelandscape != '') {echo esc_attr($tz_col_mobilelandscape);} else {echo '2';}?>,

                            },
                            992:{
                                items:<?php if ($tz_col_tabletportrait != '') {echo esc_attr($tz_col_tabletportrait);} else {echo '3';}?>,

                            },
                            1200:{
                                items:<?php if ($tz_col_desktop != '') {echo esc_attr($tz_col_desktop);} else {echo '4';}?>,
                            }
                        }

                    })

                })
            })

        </script>
    </div>
    <?php
    return ob_get_clean();
}

add_shortcode('tz-view-team', 'tzinteriart_view_team');
?>