<?php
namespace ova_framework\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Core\Schemes\Typography;
use Elementor\Group_Control_Typography;
use Elementor\Core\Schemes\Color;
use Elementor\Group_Control_Border;

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

class ova_blog extends Widget_Base {

	public function get_name() {
		return 'ova_blog';
	}

	public function get_title() {
		return __( 'Blog', 'ova-framework' );
	}

	public function get_icon() {
		return 'eicon-posts-ticker';
	}

	public function get_categories() {
		return [ 'ovatheme' ];
	}

	public function get_script_depends() {
		return [ 'script-elementor' ];
	}

	protected function _register_controls() {

		$args = array(
		  'orderby' => 'name',
		  'order' => 'ASC'
		  );

		$categories=get_categories($args);
		$cate_array = array();
		$arrayCateAll = array( 'all' => 'All categories ' );
		if ($categories) {
			foreach ( $categories as $cate ) {
				$cate_array[$cate->cat_name] = $cate->slug;
			}
		} else {
			$cate_array["No content Category found"] = 0;
		}



		//SECTION CONTENT
		$this->start_controls_section(
			'section_content',
			[
				'label' => __( 'Content', 'ova-framework' ),
			]
		);

			$this->add_control(
				'category',
				[
					'label' => __( 'Category', 'ova-framework' ),
					'type' => Controls_Manager::SELECT,
					'default' => 'all',
					'options' => array_merge($arrayCateAll,$cate_array),
				]
			);

			$this->add_control(
				'total_count',
				[
					'label' => __( 'Post Total', 'ova-framework' ),
					'type' => Controls_Manager::NUMBER,
					'default' => 3,
				]
			);

			$this->add_control(
				'number_column',
				[
					'label' => __( 'Number Of Columns', 'ova-framework' ),
					'type' => Controls_Manager::SELECT,
					'default' => 'grid_sidebar',
					'options' => [
						'grid_sidebar' => esc_html__( '2 Columns', 'ova-framework' ),
						'grid_medium' => esc_html__( '3 Columns', 'ova-framework' ),
						'grid_small' => esc_html__( '4 Columns', 'ova-framework' ),

						
					]
				]
			);

			


			$this->add_control(
				'order_by',
				[
					'label' => __('Order', 'ova-framework'),
					'type' => Controls_Manager::SELECT,
					'default' => 'desc',
					'options' => [
						'asc' => __('Ascending', 'ova-framework'),
						'desc' => __('Descending', 'ova-framework'),
					]
				]
			);

			$this->add_control(
				'text_readmore',
				[
					'label' => __( 'Text Read More', 'ova-framework' ),
					'type' => Controls_Manager::TEXT,
					'default' => __('Continue Reading', 'ova-framework'),
				]
			);


			$this->add_control(
				'show_date',
				[
					'label' => __( 'Show Date', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'return_value' => 'yes',
					'default' => 'yes',
				]
			);

			$this->add_control(
				'show_meta',
				[
					'label' => __( 'Show Meta', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'return_value' => 'yes',
					'default' => 'yes',
				]
			);


			$this->add_control(
				'show_title',
				[
					'label' => __( 'Show Title', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'return_value' => 'yes',
					'default' => 'yes',
				]
			);


			$this->add_control(
				'show_read_more',
				[
					'label' => __( 'Show Reada More', 'ova-framework' ),
					'type' => \Elementor\Controls_Manager::SWITCHER,
					'label_on' => __( 'Show', 'ova-framework' ),
					'label_off' => __( 'Hide', 'ova-framework' ),
					'return_value' => 'yes',
					'default' => 'yes',
				]
			);

		$this->end_controls_section();
		//END SECTION CONTENT


		//SECTION TAB STYLE DATE
		$this->start_controls_section(
			'section_date',
			[
				'label' => __( 'Date', 'ova-framework' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'date_typography',
				'selector' => '{{WRAPPER}} .blog-grid article.post-wrap .post-meta-date .post-meta-content-date',
				'scheme' => Typography::TYPOGRAPHY_1,
			]
		);

		$this->add_control(
			'color_date',
			[
				'label' => __( 'Color', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-date .post-meta-content-date' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'bg_color_date',
			[
				'label' => __( 'Background Color', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-date .post-meta-content-date' => 'background-color : {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();
		//END SECTION TAB STYLE DATE


		$this->start_controls_section(
			'section_meta',
			[
				'label' => __( 'Meta', 'ova-framework' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'meta_typography',
				'selector' => '{{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid .post-meta-content-grid a, {{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid .post-meta-content-grid span',
				'scheme' => Typography::TYPOGRAPHY_1,
			]
		);

		$this->add_control(
			'color_meta',
			[
				'label' => __( 'Color', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid .post-meta-content-grid a' => 'color : {{VALUE}};',
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid .post-meta-content-grid span' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'color_meta_hover',
			[
				'label' => __( 'Color Hover', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid .post-meta-content-grid a:hover' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'margin_meta',
			[
				'label' => __( 'Margin', 'ova-framework' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-meta-grid' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();


		//SECTION TAB STYLE TITLE
		$this->start_controls_section(
			'section_title',
			[
				'label' => __( 'Title', 'ova-framework' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'title_typography',
				'selector' => '{{WRAPPER}} .blog-grid article.post-wrap .post-title h2.post-title a',
				'scheme' => Typography::TYPOGRAPHY_1,
			]
		);

		$this->add_control(
			'color_title',
			[
				'label' => __( 'Color Title', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-title h2.post-title a' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'color_title_hover',
			[
				'label' => __( 'Color Title Hover', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-title h2.post-title a:hover' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'margin_title',
			[
				'label' => __( 'Margin', 'ova-framework' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-title h2.post-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
		//END SECTION TAB STYLE TITLE


		//SECTION TAB STYLE READMORE
		$this->start_controls_section(
			'section_readmore',
			[
				'label' => __( 'Read More', 'ova-framework' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name' => 'readmore_typography',
				'selector' => '{{WRAPPER}} .blog-grid article.post-wrap .post-footer .egovt-post-readmore a',
				'scheme' => Typography::TYPOGRAPHY_1,
			]
		);

		$this->add_control(
			'color_readmore',
			[
				'label' => __( 'Color Read More', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-footer .egovt-post-readmore a' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'color_readmore_hover',
			[
				'label' => __( 'Color Read More Hover', 'ova-framework' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-footer .egovt-post-readmore a:hover' => 'color : {{VALUE}};',
				],
			]
		);

		$this->add_responsive_control(
			'margin_readmore',
			[
				'label' => __( 'Margin', 'ova-framework' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em', '%' ],
				'selectors' => [
					'{{WRAPPER}} .blog-grid article.post-wrap .post-footer .egovt-post-readmore' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$this->end_controls_section();
		//END SECTION TAB STYLE READMORE


	}

	/**
	 * Render the 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();
		
		$category = $settings['category'];
		$total_count = $settings['total_count'];
		$order = $settings['order_by'];
		$number_column = $settings['number_column'];

		$text_readmore = $settings['text_readmore'];
		$show_date = $settings['show_date'];
		$show_meta = $settings['show_meta'];
		$show_title = $settings['show_title'];
		$show_read_more = $settings['show_read_more'];


		$args = [];
		if ($category == 'all') {
			$args=[
				'post_type' => 'post',
				'posts_per_page' => $total_count,
				'order' => $order,
			];
		} else {
			$args=[
				'post_type' => 'post', 
				'category_name'=>$category,
				'posts_per_page' => $total_count,
				'order' => $order,
			];
		}

		$blog = new \WP_Query($args);

		?>

		<div class="ova-blog blog-grid <?php echo esc_attr( $number_column ) ?>">
			<div class="ova-wrap-grid">
			<?php

				if($blog->have_posts()) : while($blog->have_posts()) : $blog->the_post();

					
					$blog_img  = wp_get_attachment_image_url( get_post_thumbnail_id(), 'egovt_blog_thumb' );
					$medium_img  = wp_get_attachment_image_url( get_post_thumbnail_id(), 'medium' );
			?>
				<article class="post-wrap">
					<div class="wrap-article">
						<div class="post-media">
							<?php if( $blog_img ){ ?>
								<a href="<?php echo get_the_permalink() ?>">

									<img alt="<?php echo get_the_title(); ?>" src="<?php echo esc_url($blog_img); ?>" 
											
											srcset="<?php echo esc_url($blog_img).' 560w'; ?>,
											<?php echo esc_url($medium_img).' 300w'; ?>" 
											sizes="(max-width: 767px) 100vw, 560px" >
								</a>
							<?php } ?>
						</div>
						<div class="evgovt-content">
							<?php if( $show_date == 'yes' ){ ?>
							<div class="post-meta-date">
								<span class="post-meta-content-date">
								    <span class=" post-date">
								        <span class="right second_font"><?php the_time( get_option( 'date_format' ));?></span>
								    </span>
								</span>
							</div>
							<?php } ?>

							<?php if( $show_meta == 'yes' ){ ?>
							<div class="post-meta-grid">
								<span class="post-meta-content-grid">
								    <?php if(has_category( )) { ?>
								    	<span class="wp-categories">
								    		<span class="slash ova-meta-general"><?php esc_html_e('In', 'ova-framework') ?></span>
										    <span class=" categories">
										        <span class="right"><?php the_category('&sbquo;&nbsp;'); ?></span><!-- end right -->             
										    </span><!-- end categories -->
								    	</span><!-- end wp-category -->
									<?php } ?>

								    <span class=" comment">
								        <span class="right">
								        	<i data-feather="message-square"></i>
								            <?php comments_popup_link(
								            	esc_html__(' 0', 'ova-framework'), 
								            	esc_html__(' 1', 'ova-framework'), 
								            	' % ',
								            	'',
						                  		esc_html__( 'Comment off', 'ova-framework' )
								            ); ?>
								        </span>                
								    </span>

								</span>
							</div>
							<?php } ?>
							<?php if( $show_title == 'yes' ){ ?>
							<div class="post-title">
								<h2 class="post-title">
							        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
							          <?php the_title(); ?>
							        </a>
							      </h2>
							</div>
							<?php } ?>
							<?php if( $show_read_more == 'yes' ){ ?>
							<div class="post-footer">
								<div class="egovt-post-readmore">
								    <a class="btn-readmore second_font" href="<?php the_permalink(); ?>">
								    	<?php echo esc_html( $text_readmore ) ?>
								    	<i data-feather="arrow-right"></i>
								    </a>
								</div>
							</div>
							<?php } ?>
						</div>
					</div>
				</article>
			<?php
				endwhile; endif; wp_reset_postdata();
			?>
			</div>
		</div>
		<?php
	}
}
