<?php

namespace Elementor;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Define our Pt Elementor Call Out settings.
 */
class multiblog_elementor_list_blog extends Widget_Base {
    /**
     * Define our get_name settings.
     */
    public function get_name() {
        return 'multiblog-list-blog';
    }
    /**
     * Define our get_title settings.
     */
    public function get_title() {
        return __( 'Blog List Layout', 'multiblog' );
    }
    /**
     * Define our get_icon settings.
     */
    public function get_icon() {
        return 'eicon-call-to-action';
    }
    /**
     * Define our get_categories settings.
     */
    public function get_categories() {
        return [ 'multiblog-elementor-widget' ];
    }
    /**
     * Define our _register_controls settings.
     */
    protected function register_controls()
    {
        /**
         * Info Box Title and Description Section.
         */
        $this->start_controls_section(
            'multiblog_blog_section',
            [
                'label' => esc_html__('Blog Options', 'multiblog'),
            ]
        );
        $this->add_control(
            'multiblog_blog_get_category',
            [
                'label' => __('Select Category', 'multiblog'),
                'type' => Controls_Manager::SELECT2,
                'default' => array(),
                'options' => multiblog_blog_get_category(),
                'multiple' => true

            ]
        );
        $this->add_control(
            'multiblog_blog_post_count',
            [
                'label' => __('No. Of Posts (Max 5 Posts in Free version', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' =>3,
                'max' =>5,
            ]
        );

        $this->add_control(
            'multiblog_blog_post_offset',
            [
                'label' => __('Post Offset', 'multiblog'),
                'type' => Controls_Manager::NUMBER,
                'default' => 0,
                'description' => __('Number of posts to skip', 'multiblog'),
            ]
        );

        $this->add_control(
            'show_image',
            [
                'label' => esc_html__( 'Show Featured Image', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_title',
            [
                'label' => esc_html__( 'Show Post Excerpt', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_author_meta',
            [
                'label' => esc_html__( 'Show Author Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'show_date_meta',
            [
                'label' => esc_html__( 'Show Date Meta', 'multiblog' ),
                'type' => \Elementor\Controls_Manager::SWITCHER,
                'label_on' => esc_html__( 'Show', 'multiblog' ),
                'label_off' => esc_html__( 'Hide', 'multiblog' ),
                'return_value' => 'yes',
                'default' => 'yes',
            ]
        );

        $this->end_controls_section();


            // Heading style
            $this->start_controls_section(
                'multiblog_heading_style',
                [
                    'label' => esc_html__('Heading Style', 'multiblog'),
                    'tab' => Controls_Manager::TAB_STYLE,
                ]
            );

            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'name' => 'heading_typography',
                    'label' => __('Typography', 'multiblog'),
                    'selector' => '{{WRAPPER}} .custom-blog-widget h3',
                ]
            );
            $this->add_control(
                'heading_color',
                [
                    'label' => __('Heading Color', 'multiblog'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .custom-blog-widget h3.entry-title a' => 'color: {{VALUE}};',
                    ],
                ]
            );

            $this->end_controls_section();


            // Post Excerptstyle
            $this->start_controls_section(
                'multiblog_excerpt_style',
                [
                    'label' => esc_html__('Excerpt Style', 'multiblog'),
                    'tab' => Controls_Manager::TAB_STYLE,
                ]
            );

            $this->add_group_control(
                Group_Control_Typography::get_type(),
                [
                    'name' => 'excerpt_typography',
                    'label' => __('Typography', 'multiblog'),
                    'selector' => '{{WRAPPER}} .post-excerpt',
                ]
            );
             $this->add_control(
                'excerpt_color',
                [
                    'label' => __('Excerpt Color', 'multiblog'),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'selectors' => [
                        '{{WRAPPER}} .custom-blog-widget p.post-excerpt' => 'color: {{VALUE}};',
                    ],
                ]
            );       

            $this->end_controls_section();


    }
    /**
     * Define our Content Display inline settings.
     */
    protected function add_inline_editing_attributes( $key, $toolbar = 'basic' ) {
        if ( ! Plugin::$instance->editor->is_edit_mode() ) {
            return;
        }
        $this->add_render_attribute( $key, [
            'class' => 'elementor-inline-editing',
            'data-elementor-setting-key' => $key,
        ] );
        if ( 'basic' !== $toolbar ) {
            $this->add_render_attribute( $key, [
                'data-elementor-inline-editing-toolbar' => $toolbar,
            ] );
        }
    }

    /**
     * Define our Content Display settings.
     */
    protected function render() {
        $settings = $this->get_settings();
        $taxs = $settings['multiblog_blog_get_category'];
        $posts_count = $settings['multiblog_blog_post_count'];
        $post_offset = $settings['multiblog_blog_post_offset'] ?? 0;

        $loop = ($posts_count<=0)?5:$posts_count;
        if($taxs){
            $args = array(
                'post_type' => 'post',
                'posts_per_page' => esc_attr($loop),
                'offset' => esc_attr($post_offset),
                'post_status' => 'publish',
                'orderby' => 'menu_order date',
                'order' => 'desc',
                'tax_query' =>
                    array(
                        array(
                            'taxonomy' => 'category',
                            'field' => 'id',
                            'terms' => $taxs,
                        ),
                    ),
            );
        }
        else{
            $args = array(
                'post_type' => 'post',
                'posts_per_page' => esc_attr($loop),
                'offset' => esc_attr($post_offset),
                'post_status' => 'publish',
                'order' => 'desc',
                'orderby' => 'menu_order date',
            );
        }

        $query = new \WP_Query($args);

         if ($query->have_posts()):
            ?>
            <div class="custom-blog-widget list-layout">
                        <?php
                        while ($query->have_posts()) : $query->the_post();
                            global $post;
                            $post_format = get_post_format($post->ID);
                            $post_thumbnail_id = get_post_thumbnail_id(get_the_ID());
                            $image = wp_get_attachment_image_src($post_thumbnail_id, 'multiblog-blog-thumbnail-img');
                            $content = get_the_content();
  

                            if (!empty($image)) {
                                $image_style = "style='background-image:url(" . esc_url($image[0]) . ")'";
                            } else {
                                $image_style = '';
                            }

                            if($loop>=1) :
                                ?>
                                <article class="blog-post">
                                   <?php if ( 'yes' === $settings['show_image'] ) { ?> <div class="blog-img">
                                    <img src="<?php echo esc_url($image[0]); ?>" alt="" />
                                      </div>
                                      <?php } ?>  
                                    <div class="post-content">
                                    <h3 class="entry-title"><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>
                                        <?php if ( 'yes' === $settings['show_title'] ) { ?>
                                             <p class="post-excerpt"><?php echo wp_kses_post(multiblog_get_excerpt(get_the_ID(), 125)); ?></p>   <?php } ?>                    

                                            <?php
                                            $comment_count = get_comments_number();
                                            $archive_year  = get_the_time('Y');
                                            $archive_month = get_the_time('m');
                                            echo '<div class="entry-meta"><div class="author">
<a href="'.esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ).'" title="'.esc_attr(get_the_author()).'">';
                                            ?>

                                             <?php if ( 'yes' === $settings['show_author_meta'] ) {  echo '<span>'.get_the_author().'</span></a></div>';  } 
                
                                            if ( 'yes' === $settings['show_date_meta'] ) { echo '<div class="date"><a href="'.esc_url(get_month_link($archive_year,$archive_month)).'">
<span>'.esc_html(get_the_date()).'</span></a></div></div>'; }
                                            ?>

                                            <?php
                                         
                
                                            
                                            ?>

                                        </div>
                                    </article>
                                <?php $loop--; endif; endwhile;
                        wp_reset_postdata(); ?>
                    </div>
        <?php endif;
    }
    
}
/*=============Call this every widget ====================*/
Plugin::instance()->widgets_manager->register_widget_type( new multiblog_elementor_list_blog() );