<?php 

    namespace Pixebuilder\Element;

    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Column{
        static function render($settings){

            $width_type = empty($settings['width'])? '': pixe_get_unit($settings['width']);
            $extra_class = empty($settings['class'])? '': ' ' . trim($settings['class']);
            $extra_class .= pixe_get_responsive_class($settings);
            $extra_class .= pixe_animation_class($settings);

            if( !empty($settings['no-content']) ){
                $extra_class .= ' pixe-column-no-content';

                if( !empty($settings['has-bg']) ){
                    $extra_class .= ' pixe-has-bg';
                }
            }

            // extend backaground
            if( !empty($settings['extend-background']) ){
                $extra_class .= ' pixe-bgext-' . $settings['extend-background'];
            }
            if( !empty($settings['tablet-extend-background']) ){
                $extra_class .= ' pixe-tbgext-' . $settings['tablet-extend-background'];
            }
            if( !empty($settings['mobile-extend-background']) ){
                $extra_class .= ' pixe-mbgext-' . $settings['mobile-extend-background'];
            }

            // query subtype    
            if( !empty($settings['_subtype']) && $settings['_subtype'] == 'query' ){
                global $pixe_query, $pixe_terms;

                // for term query
                if( !empty($pixe_terms) ){
                    if( !current($pixe_terms) ){
                        return '';
                    }

                // for posts
                }else if( !empty($pixe_query) ){
                    if( pixe_have_posts($pixe_query) ){
                        $pixe_query->the_post();

                        if( get_post_type() == 'product' ){
                            global $post;
                            wc_setup_product_data($post);
                        }
                    }else{
                        return '';
                    }
                }
            }

            // background filter
            if( !empty($settings['bg-style']) ){
                pixe_set_background_color_filter($settings['bg-style']);
            }

            $ret  = '<div class="pixe-column' . esc_attr($extra_class) . '" ';
            $ret .= empty($settings['id'])? '': 'id="' . esc_attr($settings['id']) . '" ';
            $ret .= pixe_get_data_atts(array(
                'id' => empty($settings['_id'])? '': $settings['_id'],
                'type' => empty($settings['_subtype'])? '': $settings['_subtype'],
                'width' => ($width_type == 'col')? str_replace('/', '-', $settings['width']): '',
                'anim-location' => pixe_animation_atts($settings),
                'scroll-motion' => pixe_scroll_motion_atts($settings),
            ));
            $ret .= ' >';
            $ret .= '<div class="pixe-column-content" >'; 
            $ret .= pixe_get_shape_divider($settings);
            $ret .= pixe_get_background_media($settings, !empty($settings['bg-style-display']));
            $ret .= empty($settings['_elements'])? '': pixe_get_builder_content($settings['_elements']);
            $ret .= '</div>'; // pixe-column-content

            if( !empty($settings['column-link']) ){
                $link_tag = pixe_get_link_tag($settings['column-link'], array(
                    'class' => 'pixe-column-link',
                    'default-link-to' => 'custom'
                ));

                $ret .= empty($link_tag)? '': $link_tag . '</a>';
            }
            $ret .= '</div>'; // pixe-column

            // set next pointer to the term query
            if( !empty($settings['_subtype']) && $settings['_subtype'] == 'query' ){
                if( !empty($pixe_terms) ){
                    next($pixe_terms);
                }
            }

            return $ret;
        }
    }
