<?php
extract($settings);
$html_id = pxl_get_element_id($settings);
$tax = ['portfolio-category'];
$select_post_by = $widget->get_setting('select_post_by', '');
$source = $post_ids = [];
if($select_post_by === 'post_selected'){
    $post_ids = $widget->get_setting('source_'.$settings['post_type'].'_post_ids', '');
}else{
    $source  = $widget->get_setting('source_'.$settings['post_type'], '');
}
$orderby = $widget->get_setting('orderby', 'date');
$order = $widget->get_setting('order', 'desc');
$limit = $widget->get_setting('limit', 6);
$settings['layout']    = $settings['layout_'.$settings['post_type']];
extract(pxl_get_posts_of_grid('portfolio', [
    'source' => $source,
    'orderby' => $orderby,
    'order' => $order,
    'limit' => $limit,
    'post_ids' => $post_ids,
    'tax'=> $tax,
]));

$pxl_animate = $widget->get_setting('pxl_animate', '');
$col_xs = $widget->get_setting('col_xs', '');
$col_sm = $widget->get_setting('col_sm', '');
$col_md = (int)$widget->get_setting('col_md', '');
if($col_md == 'custom') {
    $col_md = $widget->get_setting('col_md_custom', '');
}
$col_lg = (int)$widget->get_setting('col_lg', '');
if($col_lg == 'custom') {
    $col_lg = $widget->get_setting('col_lg_custom', '');
}
$col_xl = (int)$widget->get_setting('col_xl', '');
if($col_xl == 'custom') {
    $col_xl = $widget->get_setting('col_xl_custom', '');
}
$col_xxl = (int)$widget->get_setting('col_xxl', '');
if($col_xxl == 'custom') {
    $col_xxl = $widget->get_setting('col_xxl_custom', '');
}
$slides_to_scroll = $widget->get_setting('slides_to_scroll', '');

$arrows = $widget->get_setting('arrows', false);
$pagination = $widget->get_setting('pagination', false);
$pagination_type = $widget->get_setting('pagination_type', 'bullets');
$pause_on_hover = $widget->get_setting('pause_on_hover', false);
$autoplay = $widget->get_setting('autoplay', false);
$autoplay_speed = $widget->get_setting('autoplay_speed', '5000');
$infinite = $widget->get_setting('infinite', false);
$speed = $widget->get_setting('speed', '500');
$center = $widget->get_setting('center', false);
$drap = $widget->get_setting('drap', false);

$img_size = $widget->get_setting('img_size');
$show_excerpt = $widget->get_setting('show_excerpt');
$num_words = $widget->get_setting('num_words');
$show_button = $widget->get_setting('show_button');
$show_info = $widget->get_setting('show_info');
$show_category = $widget->get_setting('show_category');
$button_text = $widget->get_setting('button_text');

$opts = [
    'slide_direction'               => 'horizontal',
    'slide_percolumn'               => 1, 
    'slide_percolumnfill'           => 1, 
    'slide_mode'                    => 'slide', 
    'slides_to_show'                => $col_xl, 
    'slides_to_show_xxl'            => $col_xxl, 
    'slides_to_show_lg'             => $col_lg, 
    'slides_to_show_md'             => $col_md, 
    'slides_to_show_sm'             => (int)$col_sm, 
    'slides_to_show_xs'             => (int)$col_xs, 
    'slides_to_scroll'              => (int)$slides_to_scroll,  
    'slides_gutter'                 => 30, 
    'arrow'                         => (bool)$arrows,
    'pagination'                    => (bool)$pagination,
    'pagination_type'               => $pagination_type,
    'autoplay'                      => (bool)$autoplay,
    'pause_on_hover'                => (bool)$pause_on_hover,
    'pause_on_interaction'          => true,
    'delay'                         => (int)$autoplay_speed,
    'loop'                          => (bool)$infinite,
    'speed'                         => (int)$speed,
    'center'                        => (bool)$center,
];

$widget->add_render_attribute( 'carousel', [
    'class'         => 'pxl-swiper-container',
    'dir'           => is_rtl() ? 'rtl' : 'ltr',
    'data-settings' => wp_json_encode($opts)
]);

if ( ! empty( $settings['link']['url'] ) ) {
    $widget->add_render_attribute( 'link_btn', 'href', $settings['link']['url'] );

    if ( $settings['link']['is_external'] ) {
        $widget->add_render_attribute( 'link_btn', 'target', '_blank' );
    }

    if ( $settings['link']['nofollow'] ) {
        $widget->add_render_attribute( 'link_btn', 'rel', 'nofollow' );
    }
} 
?>

<?php if (is_array($posts)): ?>
    <div class="pxl-swiper-slider pxl-portfolio-carousel pxl-portfolio-carousel5" <?php if($drap !== false): ?>data-cursor-drap="<?php echo esc_html('DRAG', 'hubsteel'); ?>"<?php endif; ?>>
        <div class="pxl-carousel-inner">
            <div <?php pxl_print_html($widget->get_render_attribute_string( 'carousel' )); ?>>
                <div class="pxl-swiper-wrapper">
                    <?php
                    foreach ($posts as $post):
                        $image_size = !empty($img_size) ? $img_size : '870x614';
                        $location = get_post_meta($post->ID, 'location', true);
                        $date_finish = get_post_meta($post->ID, 'date_finish', true);
                        $materials = get_post_meta($post->ID, 'materials', true);
                        $portfolio_excerpt = get_post_meta($post->ID, 'portfolio_excerpt', true);
                        $img_id       = get_post_thumbnail_id( $post->ID );
                        $img          = pxl_get_image_by_size( array(
                            'attach_id'  => $img_id,
                            'thumb_size' => $image_size
                        ) );
                        $thumbnail    = $img['thumbnail']; 
                        $filter_class = '';
                        if ($select_post_by === 'term_selected' )
                            $filter_class = pxl_get_term_of_post_to_class($post->ID, array_unique($tax));
                        ?>
                        <div class="pxl-swiper-slide" data-filter="<?php echo esc_attr($filter_class); ?>">
                            <div class="pxl-post--inner">
                                <div class="pxl-content-meta">
                                    <div class="pxl-item--image">
                                        <?php echo wp_kses_post($thumbnail); ?>
                                    </div>
                                    <div class="pxl-post--holder">
                                        <div class="pxl-meta-top">
                                            <?php if($show_category == 'true'): ?>
                                                <div class="pxl-post--category">
                                                    <?php the_terms( $post->ID, 'portfolio-category', '', ' ' ); ?>
                                                </div>
                                            <?php endif; ?>
                                            <h5 class="pxl-post--title">
                                                <a href="<?php echo esc_url(get_permalink( $post->ID )); ?>"><?php echo pxl_print_html(get_the_title($post->ID)); ?></a>
                                            </h5>
                                            <?php if($show_excerpt == 'true' && !empty($portfolio_excerpt)): ?>
                                                <div class="pxl-post--content">
                                                    <?php echo wp_trim_words( $portfolio_excerpt, $num_words, $more = null ); ?>
                                                </div>
                                            <?php endif; ?>
                                        </div>
                                        <div class="pxl-content-list <?php if ($show_info !='true'){ echo esc_attr('opacity-0');} ?>">
                                            <div class="pxl-item-list date">
                                                <span class="pxl-item-name"><?php echo esc_html__('Completion Date: ','hubsteel'); ?></span>
                                                <?php echo pxl_print_html($date_finish); ?>
                                            </div>
                                            <div class="pxl-item-list location">
                                                <span class="pxl-item-name"><?php echo esc_html__('Location: ','hubsteel'); ?></span>
                                                <?php echo pxl_print_html($location); ?>
                                            </div>
                                            <div class="pxl-item-list materials">
                                                <span class="pxl-item-name"><?php echo esc_html__('Materials: ','hubsteel'); ?></span>
                                                <?php echo pxl_print_html($materials); ?>
                                            </div>
                                        </div>
                                        <?php if($show_button == 'true') : ?>
                                            <div class="pxl-btn-portfolio">
                                                <a class="btn pxl-icon-active  btn-outline  pxl-icon--right" href="<?php echo esc_url(get_permalink( $post->ID )); ?>">
                                                    <span class="pxl-btn-icon pxl-ml-6">
                                                        <svg xmlns="http://www.w3.org/2000/svg" fill="#000" height="512" viewBox="0 0 24 24" width="512"><path d="m16.0039 9.414-8.60699 8.607-1.414-1.414 8.60599-8.607h-7.58499v-2h10.99999v11h-2z" fill="rgb(0,0,0)"></path></svg>
                                                    </span>
                                                    <span class="pxl--btn-text"><?php if(!empty($button_text)) {
                                                        echo esc_attr($button_text);
                                                    } else {
                                                        echo esc_html__('Read more', 'hubsteel');
                                                    } ?></span>
                                                </a>
                                            </div>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div> 
            </div>
            <?php if($arrows !== false): ?>
                <div class="pxl-swiper-arrow-wrap style-5">
                    <div class="pxl-swiper-arrow pxl-swiper-arrow-next">
                        <div class="hover">
                            <svg id="Layer_1" enable-background="new 0 0 20 20" height="512" viewBox="0 0 20 20" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m12 2-1.4 1.4 5.6 5.6h-16.2v2h16.2l-5.6 5.6 1.4 1.4 8-8z" fill="rgb(0,0,0)"/></svg>
                        </div>
                    </div>
                    <div class="pxl-swiper-arrow pxl-swiper-arrow-prev">
                        <div class="hover">
                            <svg id="Layer_1" enable-background="new 0 0 20 20" height="512" viewBox="0 0 20 20" width="512" xmlns="http://www.w3.org/2000/svg"><path d="m12 2-1.4 1.4 5.6 5.6h-16.2v2h16.2l-5.6 5.6 1.4 1.4 8-8z" fill="rgb(0,0,0)"/></svg>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            <?php if($pagination !== false): ?>
                <div class="pxl-swiper-dots-wrap">
                    <div class="pxl-swiper-dots style-1"></div>
                </div>
            <?php endif; ?>
        </div>
    </div>
<?php endif; ?>