<?php
/**
 * Article Banner Slider Widget Style
 *
 * @package Vintuna
 */
if (!function_exists('vintuna_post_banner_slider_widget')) :
    /**
     * Load widgets.
     *
     * @since 1.0.0
     */
    function vintuna_post_banner_slider_widget()
    {
        register_widget('Vintuna_Post_Banner_Slider_Widget');
    }
endif;
add_action('widgets_init', 'vintuna_post_banner_slider_widget');
// Article Widget Carousel Layout
if (!class_exists('Vintuna_Post_Banner_Slider_Widget')) :
    /**
     * Article Widget Carousel Layout
     *
     * @since 1.0.0
     */
    class Vintuna_Post_Banner_Slider_Widget extends Vintuna_Widget_Base
    {
        /**
         * Sets up a new widget instance.
         *
         * @since 1.0.0
         */
        function __construct()
        {
            $opts = array(
                'classname' => 'vintuna_post_banner_slider_widget',
                'description' => esc_html__('Displays post form selected category in banner slider styles', 'vintuna'),
                'customize_selective_refresh' => true,
            );
            $fields = array(
                'title' => array(
                    'label' => esc_html__('Section Title:', 'vintuna'),
                    'type' => 'text',
                    'class' => 'widefat',
                ),
                'post_category' => array(
                    'label' => esc_html__('Select Category:', 'vintuna'),
                    'type' => 'dropdown-taxonomies',
                    'show_option_all' => esc_html__('All Categories', 'vintuna'),
                ),
                'display_orientation' => array(
                    'label' => esc_html__('Display Layout:', 'vintuna'),
                    'type' => 'select',
                    'default' => 'layout-1',
                    'options' => array(
                        'news-article-default' => esc_html__('Layout - 1', 'vintuna'),
                        'news-article-tiles entry-image-overlay image-overlay-gradient' => esc_html__('Layout - 2', 'vintuna'),
                    ),
                ),
                'content_alignment' => array(
                    'label' => esc_html__('Text Alignment:', 'vintuna'),
                    'type' => 'select',
                    'default' => 'text-center',
                    'options' => array(
                        'content-text-left' => esc_html__('Align Left', 'vintuna'),
                        'content-text-center' => esc_html__('Align Center', 'vintuna'),
                        'content-text-right' => esc_html__('Align Right', 'vintuna'),
                    ),
                ),
                'image_size' => array(
                    'label' => esc_html__('Image Size:', 'vintuna'),
                    'type' => 'select',
                    'default' => 'entry-image-xlarge',
                    'options' => array(
                        'entry-image-xlarge' => esc_html__('Extra Large', 'vintuna'),
                        'entry-image-large' => esc_html__('Large', 'vintuna'),
                        'entry-image-big' => esc_html__('Big', 'vintuna'),
                    ),
                ),
                'post_number' => array(
                    'label' => esc_html__('Number of posts to show:', 'vintuna'),
                    'type' => 'number',
                    'default' => 5,
                    'css' => 'max-width:60px;',
                    'min' => 1,
                    'max' => 10,
                ),
                'post_offset' => array(
                    'label' => esc_html__('Posts offset:', 'vintuna'),
                    'type' => 'number',
                    'default' => 0,
                    'css' => 'max-width:60px;',
                    'min' => 0,
                    'max' => 12,
                ),
                'enable_meta' => array(
                    'label' => esc_html__('Enable Categories:', 'vintuna'),
                    'type' => 'checkbox',
                    'default' => true,
                ),
                'enable_meta_1' => array(
                    'label' => esc_html__('Enable Date & Author:', 'vintuna'),
                    'type' => 'checkbox',
                    'default' => true,
                ),
                'button_text' => array(
                    'label' => esc_html__('Button Text:', 'vintuna'),
                    'type' => 'text',
                    'default' => 'View more',
                    'class' => 'widefat',
                ),
            );
            parent::__construct('vintuna-widget-banner-slider-style', esc_html__('Vintuna: Slider Widget', 'vintuna'), $opts, array(), $fields);
        }

        /**
         * Outputs the content for the current widget instance.
         *
         * @param array $args Display arguments.
         * @param array $instance Settings for the current widget instance.
         * @since 1.0.0
         *
         */
        function widget($args, $instance)
        {
            $params = $this->get_params($instance);
            echo $args['before_widget'];
            $post_number = $params['post_number'];
            $post_offset = $params['post_offset'];
            $qargs = array(
                'post_type' => 'post',
                'posts_per_page' => $post_number,
                'offset'  => $post_offset,
                'post__not_in' => get_option("sticky_posts"),
            );
            $cat_link = "";

            if( is_rtl() ) {
                $rtl = 'true';
            }else{
                $rtl = 'false';
            }

            if (absint($params['post_category']) > 0) {
                $qargs['cat'] = absint($params['post_category']);
                $cat_link = get_category_link($params['post_category']);
            }
            $slider_posts_query = new WP_Query($qargs);
            if ($slider_posts_query->have_posts()) : ?>
                <div class="widget-layout widget-layout-slider">
                    <header class="theme-widget-header">
                        <?php if (!empty($params['title'])) { ?>
                            <h2 class="widget-title">
                                <?php echo $params['title'] ?>
                            </h2>
                        <?php } ?>
                        <span class="block-title-dash"></span>
                    </header>
                    <div class="theme-widget-content">
                        <div class="widget-slider-panel">
                            <div class="theme-widget-slider" data-slick='{"rtl": <?php echo esc_attr($rtl); ?>}'>
                                <?php
                                while ($slider_posts_query->have_posts()) :
                                    $slider_posts_query->the_post();
                                    ?>
                                    <div class="slick-slide-widget">
                                        <article id="theme-post-<?php the_ID(); ?>" <?php 
                                        post_class(
                                            'news-article ' .
                                            esc_attr( $params['display_orientation'] ) . ' ' .
                                            esc_attr( $params['content_alignment'] )
                                        ); ?>>


                                            <?php
                                            $fallback_image_url = vintuna_get_fallback_image_url();
                                            if (has_post_thumbnail() || $fallback_image_url) : ?>
                                                <div class="entry-image <?php echo esc_attr( $params['image_size'] ); ?>">
                                                    <a class="post-thumbnail" href="<?php the_permalink(); ?>"
                                                       aria-hidden="true" tabindex="-1">
                                                        <?php
                                                        if (has_post_thumbnail()) {
                                                            the_post_thumbnail('large', [
                                                                'alt' => the_title_attribute(['echo' => false]),
                                                            ]);
                                                        } else {
                                                            echo '<img src="' . esc_url($fallback_image_url) . '" alt="' . esc_attr(get_the_title()) . '" />';
                                                        }
                                                        ?>
                                                    </a>
                                                </div>
                                            <?php endif; ?>

                                            <div class="article-content">
                                                <div class="entry-meta">
                                                    <?php vintuna_entry_footer($cats = true, $tags = false, $edits = false, $text = false, $icon = false); ?>
                                                </div>
                                                <h3 class="entry-title entry-title-large">
                                                    <a href="<?php the_permalink(); ?>" tabindex="0" rel="bookmark" title="<?php the_title_attribute(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h3>
                                                <div class="entry-meta">
                                                    <?php vintuna_posted_by(); ?>
                                                </div>

                                                <div class="entry-content hidden-xs-element entry-content-muted">
                                                    <?php
                                                    if (has_excerpt()) {
                                                        the_excerpt();
                                                    } else {
                                                        echo '<p>';
                                                        echo esc_html(wp_trim_words(get_the_content(), 25, '...'));
                                                        echo '</p>';
                                                    } ?>
                                                </div>
                                            </div>

                                        </article>
                                    </div>
                                    <?php
                                    wp_reset_postdata();
                                endwhile; ?>
                            </div>
                        </div>
                    </div>
                    <?php if (!empty($params['button_text']) && !empty($cat_link)) { ?>
                        <div class="theme-footer-link theme-widget-footer">
                            <div class="theme-footer-panel widget-footer-panel">
                                <hr>
                                <a class="theme-btn theme-primary-btn title-view-more theme-viewmore-link" href="<?php echo esc_url($cat_link); ?>"><?php echo $params['button_text']; ?><?php vintuna_theme_svg('arrow-right'); ?></a>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            <?php endif;
            echo $args['after_widget'];
        }
    }
endif;