<?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 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_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__( '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(
			'blog',
			[
				'label' => esc_html__( 'Blog', 'tranzix' ),
			]
		);
		$this->add_control(
            'style', 
				[
					'label'   => esc_html__( 'Choose Different Style', 'tranzix' ),
					'label_block' => true,
					'type'    => Controls_Manager::SELECT,
					'default' => 'style1',
					'options' => array(
						'style8' => esc_html__( 'Choose Style8', 'tranzix' ),
						'style9' => esc_html__( 'Choose Style9', 'tranzix' ),
					),
				]
		);
			$this->add_control(
            'thumb', 
				[
					'label'   => esc_html__( 'Choose Post Image', 'tranzix' ),
					'label_block' => true,
					'type'    => Controls_Manager::SELECT,
					'default' => 'style1',
					'options' => array(
						'style1' => esc_html__( 'Meta Box Image', 'tranzix' ),
						'style2' => esc_html__( 'Dafult Thumbnail', 'tranzix' ),
					),
				]
		);
		$this->add_control(
			'bgimg',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style4',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style15',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
					),
				),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'bgimg1',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style15',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
					),
				),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'bgimg2',
			[
				'label' => esc_html__('Background image', 'rashid'),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
					),
				),
				'type' => Controls_Manager::MEDIA,
				'default' => ['url' => Utils::get_placeholder_image_src(),],
			]
		);
		$this->add_control(
			'subtitle',
			[
				'label'       => __( 'Sub Title', 'rashid' ),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style1',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style2',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style3',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style4',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style5',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style6',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style7',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style8',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style10',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style11',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style12',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style13',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style14',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style15',
						),
					),
				),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Sub title', 'rashid' ),
			]
		);
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'rashid' ),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style1',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style2',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style3',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style4',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style5',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style6',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style7',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style8',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style9',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style10',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style11',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style12',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style13',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style14',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
					),
				),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your title', 'rashid' ),
			]
		);
		$this->add_control(
			'text',
			[
				'label'       => __( 'Description Text', 'rashid' ),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style1',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style2',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style5',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style7',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style8',
						),
					),
				),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Description', 'rashid' ),
			]
		);
		$this->add_control(
			'bttn',
			[
				'label'       => __( 'Button', 'rashid' ),
				'conditions' => array(
					'relation' => 'or',
					'terms'    => array(
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style1',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style2',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style3',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style4',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style5',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style6',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style7',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style8',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style9',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style11',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style12',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style16',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style18',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style19',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style20',
						),
					),
				),
				'type'        => Controls_Manager::TEXT,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Button 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() ) 
		{ ?>
	
			<?php  if ( 'style8' === $settings['style'] ) : ?>
		
		<section class="news-eight p_relative pt_140 pb_60 <?php echo esc_attr($settings['sec_class']);?>">
            <div class="auto-container">
				<?php if($settings['title']): ?>
                <div class="upper-box p_relative d_block mb_45">
                    <div class="row clearfix">
                        <div class="col-lg-6 col-md-12 col-sm-12 title-column">
                            <div class="sec-title-eight p_relative d_block">
                                <h5 class="p_relative d_iblock fs_18 lh_30 fw_sbold font_family_poppins mb_12"><?php echo $settings['subtitle'];?></h5><br />
                                <h2 class="d_block fs_50 fw_bold lh_50 fw_bold font_family_pt"><?php echo $settings['title'];?></h2>
                            </div>
                        </div>
                        <div class="col-lg-6 col-md-12 col-sm-12 text-column">
                            <div class="text p_relative d_block mt_35 pl_100">
                                <p class="fs_17 font_family_poppins fw_medium lh_28"><?php echo $settings['text'];?></p>
                            </div>
                        </div>
                    </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="col-lg-<?php echo esc_attr($settings['column'], true );?> col-md-6 col-sm-12 news-block">
                        <div class="news-block-one wow fadeInUp animated" data-wow-delay="00ms" data-wow-duration="1500ms">
                            <div class="inner-box p_relative d_block">
                                <div class="image-box p_relative d_block">
                                    <figure class="image p_relative d_block">
											<?php  if ( 'style1' === $settings['thumb'] ) : ?>
									<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><img src="<?php echo wp_get_attachment_url($meta_image['id']);?>" alt="" /></a>
									<?php endif; ?> 
									<?php  if ( 'style2' === $settings['thumb'] ) : ?>      
									<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php  the_post_thumbnail();    ?></a>
									<?php endif; ?> 
									</figure>
                                    <div class="post-date-two p_absolute l_30 t_30 w_55 centred pt_10 pb_10"><h4 class="fs_20 font_family_oxygen fw_bold lh_20"><?php echo get_the_date('d'); ?> <span class="d_block"><?php echo get_the_date('M'); ?></span></h4></div>
                                </div>
                                <div class="lower-content p_relative d_block pt_25">
                                    <h4 class="d_block fs_24 lh_30 mb_10 fw_bold font_family_pt"><a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php the_title(); ?></a></h4>
                                    <ul class="post-info clearfix p_relative d_block mb_15">
                                        <li class="p_relative d_iblock float_left mr_30 fs_16 font_family_poppins"><a href="<?php echo esc_url(get_author_posts_url( get_the_author_meta('ID') )); ?>"><?php the_author(); ?></a></li>
                                        <li class="p_relative d_iblock float_left fs_16 font_family_poppins"><?php comments_number(); ?></li>
                                    </ul>
                                    <p class="fs_16 d_block font_family_poppins mb_25"><?php echo tranzix_trim(get_the_content(), $settings['text_limit']); ?></p>
									<?php if($settings['bttn']): ?>
                                    <div class="btn-box">
                                        <a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>" class="theme-btn"><?php echo $settings['bttn'];?></a>
                                    </div>
									<?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
					
					<?php endwhile; ?>
					
					
                </div>
				
				<?php if($settings['show_pagination']){ ?>
				<div class="pagination-wrapper p_relative d_block pt_40">
					<?php tranzix_the_pagination2(array('total'=>$query->max_num_pages, 'next_text' => '<i class="fa fa-long-arrow-right"></i> ', 'prev_text' => '<i class=" fa fa-long-arrow-left"></i>')); ?>
				</div>
				<?php } ?>
				
            </div>
        </section>
		
		<?php endif;?>

		
		<?php  if ( 'style9' === $settings['style'] ) : ?>
		
		<section class="news-nine p_relative sec-pad <?php echo esc_attr($settings['sec_class']);?>">
            <div class="auto-container">
				<?php if($settings['title']): ?>
                <div class="sec-title p_relative d_block mb_50 centred">
                    <h2 class="d_block fs_50 fw_bold lh_55 fw_bold font_family_spartan"><?php echo $settings['title'];?></h2>
                </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="col-lg-<?php echo esc_attr($settings['column'], true );?> col-md-6 col-sm-12 news-block">
                        <div class="news-block-one wow fadeInUp animated" data-wow-delay="00ms" data-wow-duration="1500ms">
                            <div class="inner-box p_relative d_block b_shadow_6">
                                <div class="image-box p_relative d_block">
                                    <figure class="image p_relative d_block">
											<?php  if ( 'style1' === $settings['thumb'] ) : ?>
									<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><img src="<?php echo wp_get_attachment_url($meta_image['id']);?>" alt="" /></a>
									<?php endif; ?> 
									<?php  if ( 'style2' === $settings['thumb'] ) : ?>      
									<a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php  the_post_thumbnail();    ?></a>
									<?php endif; ?> 
									</figure>
                                    <div class="post-date-two p_absolute l_10 t_10 w_60 centred pt_10 pb_10"><h4 class="fs_20 font_family_oxygen fw_bold lh_20"><?php echo get_the_date('d'); ?> <span class="d_block"><?php echo get_the_date('M'); ?></span></h4></div>
                                </div>
                                <div class="lower-content p_relative d_block pt_40 pr_40 pb_50 pl_40">
                                    <ul class="post-info clearfix p_relative d_block mb_17">
                                        <li class="p_relative d_iblock float_left mr_30 fs_16 font_family_oxygen"><a href="<?php echo esc_url(get_author_posts_url( get_the_author_meta('ID') )); ?>"><?php the_author(); ?></a></li>
                                        <li class="p_relative d_iblock float_left fs_16 font_family_oxygen"><?php comments_number(); ?></li>
                                    </ul>
                                    <h4 class="d_block fs_24 lh_28 mb_15 fw_sbold font_family_spartan"><a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>"><?php the_title(); ?></a></h4>
                                    <p class="fs_16 d_block font_family_oxygen mb_25"><?php echo tranzix_trim(get_the_content(), $settings['text_limit']); ?></p>
									<?php if($settings['bttn']): ?>
                                    <div class="btn-box">
                                        <a href="<?php echo esc_url( the_permalink( get_the_id() ) );?>" class="theme-btn theme-btn-eight"><?php echo $settings['bttn'];?></a>
                                    </div>
									<?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
					
					
					<?php endwhile; ?>
					
                </div>
				
				<?php if($settings['show_pagination']){ ?>
				<div class="pagination-wrapper p_relative d_block pt_40">
					<?php tranzix_the_pagination2(array('total'=>$query->max_num_pages, 'next_text' => '<i class="fa fa-long-arrow-right"></i> ', 'prev_text' => '<i class=" fa fa-long-arrow-left"></i>')); ?>
				</div>
				<?php } ?>
				
            </div>
        </section>
		
		<?php endif;?>
		
		
		
        <?php }
		wp_reset_postdata();
	}

}