<?php
    $html_id = pxl_get_element_id($settings);
    $tax = ['category'];
    $select_post_by = $widget->get_setting('select_post_by', '');

    $post_ids = ($select_post_by === 'post_selected') ? $widget->get_setting('source_'.$settings['post_type'].'_post_ids', '') : [];
    $source = ($select_post_by !== 'post_selected') ? $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);
    extract(pxl_get_posts_of_grid(
        'post', 
        [
            'source' => $source, 
            'orderby' => $orderby, 
            'order' => $order, 
            'limit' => $limit, 
            'post_ids' => $post_ids
        ],
        $tax
    ));
    $filter_default_title = $widget->get_setting('filter_default_title', 'All');

    if($settings['col_xl'] == '5') {
        $col_xl = 'pxl5';
    } else {
        $col_xl = 12 / intval($widget->get_setting('col_xl', 4));
    }
    $col_lg = 12 / intval($widget->get_setting('col_lg', 4));
    $col_md = 12 / intval($widget->get_setting('col_md', 3));
    $col_sm = 12 / intval($widget->get_setting('col_sm', 2));
    $col_xs = 12 / intval($widget->get_setting('col_xs', 1));
    $grid_sizer = "col-xl-{$col_xl} col-lg-{$col_lg} col-md-{$col_md} col-sm-{$col_sm} col-{$col_xs}";

    $grid_class = 'pxl-grid-inner pxl-grid-masonry row';

    $filter = $widget->get_setting('filter', 'false');
    $filter_type = $widget->get_setting('filter_type', 'normal');
    $filter_alignment = $widget->get_setting('filter_alignment', 'center');
    $pagination_type = $widget->get_setting('pagination_type', 'pagination');

    $post_type = $widget->get_setting('post_type','post');
    $layout = $widget->get_setting('layout_'.$post_type, 'post-1');

    // Animation
    $animate = $widget->get_setting('pxl_animate');
    $animate_delay = $widget->get_setting('pxl_animate_delay', 0);


    // Display
    $show_date = !empty($settings['show_date']) || false;
    $show_category = !empty($settings['show_category']) || false;
    $show_button = !empty($settings['show_button']) || false;
    $button_text = !empty($settings['button_text']) ? $settings['button_text'] : 'Read Article';
    $img_size = !empty($settings['img_size']) ? $settings['img_size'] : '767x575';

    $load_more = array(
        'tax'             => $tax,
        'post_type'       => $post_type,   
        'layout'          => $layout,
        'startPage'       => $paged,
        'maxPages'        => $max,
        'total'           => $total,
        'filter'          => $filter,
        'filter_type'     => $filter_type,
        'perpage'         => $limit,
        'nextLink'        => $next_link,
        'source'          => $source,
        'orderby'         => $orderby,
        'order'           => $order,
        'limit'           => $limit,
        'post_ids'        => $post_ids,
        'col_xl'          => $col_xl,
        'col_lg'          => $col_lg,
        'col_md'          => $col_md,
        'col_sm'          => $col_sm,
        'col_xs'          => $col_xs,
        'pagination_type' => $pagination_type,
        'show_date'       => $show_date,
        'show_category'    => $show_category,
        'show_button'     => $show_button,
        'button_text'     => $button_text,
        'img_size'        => $img_size,
        'animate'     => $animate,
        'animate_delay' => (int)$animate_delay,
        'title_hover_style' => $settings['title_hover_style'],
    );

    $filter_grid_args = [
        'select_post_by' => $select_post_by,
        'filter' => $filter,
        'filter_type' => $filter_type,
        'filter_default_title' => $filter_default_title,
        'grid_class' => $grid_class,
        'load_more' => $load_more,
        'grid_sizer' => $grid_sizer,
        'pagination_type' => $pagination_type,
        'posts' => $posts,
        'query' => $query,
    ];


    $wrap_attrs = [
        'id'               => $html_id,
        'class'            => trim('pxl-grid pxl-post-grid pxl-news-grid pxl-news-grid1'),
        'data-start-page'  => $paged,
        'data-max-pages'   => $max,
        'data-total'       => $total,
        'data-perpage'     => $limit,
        'data-next-link'   => $next_link
    ];

    if ($pagination_type != 'false'){
        $wrap_attrs['data-loadmore'] = json_encode($load_more);
    }
    $widget->add_render_attribute( 'wrapper', $wrap_attrs );
?>
<?php if(count($posts) <= 0) : ?>
    <div class="pxl-no-post-found">
        <?php echo esc_html__( 'No Post Found', 'solarva' ); ?>
    </div>
<?php endif; ?>

<div <?php pxl_print_html($widget->get_render_attribute_string( 'wrapper' )) ?>>
    <?php extract($filter_grid_args);
    if ($select_post_by == 'term_selected' && $filter == "true"): ?>
        <div class="pxl-grid-filter <?php echo esc_attr($filter_type) ?>">
            <div class="pxl-filter-inner">
                <span class="filter-item active" data-filter="*"><?php echo esc_html($filter_default_title); ?></span>
                <?php foreach ($categories as $category): ?>
                    <?php 
                        $category_arr = explode('|', $category); 
                        $term = get_term_by('slug',$category_arr[0], $category_arr[1]); 
                    ?>
                    <span class="filter-item" data-filter="<?php echo esc_attr('.' . $term->slug); ?>">
                        <?php echo esc_html($term->name); ?>
                    </span>
                <?php endforeach; ?>
            </div>
        </div>
    <?php endif; ?>

    <div class="<?php echo esc_attr($grid_class); ?>" data-gutter="15">
        <?php solarva_get_post_grid($posts, $load_more); ?>
        <div class="grid-sizer <?php echo esc_attr($grid_sizer); ?>"></div>
    </div>
    <?php if ($pagination_type == 'pagination') : ?>
        <div class="pxl-grid-pagination">
            <?php solarva()->page->get_pagination($query, true); ?>
        </div>
    <?php endif; ?>
    <?php if (!empty($next_link) && $pagination_type == 'loadmore') : ?>
        <div class="pxl-load-more">
            <span class="btn btn-grid-loadmore">
                <span class="pxl-loadmore-text"><?php echo esc_html__('Load More', 'solarva') ?></span>
                <span class="pxl-load-icon"></span>
            </span>
        </div>
    <?php endif; ?>
</div>