<?php

/**
 * Article Carousel Widget Style
 *
 * @package Vintuna
 */
if (!function_exists('vintuna_post_carousel_widget')) :
    /**
     * Load widgets.
     *
     * @since 1.0.0
     */
    function vintuna_post_carousel_widget()
    {
        register_widget('Vintuna_Post_Carousel_Widget');
    }
endif;
add_action('widgets_init', 'vintuna_post_carousel_widget');
// Article Widget Carousel Layout
if (!class_exists('Vintuna_Post_Carousel_Widget')) :
    /**
     * Article Widget Carousel Layout
     *
     * @since 1.0.0
     */
    class Vintuna_Post_Carousel_Widget extends Vintuna_Widget_Base
    {
        /**
         * Sets up a new widget instance.
         *
         * @since 1.0.0
         */
        function __construct()
        {
            $opts = array(
                'classname' => 'vintuna_post_carousel_widget',
                'description' => esc_html__('Displays post form selected category in carousel 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'),
                ),
                '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-carousel-style', esc_html__('Vintuna: Carousel 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',
                'offset'  => $post_offset,
                'posts_per_page' => $post_number,
                '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']);
            }
            $style_1_posts_query = new WP_Query($qargs);
            if ($style_1_posts_query->have_posts()) : ?>
                <div class="widget-layout widget-layout-carousel">
                    <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>

                                <div class="widget-title-controls">
                                    <button type="button" class="slide-btn slide-btn-small slide-widget-prev">
                                        <?php vintuna_theme_svg('chevron-left'); ?>
                                    </button>
                                    <button type="button" class="slide-btn slide-btn-small slide-widget-next">
                                        <?php vintuna_theme_svg('chevron-right'); ?>
                                    </button>
                                </div>


                    </header>

                    <div class="theme-widget-carousel" data-slick='{"rtl": <?php echo esc_attr($rtl); ?>}'>
                        <?php
                        while ($style_1_posts_query->have_posts()) :
                            $style_1_posts_query->the_post();
                        ?>
                            <div class="slick-carousel-widget">
                                <article id="carousel-widget-<?php the_ID(); ?>" <?php post_class('news-article news-article-default'); ?>>
                                    <?php if (has_post_thumbnail()) : ?>
                                        <div class="entry-image entry-image-small image-hover-effect hover-effect-shine">
                                            <a class="post-thumbnail" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
                                                <?php the_post_thumbnail('medium_large', ['alt' => the_title_attribute(['echo' => false])]); ?>
                                            </a>
                                        </div>
                                    <?php endif; ?>

                                    <div class="article-content">
                                        <h3 class="entry-title entry-title-small line-clamp-2">
                                            <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>
                                </article>
                            </div>
                        <?php
                            wp_reset_postdata();
                        endwhile; ?>
                    </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;
