<?php
/**
 * Elementor Widget
 * @package ele-blog
 * @since 1.0.0
 */

namespace Elementor;
class Eleblog_Post_Selector_Widget_Two 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_post_selector_two';
	}

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @return string Widget title.
	 * @since 1.0.0
	 * @access public
	 *
	 */
	public function get_title() {
		return esc_html__( 'Post Selector Two', '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(
			'select_post', [
				'label'   => esc_html__( 'Select Post', 'ele-blog' ),
				'type'    => Controls_Manager::SELECT,
				'options' => eleblog_select_post(),
			]
		);


		$this->add_control(
			'select_post_two', [
				'label'     => esc_html__( 'Second Post Select', 'ele-blog' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => eleblog_select_post(),

			]
		);

		$this->add_control(
			'select_post_third', [
				'label'     => esc_html__( 'Third Post Select', 'ele-blog' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => eleblog_select_post(),

			]
		);


		$this->end_controls_section();

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


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

		// Title color
		$this->add_control(
			'title_hover_color',
			[
				'label'     => esc_html__( 'Title Hover Color', 'ele-blog' ),
				'type'      => Controls_Manager::COLOR,
				'default'   => '#2979ff',
				'selectors' => [
					'{{WRAPPER}} .single-post-wrap.style-overlay .details .title a:hover' => 'color: {{VALUE}} !important;',
				],
			]
		);


		//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 ] ],
				],
			]
		);

		$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();


			$selected_post = new \WP_Query( array(
				'post_type'      => 'post',
				'posts_per_page' => 1,
				'p'              => $settings['select_post']
			) );
			if ( $selected_post->have_posts() ) :
				while ( $selected_post->have_posts() ) : $selected_post->the_post();


					?>
                    <div class="single-post-wrap style-overlay">
                        <div class="thumb">
                            <?php the_post_thumbnail(); 
							
							$cat = get_the_category();
							if ( isset( $cat[0] ) ):

								?>
                                <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>
							<?php endif; ?>

                        </div>
                        <div class="details">
                            <div class="post-meta-single">
                                <p><?php eleblog_post_date(); ?></p>
                            </div>
                            <h5 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                        </div>
                    </div>
				<?php endwhile;
				wp_reset_postdata(); endif; ?>

            <div class="eblog-row">
				<?php
				$selected_post = new \WP_Query( array(
					'post_type'      => 'post',
					'posts_per_page' => 1,
					'p'              => $settings['select_post_two']
				) );
				if ( $selected_post->have_posts() ) :
					while ( $selected_post->have_posts() ) : $selected_post->the_post();
					$src = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full', false, '' );

					$img_id    = get_post_thumbnail_id( get_the_ID() );
					$image_alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true );

						?>
                        <div class="eblog-col-lg-6 eblog-col-sm-6">
                            <div class="single-post-wrap style-overlay">
								<?php if ( has_post_thumbnail() ) : ?>
                                    <div class="thumb">
										<img src="<?php echo esc_url( ele_blog_img_resize( $src[0], 355, 226, true ) ); ?>"
                                         alt="<?php echo esc_attr( $image_alt ); ?>">
                                    </div>
								<?php endif; ?>
                                <div class="details">
                                    <div class="post-meta-single">
                                        <p><?php eleblog_post_date(); ?></p>
                                    </div>
                                    <h6 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                                </div>
                            </div>
                        </div>
					<?php

					endwhile;
					wp_reset_postdata(); endif;

				$selected_post = new \WP_Query( array(
					'post_type'      => 'post',
					'posts_per_page' => 1,
					'p'              => $settings['select_post_third']
				) );

				if ( $selected_post->have_posts() ) :
					while ( $selected_post->have_posts() ) : $selected_post->the_post();
					$src = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full', false, '' );

					$img_id    = get_post_thumbnail_id( get_the_ID() );
					$image_alt = get_post_meta( $img_id, '_wp_attachment_image_alt', true );

						?>

                        <div class="eblog-col-lg-6 eblog-col-sm-6">
                            <div class="single-post-wrap style-overlay">
								<?php if ( has_post_thumbnail() ) : ?>
                                    <div class="thumb">
										<img src="<?php echo esc_url( ele_blog_img_resize( $src[0], 355, 226, true ) ); ?>"
                                         alt="<?php echo esc_attr( $image_alt ); ?>">
                                    </div>
								<?php endif; ?>
                                <div class="details">
                                    <div class="post-meta-single">
                                        <p><?php eleblog_post_date(); ?></p>
                                    </div>
                                    <h6 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                                </div>
                            </div>
                        </div>
					<?php endwhile;
					wp_reset_postdata(); endif; ?>
            </div>
		<?php
	}
}

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