<?php
/**
 * Elementor Widget
 * @package eleblog
 * @since 1.0.0
 */

namespace Elementor;

class Eleblog_What_Is_New_Widget extends Widget_Base {

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

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @return string Widget title.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_title() {
		return esc_html__( 'What\'s New', 'ele-blog' );
	}

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

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

	/**
	 * Register Elementor widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 *
	 * @since 1.0.0
	 * @access protected
	 */
	protected function _register_controls() {

		$this->start_controls_section(
			'settings_sticky',
			[
				'label' => esc_html__( 'General Settings', 'ele-blog' ),
				'tab'   => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'section_title', [
				'label'   => esc_html__( 'Section Titlte', 'ele-blog' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( 'What’s new', 'ele-blog' )
			]
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'select_post', [
				'label' => __( 'Select Post', 'ele-blog' ),
				'type' => Controls_Manager::SELECT,
				'options' => eleblog_select_post(),
				'label_block' => true,
			]
		);

		$this->add_control(
			'select_posts',
			[
				'label' => __( 'Select Post', 'ele-blog' ),
				'type' => Controls_Manager::REPEATER,
				'fields' => $repeater->get_controls(),
			]
		);


		$this->add_control(
			'word_limit', [
				'label'   => esc_html__( 'Word Limit For Content', 'ele-blog' ),
				'type'    => Controls_Manager::TEXT,
				'default' => esc_html__( '40', 'ele-blog' )
			]
		);

		$this->end_controls_section();
				//style
		$this->start_controls_section(
			'style',
			[
				'label' => esc_html__( 'Style', 'ele-blog' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);

		//section Title color
		$this->add_control(
			'section_title_color',
			[
				'label'     => esc_html__( 'Section Title Color', 'ele-blog' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#201654',
				'selectors' => [
					'{{WRAPPER}} .section-title .title' => 'color: {{VALUE}};',
				],
			]
		);

		//section typography
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'           => 'Section_title_typography',
				'label'          => esc_html__( 'Section Title Typography', 'ele-blog' ),
				'selector'       => '{{WRAPPER}} .section-title .title',
				'fields_options' => [
					// first mimic the click on Typography edit icon
					'typography'  => [ 'default' => 'yes' ],
					// then redifine the Elementor defaults
					'font_size'   => [ 'default' => [ 'size' => 20 ] ],
					'font_weight' => [ 'default' => 500 ],
					'line_height' => [ 'default' => [ 'size' => 27 ] ],
				],
			]
		);


		// Title color
		$this->add_control(
			'title_color',
			[
				'label'     => esc_html__( 'Title Color', 'ele-blog' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#201654',
				'selectors' => [
					'{{WRAPPER}} .single-post-wrap .details .title a' => 'color: {{VALUE}};',
				],
			]
		);

		// Title color
		$this->add_control(
			'title_hover_color',
			[
				'label'     => esc_html__( 'Title Hover Color', 'ele-blog' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#2979ff',
				'selectors' => [
					'{{WRAPPER}} a:hover' => 'color: {{VALUE}};',
				],
			]
		);


		//title typography
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'           => 'title_typography',
				'label'          => esc_html__( 'Title Typography', 'ele-blog' ),
				'selector'       => '{{WRAPPER}} .single-post-wrap .details .title',
				'fields_options' => [
					// first mimic the click on Typography edit icon
					'typography'  => [ 'default' => 'yes' ],
					// then redifine the Elementor defaults
					'font_size'   => [ 'default' => [ 'size' => 18 ] ],
					'font_weight' => [ 'default' => 500 ],
					'line_height' => [ 'default' => [ 'size' => 24 ] ],
				],
			]
		);

		// Content color
		$this->add_control(
			'content_color',
			[
				'label'     => esc_html__( 'Content Color', 'ele-blog' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#201654',
				'selectors' => [
					'{{WRAPPER}} .single-post-wrap .details p' => 'color: {{VALUE}};',
				],
			]
		);


		//title typography
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'           => 'content_typography',
				'label'          => esc_html__( 'Content Typography', 'ele-blog' ),
				'selector'       => '{{WRAPPER}} .single-post-wrap .details p',
				'fields_options' => [
					// first mimic the click on Typography edit icon
					'typography'  => [ 'default' => 'yes' ],
					// then redifine the Elementor defaults
					'font_size'   => [ 'default' => [ 'size' => 16 ] ],
					'font_weight' => [ 'default' => 400 ],
					'line_height' => [ 'default' => [ 'size' => 27 ] ],
				],
			]
		);

		$this->end_controls_section();


	}

	/**
	 * Render Elementor 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();
		?>
		<?php if ( ! empty( $settings['section_title'] ) ) : ?>
            <div class="section-title">
                <h6 class="title"><?php echo esc_html( $settings['section_title'] ); ?></h6>
            </div>
		<?php endif; ?>
        <div class="post-slider owl-carousel">
			<?php

			foreach ( $settings['select_posts'] as $post ) :

				$selected_post = new \WP_Query( array(
					'post_type'      => 'post',
					'posts_per_page' => 1,
					'p'              => $post['select_post']
				) );
				if ( $selected_post->have_posts() ) :
					while ( $selected_post->have_posts() ) : $selected_post->the_post();
						$img_id    = get_post_thumbnail_id( get_the_ID() );
						$image_alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true );
						$src       = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full', false, '' );
						?>
                        <div class="item">
                            <div class="single-post-wrap">
								<?php if ( has_post_thumbnail() ) : ?>
                                    <div class="thumb">
									  <img src="<?php echo esc_url( ele_blog_img_resize( $src[0], 315, 210, true ) ); ?>"
                                         alt="<?php echo esc_attr( $image_alt ); ?>">
                                    </div>
								<?php endif; ?>
                                <div class="details">
                                    <div class="post-meta-single ele-post-meta-single mb-4 pt-1">
                                        <ul>
											<?php $cat = get_the_category(); ?>
											<?php
											if ( isset( $cat[0] ) ):
												?>
                                                <li><a class="tag-base ele-<?php echo esc_attr( $cat[0]->slug ); ?>"
                                                       href="<?php echo get_category_link( $cat[0]->term_id ); ?>"
                                                    >
														<?php echo get_cat_name( $cat[0]->term_id ); ?>
                                                    </a>
                                                </li>
											<?php endif; ?>
                                            <li><?php eleblog_post_date(); ?></li>
                                        </ul>
                                    </div>
                                    <h6 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                                    <p><?php echo wp_trim_words( get_the_content(), $settings[ 'word_limit' ] ); ?></p>
                                </div>
                            </div>
                        </div>
					<?php endwhile;
					wp_reset_postdata(); endif; endforeach; ?>
        </div>
		<?php
	}
}

Plugin::instance()->widgets_manager->register_widget_type( new Eleblog_What_Is_New_Widget() );