<?php

/**
 * Elementor Widget
 * @package Meditest
 * @since 1.0.0
 */

namespace Elementor;

if (!defined('ABSPATH')) exit; // Exit if accessed directly.

class meditest_post_block_item extends Widget_Base
{

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

	/**
	 * Get widget title.
	 *
	 * Retrieve Elementor widget title.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget title.
	 */
	public function get_title()
	{
		return esc_html__('Post Block Item', 'meditest-extra');
	}

	/**
	 * Get widget icon.
	 *
	 * Retrieve Elementor widget icon.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return string Widget icon.
	 */
	public function get_icon()
	{
		return 'fas fa-th-large';
	}

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

	/**
	 * 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->layout_options();
		$this->post_query_options();
		$this->meta_options();
		$this->design_options();
	}

	/**
	 * Layout Options
	 */
	private function layout_options()
	{


		$this->start_controls_section(
			'layout_option',
			[
				'label' => __('Layout Options', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);


		$this->add_responsive_control(
			'post_block_one_img_height',
			[
				'label' => esc_html__('Set Post Image Height', 'meditest-extra'),
				'type' => Controls_Manager::SLIDER,
				'range' => [
					'px' => [
						'min' => 0,
						'max' => 1000,
					],
				],
				'devices' => ['desktop', 'tablet', 'mobile'],
				'desktop_default' => [
					'size' => 207,
					'unit' => 'px',
				],
				'tablet_default' => [
					'size' => 207,
					'unit' => 'px',
				],
				'mobile_default' => [
					'size' => 207,
					'unit' => 'px',
				],

				'default'  => [
					'unit' => 'px',
					'size' => 207,
				],

				'selectors' => [
					'{{WRAPPER}} .post-block-col-one.post-item-style-one .news-post-grid-thumbnail a img' => 'height: {{SIZE}}{{UNIT}};',
				],
			]
		);


		$this->end_controls_section();
	}

	/**
	 * Post Query Options
	 */
	private function post_query_options()
	{


		$this->start_controls_section(
			'post_query_option',
			[
				'label' => __('Post Options', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);


		// Post Sort

		$this->add_control(
			'post_sorting',
			[
				'type'    => Controls_Manager::SELECT2,
				'label' => esc_html__('Post Sorting', 'meditest-extra'),
				'default' => 'date',
				'options' => [
					'date' => esc_html__('Recent Post', 'meditest-extra'),
					'rand' => esc_html__('Random Post', 'meditest-extra'),
					'title'         => __('Title Sorting Post', 'meditest-extra'),
					'modified' => esc_html__('Last Modified Post', 'meditest-extra'),
					'comment_count' => esc_html__('Most Commented Post', 'meditest-extra'),

				],
			]
		);

		// Post Order

		$this->add_control(
			'post_ordering',
			[
				'type'    => Controls_Manager::SELECT2,
				'label' => esc_html__('Post Ordering', 'meditest-extra'),
				'default' => 'DESC',
				'options' => [
					'DESC' => esc_html__('Desecending', 'meditest-extra'),
					'ASC' => esc_html__('Ascending', 'meditest-extra'),
				],
			]
		);


		// Post Categories

		$this->add_control(
			'post_categories',
			[
				'type'      => Controls_Manager::SELECT2,
				'label' => esc_html__('Select Categories', 'meditest-extra'),
				'options'   => $this->posts_cat_list(),
				'label_block' => true,
				'multiple'  => true,
			]
		);



		// Post Items.

		$this->add_control(
			'post_number',
			[
				'label'         => esc_html__('Number Of Posts', 'meditest-extra'),
				'type'          => Controls_Manager::NUMBER,
				'default'       => '2',
			]
		);

		$this->add_control(
			'enable_offset_post',
			[
				'label' => esc_html__('Enable Skip Post', 'meditest-extra'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'meditest-extra'),
				'label_off' => esc_html__('No', 'meditest-extra'),
				'default' => 'no',

			]
		);

		$this->add_control(
			'post_offset_count',
			[
				'label'         => esc_html__('Skip Post Count', 'meditest-extra'),
				'type'          => Controls_Manager::NUMBER,
				'default'       => '1',
				'condition' => ['enable_offset_post' => 'yes']

			]
		);


		$this->end_controls_section();
	}

	/**
	 * Meta Options
	 */
	private function meta_options()
	{


		$this->start_controls_section(
			'meta_option',
			[
				'label' => __('Meta Options', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);

		$this->add_control(
			'display_excerpt',
			[
				'label' => esc_html__('Display Post Excerpt', 'meditest-extra'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'meditest-extra'),
				'label_off' => esc_html__('No', 'meditest-extra'),
				'default' => 'yes',
			]
		);

		$this->add_control(
			'display_cat',
			[
				'label' => esc_html__('Display Category Name', 'meditest-extra'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'meditest-extra'),
				'label_off' => esc_html__('No', 'meditest-extra'),
				'default' => 'yes',
			]
		);

		$this->add_control(
			'display_tab_top_meta',
			[
				'label' => esc_html__('Display Tab Large Post Meta', 'meditest-extra'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'meditest-extra'),
				'label_off' => esc_html__('No', 'meditest-extra'),
				'default' => 'yes',
			]
		);

		$this->add_control(
			'display_author_meta',
			[
				'label' => esc_html__('Display Author Thumbnail', 'meditest-extra'),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_html__('Yes', 'meditest-extra'),
				'label_off' => esc_html__('No', 'meditest-extra'),
				'default' => 'yes',
			]
		);






		$this->end_controls_section();
	}

	/**
	 * Design Options
	 */
	private function design_options()
	{


		$this->start_controls_section(
			'design_one_post_option',
			[
				'label' => __('Slider Typography', 'meditest-extra'),
				'tab' => Controls_Manager::TAB_CONTENT,
			]
		);


		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'bone_post_title_typography',
				'label' => __('Tab Big Title Typography', 'meditest-extra'),
				'selector' => '{{WRAPPER}} .post-block-col-one.post-item-style-one .grid-content-inner h3.post-title',
			]
		);


		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'bone_post_content_typography',
				'label' => __('Small Paragraph Typography', 'meditest-extra'),
				'selector' => '{{WRAPPER}} .post-block-col-one.post-item-style-one .grid-content-inner .post-excerpt-box p',
			]
		);


		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'bone_post_cat_meta_typography',
				'label' => esc_html__('Category Meta Typography', 'meditest-extra'),
				'selector' => '{{WRAPPER}} .post-block-col-one.post-item-style-one .post-category-box.post-block-meta-cat .news-cat_Name',
			]
		);


		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'bone_post_meta_typography',
				'label' => esc_html__('Post Meta Typography', 'meditest-extra'),
				'selector' => '{{WRAPPER}} .post-block-col-one.post-item-style-one .post-meta-bottom-list-wrap .post-meta-left-wrap ul.post-bottom-meta-list li',
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'bone_post_author_box_typography',
				'label' => esc_html__('Author Box Typography', 'meditest-extra'),
				'selector' => '{{WRAPPER}} .post-block-col-one.post-item-style-one .post-meta-bottom-list-wrap .post-meta-left-content h4.post-author-name',
			]
		);


		$this->add_control('bone_post_title_color', [
			'label' => esc_html__('Large Post Title Color', 'senatory-master'),
			'type' => Controls_Manager::COLOR,
			'selectors' => [
				"{{WRAPPER}} .post-block-col-one.post-item-style-one .grid-content-inner h3.post-title a" => "color: {{VALUE}}"
			]
		]);

		$this->add_control('bone_post_one_meta_color', [
			'label' => esc_html__('Post Metas Color', 'vibenews-extra'),
			'type' => Controls_Manager::COLOR,
			'selectors' => [
				"{{WRAPPER}} .post-block-col-one.post-item-style-one .post-meta-bottom-list-wrap .post-meta-left-wrap ul.post-bottom-meta-list li" => "color: {{VALUE}}"
			]
		]);

		$this->add_control('bone_post_one_author_color', [
			'label' => esc_html__('Post Author Color', 'vibenews-extra'),
			'type' => Controls_Manager::COLOR,
			'selectors' => [
				"{{WRAPPER}} .post-block-col-one.post-item-style-one .post-meta-bottom-list-wrap .post-meta-left-content h4.post-author-name a" => "color: {{VALUE}}"
			]
		]);




		$this->add_control(
			'title_length',
			[
				'label'         => esc_html__('Post Title Length', 'meditest-extra'),
				'type'          => Controls_Manager::NUMBER,
				'default'       => '10',
			]
		);

		$this->add_control(
			'content_length',
			[
				'label'         => esc_html__('Post Excerpt Length', 'meditest-extra'),
				'type'          => Controls_Manager::NUMBER,
				'default'       => '20',
			]
		);



		$this->end_controls_section();
	}



	protected function render()
	{


		$settings = $this->get_settings_for_display();

		$title_limit = $settings['title_length'];
		$content_limit = $settings['content_length'];
		$post_count = $settings['post_number'];
		$post_order  = $settings['post_ordering'];
		$post_sortby = $settings['post_sorting'];
		$display_blog_excerpt = $settings['display_excerpt'];
		$display_blog_cat = $settings['display_cat'];

		$display_tab_top_meta = $settings['display_tab_top_meta'];

		$display_author_meta = $settings['display_author_meta'];



		$args = [
			'post_type' => 'post',
			'post_status' => 'publish',
			'order' => $settings['post_ordering'],
			'posts_per_page' => $settings['post_number'],
			'ignore_sticky_posts' => 1,
		];

		// Category
		if (!empty($settings['post_categories'])) {
			$args['category_name'] = implode(',', $settings['post_categories']);
		}

		// Post Sorting
		if (!empty($settings['post_sorting'])) {
			$args['orderby'] = $settings['post_sorting'];
		}

		// Post Offset		
		if ($settings['enable_offset_post'] == 'yes') {
			$args['offset'] = $settings['post_offset_count'];
		}

		// Query
		$query = new \WP_Query($args); ?>




		<?php if ($query->have_posts()) : ?>

			<?php while ($query->have_posts()) : $query->the_post(); ?>

				<div class="post-block-item-one post-block-col-one post-item-style-one">

					<div class="news-post-grid-thumbnail">
						<a href="<?php the_permalink(); ?>" class="news-post-grid-thumbnail-wrap">
							<img src="<?php echo esc_attr(esc_url(get_the_post_thumbnail_url(null, 'full'))); ?>" alt="<?php the_title_attribute(); ?>">
						</a>
					</div>

					<div class="news-post-grid-content grid-content-inner">

						<div class="grid-content-meta-uniq">
							<div class="post-category-box post-block-meta-cat">
								<?php require MEDITEST_THEME_DIR . '/template-parts/cat-alt-template.php'; ?>
							</div>

							<div class="cutom-comment-wrapper">
								<i class="ri-chat-4-line"></i> <?php echo get_comments_number(); ?>
							</div>

						</div>

						<h3 class="post-title">
							<a href="<?php the_permalink(); ?>"><?php echo esc_html(wp_trim_words(get_the_title(), $title_limit, '')); ?></a>
						</h3>

						<?php if ($display_blog_excerpt == 'yes') : ?>
							<div class="post-excerpt-box">
								<p><?php echo esc_html(wp_trim_words(get_the_excerpt(), $content_limit, '')); ?></p>
							</div>
						<?php endif; ?>


						<div class="post-meta-bottom-list-wrap cols-post-meta-items">
							<div class="post-meta-left-wrap cols-meta-left">
								<div class="post-meta-left-author">
									<?php echo get_avatar(get_the_author_meta('ID'), 40); ?>
								</div>

								<div class="post-meta-left-content">

									<h4 class="post-author-name">
										<?php echo get_the_author_link(); ?>
									</h4>

								</div>
							</div>

							<div class="custom-meta-date-bx">
								<?php echo esc_html(get_the_date('F j, Y')); ?>
							</div>

						</div>




					</div>

				</div>



			<?php endwhile; ?>

			<?php wp_reset_postdata(); ?>

		<?php endif; ?>



<?php
	}


	public function posts_cat_list()
	{

		$terms = get_terms(array(
			'taxonomy'    => 'category',
			'hide_empty'  => true
		));

		$cat_list = [];
		foreach ($terms as $post) {
			$cat_list[$post->slug]  = [$post->name];
		}
		return $cat_list;
	}
}


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