<?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\Utils;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Plugin;

/**
 * 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 Portfolios 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_portfolios';
	}

	/**
	 * Get widget title.
	 * Retrieve button widget title.
	 *
	 * @since  1.0.0
	 * @access public
	 * @return string Widget title.
	 */
	public function get_title() {
		return esc_html__( 'Portfolios', '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(
			'portfolios',
			[
				'label' => esc_html__( 'portfolios', 'tranzix' ),
			]
		);
		$this->add_control(
            'style', 
				[
					'label'   => esc_html__( 'Choose Different Style', 'tranzix' ),
					'label_block' => true,
					'type'    => Controls_Manager::SELECT,
					'default' => 'style1',
					'options' => array(
						'style2' => esc_html__( 'Choose Style 2', 'tranzix' ),
					),
				]
		);
		$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'    => 'style3',
						),
						array(
							'name'     => 'style',
							'operator' => '==',
							'value'    => 'style4',
						),
					),
				),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your Sub title', 'rashid' ),
			]
		);
		$this->add_control(
			'title',
			[
				'label'       => __( 'Title', 'elementor' ),
				'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',
						),
					),
				),
				'type'        => Controls_Manager::TEXTAREA,
				'dynamic'     => [
					'active' => true,
				],
				'placeholder' => __( 'Enter your title', 'elementor' ),
				
			]
		);
		$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->add_control(
			'number',
			[
				'label'   => esc_html__( 'Number of post', 'tranzix' ),
				'type'    => Controls_Manager::NUMBER,
				'default' => 3,
				'min'     => 1,
				'max'     => 100,
				'step'    => 1,
			]
		);
		$this->add_control(
			'cat_exclude',
			[
				'label'       => esc_html__( 'Exclude', 'tranzix' ),
				'type'        => Controls_Manager::TEXT,
				'description' => esc_html__( 'Exclude categories, etc. by ID with comma separated.', '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_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->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'      => 'tranzix_project',
			'posts_per_page' => tranzix_set( $settings, 'number' ),
			'orderby'        => tranzix_set( $settings, 'query_orderby' ),
			'order'          => tranzix_set( $settings, 'query_order' ),
			'paged'         => $paged
		);
		$terms_array = explode(",",tranzix_set( $settings, 'cat_exclude' ));
		if(tranzix_set( $settings, 'cat_exclude' )) $args['tax_query'] = array(array('taxonomy' => 'project_cat','field' => 'id','terms' => $terms_array,'operator' => 'NOT IN',));
		$allowed_tags = wp_kses_allowed_html('post');
		$query = new \WP_Query( $args );
		$t = '';
		$data_filtration = '';
		$data_posts = '';
		if ( $query->have_posts() ) 
		{
		ob_start();
		?>
  
		<?php 
            $count = 0; 
            $fliteration = array();
            while( $query->have_posts() ): $query->the_post();
            global  $post;
            $meta = ''; //printr($meta);
            $meta1 = ''; //_WSH()->get_meta();
            $post_terms = get_the_terms( get_the_id(), 'project_cat');// printr($post_terms); exit();
            foreach( (array)$post_terms as $pos_term ) $fliteration[$pos_term->term_id] = $pos_term;
            $temp_category = get_the_term_list(get_the_id(), 'project_cat', '', ', ');
            
            $post_terms = wp_get_post_terms( get_the_id(), 'project_cat'); 
            $term_slug = '';
            if( $post_terms ) foreach( $post_terms as $p_term ) $term_slug .= $p_term->slug.' ';
        
            ?>
			
		
			<?php  if ( 'style2' === $settings['style'] ) : ?>
			
			<div class="col-lg-<?php echo esc_attr($settings['column'], true );?> col-md-6 col-sm-12 masonry-item small-column all <?php echo esc_attr($term_slug); ?>">
				<div class="project-block-two">
					<div class="inner-box p_relative d_block mb_30">
						<figure class="image-box p_relative d_block">
							<?php the_post_thumbnail(); ?>
						</figure>
						<div class="content-box p_absolute l_30 pt_13 pr_20 pb_14 pl_20 tran_5">
							<h4 class="d_block fs_20 lh_30 font_family_spartan"><a href="<?php echo esc_url(get_post_meta( get_the_id(), 'page_link', true ));?>" class="p_relative d_iblock"><?php the_title(); ?></a></h4>
							<p class="font_family_oxygen"><?php echo (get_post_meta( get_the_id(), 'meta_subtitle', true ));?></p>
							<div class="link p_absolute t_30 r_20"><a href="<?php echo esc_url(get_post_meta( get_the_id(), 'page_link', true ));?>" class="p_relative d_iblock fs_14"><i class="icon-4"></i></a></div>
						</div>
					</div>
				</div>
			</div>
			
			<?php endif;?>
			

            <?php endwhile;?>

            <?php wp_reset_postdata();
            $data_posts = ob_get_contents();
            ob_end_clean();
            
            ob_start();?>
            
            <?php $terms = get_terms(array('project_cat')); ?>
		
			<?php  if ( 'style2' === $settings['style'] ) : ?>
			
			<section class="project-four p_relative pb_120 <?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="sortable-masonry">
						<div class="filters mb_50">
							<ul class="filter-tabs filter-btns clearfix centred">
								<li class="active filter" data-role="button" data-filter=".all"><?php esc_attr_e('All', 'tranzix');?></li>
								<?php foreach( $fliteration as $t ): ?>
								<li class="filter" data-role="button" data-filter=".<?php echo esc_attr(tranzix_set( $t, 'slug' )); ?>"><?php echo tranzix_set( $t, 'name'); ?></li>
								<?php endforeach;?>
							</ul>
						</div>
						<div class="items-container row clearfix">
						   
						   <?php echo $data_posts; ?>
							
						</div>
					</div>
				</div>
			</section>
			
			<?php endif;?>
			
    	<?php }
	}

}