<?php
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

/**
 * Elementor oEmbed Widget.
 *
 * Elementor widget that inserts an embbedable content into the page, from any given URL.
 *
 * @since 1.0.0
 */
class Elementor_slider_post_Widget extends \Elementor\Widget_Base {

/**
 * Get widget name.
 *
 * Retrieve oEmbed widget name.
 *
 * @return string Widget name.
 * @since 1.0.0
 * @access public
 */
public function get_name()
{
    return 'hyperkala_slider_post';
}

/**
 * Get widget title.
 *
 * Retrieve oEmbed widget title.
 *
 * @return string Widget title.
 * @since 1.0.0
 * @access public
 */
public function get_title()
{
    return 'اسلایدر نوشته‌ها';
}

/**
 * Get widget icon.
 *
 * Retrieve oEmbed widget icon.
 *
 * @return string Widget icon.
 * @since 1.0.0
 * @access public
 */
public function get_icon()
{
    return 'eicon-post-slider';
}

/**
 * Get custom help URL.
 *
 * Retrieve a URL where the user can get more information about the widget.
 *
 * @return string Widget help URL.
 * @since 1.0.0
 * @access public
 */
public function get_custom_help_url()
{
    return 'https://developers.elementor.com/docs/widgets/';
}

/**
 * Get widget categories.
 *
 * Retrieve the list of categories the oEmbed widget belongs to.
 *
 * @return array Widget categories.
 * @since 1.0.0
 * @access public
 */
public function get_categories()
{
    return ['hyperkala'];
}

/**
 * Get widget keywords.
 *
 * Retrieve the list of keywords the oEmbed widget belongs to.
 *
 * @return array Widget keywords.
 * @since 1.0.0
 * @access public
 */
public function get_keywords()
{
    return ['oembed', 'url', 'link'];
}

/**
 * Register oEmbed widget controls.
 *
 * Add input fields to allow the user to customize the widget settings.
 *
 * @since 1.0.0
 * @access protected
 */
    function get_post_cat(){
        $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => true,
        ));

        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $options[ $term->term_id ] = $term->name;
            }
            return $options;
        }
    }

protected function register_controls()
{

    $this->start_controls_section(
        'content_section',
        [
            'label' => 'تنظیمات محتوا',
            'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
    );
    $this->add_control(
        'content_title',
        [
            'label' => 'عنوان بخش',
            'type' => \Elementor\Controls_Manager::TEXT,
            'default' => '',
            'placeholder' => 'عنوان بخش را وارد کنید',
        ]
    );
    $this->add_control(
        'icon',
        [
            'label' => 'آيکن',
            'type' => \Elementor\Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-circle',
                'library' => 'fa-solid',
            ],
        ]
    );
    $this->add_control(
        'more_title',
        [
            'label' => 'متن دکمه',
            'type' => \Elementor\Controls_Manager::TEXT,
            'default' => '',
            'placeholder' => 'متن روی دکمه مشاهده بیشتر را وارد کنید',
        ]
    );
    $this->add_control(
        'more_link',
        [
            'label' => 'لینک دکمه',
            'type' => \Elementor\Controls_Manager::TEXT,
            'default' => '',
            'placeholder' => 'https://your-link.ir',
        ]
    );
    $this->add_control(
        'posts_slider_select_category',
        [
            'label' => __( 'دسته بندی', 'plugin-domain' ),
            'type' => \Elementor\Controls_Manager::SELECT2,
            'multiple' => true,
            'options' => $this->get_post_cat(),
            'description' =>'اگر چیزی انتخاب نشده باشد همه دسته‌ها را در نظر می‌گیرد'
        ]
    );
    $this->end_controls_section();
    $this->start_controls_section(
        'slider_settings_section',
        [
            'label' => 'تنظیمات اسلایدر',
            'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
        ]
    );

    $this->add_control(
        'slider_main_count_mobile',
        [
            'label' => 'تعداد نمایش اسلاید در گوشی',
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 1,
            'max' => 4,
            'step' => 1,
            'default' => 1,
        ]
    );
    $this->add_control(
        'slider_main_count_tablet',
        [
            'label' => 'تعداد نمایش اسلاید در تبلت',
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 1,
            'max' => 4,
            'step' => 1,
            'default' => 1,
        ]
    );
    $this->add_control(
        'slider_main_count_desktop',
        [
            'label' => 'تعداد نمایش اسلاید در دسکتاپ',
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 1,
            'max' => 6,
            'step' => 1,
            'default' => 3,
        ]
    );
    $this->add_control(
        'slider_main_count_desktop',
        [
            'label' => 'تعداد نمایش اسلاید در دسکتاپ',
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 2,
            'max' => 8,
            'step' => 1,
            'default' => 4,
        ]
    );
    $this->add_control(
        'slider_main_loop',
        [
            'label' => 'چرخش حلقه',
            'type' => \Elementor\Controls_Manager::SWITCHER,
            'label_on' => 'روشن',
            'label_off' => 'خاموش',
            'return_value' => 'yes',
            'default' => 'yes',
        ]
    );
    $this->add_control(
        'slider_main_autoplay',
        [
            'label' => 'حرکت خودکار',
            'type' => \Elementor\Controls_Manager::SWITCHER,
            'label_on' => 'روشن',
            'label_off' => 'خاموش',
            'return_value' => 'yes',
            'default' => 'yes',
        ]
    );
    $this->add_control(
        'slider_main_dots',
        [
            'label' => 'نقطه‌ها',
            'type' => \Elementor\Controls_Manager::SWITCHER,
            'label_on' => 'روشن',
            'label_off' => 'خاموش',
            'return_value' => 'yes',
            'default' => 'yes',
        ]
    );
    $this->add_control(
        'article_slide_speed',
        [
            'label' => __( 'سرعت حرکت اسلایدها', 'plugin-domain' ),
            'type' => \Elementor\Controls_Manager::NUMBER,
            'min' => 200,
            'max' => 10000,
            'step' => 1,
            'default' => 5000,
        ]
    );

    $this->end_controls_section();

}

/**
 * Render oEmbed widget output on the frontend.
 *
 * Written in PHP and used to generate the final HTML.
 *
 * @since 1.0.0
 * @access protected
 */

protected function render() {
$settings = $this->get_settings_for_display(); ?>
    <script>
        $(function (){
        $('.posts-slider-element').owlCarousel({
            loop:<?php if ($settings['slider_main_loop']) {echo "true"; } else {echo "false"; } ?>,
            margin:10,
            nav:true,
            dots:<?php if ($settings['slider_main_dots']) {echo "true"; } else {echo "false"; } ?>,
            autoplay:<?php if ($settings['slider_main_autoplay']) {echo "true"; } else {echo "false"; } ?>,
            autoplayTimeout:<?php echo $settings['article_slide_speed']; ?>,
            autoplayHoverPause: true,
            responsive:{
                0:{
                    items:<?php echo $settings['slider_main_count_mobile']; ?>
                },
                600:{
                    items:<?php echo $settings['slider_main_count_tablet']; ?>
                },
                1000:{
                    items:<?php echo $settings['slider_main_count_desktop']; ?>
                }
            }
        });
        });
    </script>
    <div class="article-slider">
        <?php if ($settings['content_title']){?>
        <div class="title-section">
            <div class="title-item">
                <a href="<?php echo $settings['more_link'];?>"><i class="fas fa-arrow-left"></i>
                    <span><?php echo $settings['more_title'];?></span></a>
            </div>
            <div class="title-item">
                <span><?php echo $settings['content_title'];?></span>
                <?php \Elementor\Icons_Manager::render_icon( $settings['icon'], [ 'aria-hidden' => 'true' ] ); ?>
            </div>
        </div>
        <?php }?>
        <div id="posts-slider-element" class="owl-carousel owl-theme posts-slider-element">
            <?php
            $article = new WP_Query(array(
                'post_type' => 'post',
                'posts_per_page' => 6,
                'cat' => $settings['posts_slider_select_category'],
            ));
            if($article->have_posts()) {
                while ($article->have_posts()) : $article->the_post(); ?>
                <a href="<?php echo get_the_permalink(); ?>" class="spostw-link"><div class="item">
                        <span class="post-cat"><?php echo get_the_category( get_the_ID() )[0]->name; ?></span>
                        <?php the_post_thumbnail('article-slider-element');?>
                        <span class="spostw-title"><?php echo wp_trim_words(get_the_title(),5); ?></span>
                        <span style="visibility: hidden;"><?php echo get_the_category( get_the_ID() )[0]->name; ?></span>
                    </div></a>
                <?php
                endwhile;
            }
            wp_reset_postdata();
            ?>
        </div>
    </div>
    <?php
}

}
