<?php

namespace Elementor;

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

            ]
        );
        $this->add_control(
            'lawyering_blog_post_count',
            [
                'label' => __('No. Of Posts', 'lawyering'),
                'type' => Controls_Manager::NUMBER,
                'default' =>3,
            ]
        );

        $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['lawyering_blog_get_category'];
        $posts_count = $settings['lawyering_blog_post_count'];
        $loop = ($posts_count<=0)?30:$posts_count;
        if($taxs){
            $args = array(
                'post_type' => 'post',
                'posts_per_page' => esc_attr($loop),
                '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),
                'post_status' => 'publish',
                'order' => 'desc',
                'orderby' => 'menu_order date',
            );
        }

        $query = new \WP_Query($args);

         if ($query->have_posts()):
            ?>
            <div class="custom-blog-widget">
                        <?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, 'lawyering-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">
                                    <div class="blog-img">
                                    <img src="<?php echo esc_url($image[0]); ?>" alt="" />

                                      </div>
                                    <div class="post-content">
                                    <h3 class="entry-title"><a href="<?php echo esc_url(get_the_permalink()); ?>"><?php the_title(); ?></a></h3>

                                                                                <?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()).'">';
                                            ?>
<svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" viewBox="0 0 24 24">
  <path fill-rule="evenodd" d="M12 20a7.966 7.966 0 0 1-5.002-1.756l.002.001v-.683c0-1.794 1.492-3.25 3.333-3.25h3.334c1.84 0 3.333 1.456 3.333 3.25v.683A7.966 7.966 0 0 1 12 20ZM2 12C2 6.477 6.477 2 12 2s10 4.477 10 10c0 5.5-4.44 9.963-9.932 10h-.138C6.438 21.962 2 17.5 2 12Zm10-5c-1.84 0-3.333 1.455-3.333 3.25S10.159 13.5 12 13.5c1.84 0 3.333-1.455 3.333-3.25S13.841 7 12 7Z" clip-rule="evenodd"/>
</svg>
                                            <?php echo '<span>'.get_the_author().'</span></a></div>';
                
                                            echo '<div class="date"><a href="'.esc_url(get_month_link($archive_year,$archive_month)).'"><svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
  <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m11.5 11.5 2.071 1.994M4 10h5m11 0h-1.5M12 7V4M7 7V4m10 3V4m-7 13H8v-2l5.227-5.292a1.46 1.46 0 0 1 2.065 2.065L10 17Zm-5 3h14a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H5a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1Z"/>
</svg>
<span>'.esc_html(get_the_date()).'</span></a></div></div>';
                                            ?>

                                            <?php
                                         
                
                                            
                                            ?>
                                            <p class="post-excerpt"><?php echo wp_kses_post(lawyering_get_excerpt(get_the_ID(), 125)); ?></p>
                                        </div>
                                    </article>
                                <?php $loop--; endif; endwhile;
                        wp_reset_postdata(); ?>
                    </div>
        <?php endif;
	}
	
}
/*=============Call this every widget ====================*/
Plugin::instance()->widgets_manager->register_widget_type( new lawyering_elementor_blog() );

