<?php

namespace TRANZIXPLUGIN\Element;

use Elementor\Controls_Manager;
use Elementor\Controls_Stack;
use Elementor\Group_Control_Typography;
use Elementor\Scheme_Typography;
use Elementor\Scheme_Color;
use Elementor\Group_Control_Border;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Plugin;
use Elementor\Utils;

/**
 * Elementor button widget.
 * Elementor widget that displays a button with the ability to control every
 * aspect of the button design.
 *
 * @since 1.0.0
 */
class H1_Blog extends Widget_Base {

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

	/**
	 * Get widget title.
	 * Retrieve button widget title.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'H1 Blog', 'tranzix' );
	}

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

	/**
	 * Get widget categories.
	 * Retrieve the list of categories the button widget belongs to.
	 * Used to determine where to display the widget in the editor.
	 *
	 * @since  2.0.0
	 * @access public
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'tranzix' ];
	}
	
	/**
	 * Register button 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(
			'h1_blog',
			[
				'label' => esc_html__( 'H1 Blog', 'tranzix' ),
			]
		);
		$this->add_control(
			'bgimg',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'bgimg1',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'rashid' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your title', 'rashid' ),
			]
		);
		$this->add_control(
			'column',
			[
				'label'   => esc_html__( 'Column', 'rashid' ),
				'type'    => Controls_Manager::SELECT,
				'default' => '3',
				'options' => array(
					'12'   => esc_html__( 'One Column', 'rashid' ),
					'6'   => esc_html__( 'Two Column', 'rashid' ),
					'4'   => esc_html__( 'Three Column', 'rashid' ),
					'3'   => esc_html__( 'Four Column', 'rashid' ),
					'2'   => esc_html__( 'Six Column', 'rashid' ),
				),
			]
		);
		$this->add_control(
			'sec_class',
			[
				'label'       => __( 'Section Class', 'rashid' ),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter Section Class', 'rashid' ),
			]
		);
		
		
		$this->end_controls_section();
	
		$this->start_controls_section(
				'content_section',
				[
					'label' => __( 'Blog Block', 'rashid' ),
					'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
				]
			);
			$this->add_control(
				'text_limit',
				[
					'label'   => esc_html__( 'Text Limit', 'tranzix' ),
					'type'    => Controls_Manager::NUMBER,
					'default' => 15,
					'min'     => 1,
					'max'     => 100,
					'step'    => 1,
				]
			);
			$this->add_control(
				'query_number',
				[
					'label'   => esc_html__( 'Number of post', 'tranzix' ),
					'type'    => Controls_Manager::NUMBER,
					'default' => 3,
					'min'     => 1,
					'max'     => 100,
					'step'    => 1,
				]
			);
			$this->add_control(
				'query_orderby',
				[
					'label'   => esc_html__( 'Order By', 'tranzix' ),
					'type'    => Controls_Manager::SELECT,
					'default' => 'date',
					'options' => array(
						'date'       => esc_html__( 'Date', 'tranzix' ),
						'title'      => esc_html__( 'Title', 'tranzix' ),
						'menu_order' => esc_html__( 'Menu Order', 'tranzix' ),
						'rand'       => esc_html__( 'Random', 'tranzix' ),
					),
				]
			);
			$this->add_control(
				'query_order',
				[
					'label'   => esc_html__( 'Order', 'tranzix' ),
					'type'    => Controls_Manager::SELECT,
					'default' => 'DESC',
					'options' => array(
						'DESc' => esc_html__( 'DESC', 'tranzix' ),
						'ASC'  => esc_html__( 'ASC', 'tranzix' ),
					),
				]
			);
			$this->add_control(
				'query_exclude',
				[
					'label'       => esc_html__( 'Exclude', 'tranzix' ),
					'type'        => Controls_Manager::TEXT,
					'description' => esc_html__( 'Exclude posts, pages, etc. by ID with comma separated.', 'tranzix' ),
				]
			);
		
			$this->add_control(
				'query_category', 
					[
					  'type' => Controls_Manager::SELECT,
					  'label' => esc_html__('Category', 'tranzix'),
					  'options' => get_blog_categories()
					]
			);
			$this->add_control(
				'show_pagination',
				[
					'label' => __( 'Enable/Disable Pagination', 'tranzix' ),
					'type'     => Controls_Manager::SWITCHER,
					'dynamic'     => [
						'active' => true,
					],
					'placeholder' => __( 'Enable/Disable Pagination', 'tranzix' ),
				]
			);
	
		
		$this->end_controls_section();

	}

	/**
	 * Render button 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();
        
        $paged = tranzix_set($_POST, 'paged') ? esc_attr($_POST['paged']) : 1;

		$this->add_render_attribute( 'wrapper', 'class', 'templatepath-tranzix' );
		$args = array(
			'post_type'      => 'post',
			'posts_per_page' => tranzix_set( $settings, 'query_number' ),
			'orderby'        => tranzix_set( $settings, 'query_orderby' ),
			'order'          => tranzix_set( $settings, 'query_order' ),
			'paged'         => $paged
		);
		if ( tranzix_set( $settings, 'query_exclude' ) ) {
			$settings['query_exclude'] = explode( ',', $settings['query_exclude'] );
			$args['post__not_in']      = tranzix_set( $settings, 'query_exclude' );
		}
		if( tranzix_set( $settings, 'query_category' ) ) $args['category_name'] = tranzix_set( $settings, 'query_category' );
		$query = new \WP_Query( $args );

		if ( $query->have_posts() ) 
		{ ?>
			
		<section class=" news-section <?php echo esc_attr($settings['sec_class']);?>">
		
			<?php  if ( esc_url($settings['bgimg']['id']) ) : ?>
			<div class="pattern-layer" style="background-image:url(<?php echo wp_get_attachment_url($settings['bgimg']['id']);?>)">
			<?php else :?>	
			<div class="noimage">
			<?php endif;?>
			</div>
			<?php  if ( esc_url($settings['bgimg1']['id']) ) : ?>
			<div class="pattern-layer-two" style="background-image:url(<?php echo wp_get_attachment_url($settings['bgimg1']['id']);?>)">
			<?php else :?>	
			<div class="noimage">
			<?php endif;?>
			</div>
			
			<div class="auto-container">
				<?php if($settings['title']): ?>
				<div class="sec-title centered">
					<h2><?php echo $settings['title'];?></h2>
					<div class="separator"></div>
				</div>
				<?php endif; ?>
				<div class="row clearfix">
				
					<?php while ( $query->have_posts() ) : $query->the_post();
					$meta_image = get_post_meta( get_the_id(), 'meta_image', true );
					?>
				
					<div class="news-block col-lg-<?php echo esc_attr($settings['column'], true );?> col-md-6 col-sm-12">
						<div class="inner-box wow fadeInUp" data-wow-delay="0ms" data-wow-duration="1500ms">
							<div class="image">
								<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>">
									<?php the_post_thumbnail(); ?>
								</a>
							</div>
							<div class="lower-content">
							<ul class="post-info">
								<li><span class="icon flaticon-calendar"></span><a href="<?php echo get_day_link( get_the_time( 'Y' ), get_the_time( 'm' ), get_the_time( 'd' ) ); ?>"><?php echo get_the_date(); ?></a></li>
								<li><span class="icon far fa-comment"></span><?php comments_number(); ?></li>
							</ul>
							<h3><a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php the_title(); ?></a></h3></div>
						</div>
					</div>
					
					<?php endwhile; ?>
					
				</div>
			</div>
		</section>
		
        <?php }
		wp_reset_postdata();
	}

}