<?php
namespace Elementor; 

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

/**
 *  Blog Box
*/

class Anomica_Masonry_Portfoliobox_Widget extends Widget_Base{

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

	public function get_title() {
		return esc_attr__( 'Masonry Portfolio Box Element', 'anomica' );
	}
	
	public function get_icon() {
		return 'eicon-gallery-grid';
	}

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

	public function __construct($data = [], $args = null) {
		parent::__construct($data, $args);
	}

	protected function _register_controls() {

		$this->start_controls_section(
			'heading_section',
			[
				'label' => esc_attr__( 'General', 'anomica' ),
			]
		);
		
		$this->add_control(
			'style',
			[
				'label'			=> esc_attr__( 'Select Portfolio Style', 'anomica' ),
				'description'	=> esc_attr__( 'Select Portfolio style.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'label_block'	=> true,
				'options' => [
					'style1'	=> esc_attr( 'PortfolioBox Style 1' ),
				],
				'default' => esc_attr( 'style1' ),
			]
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'data_section',
			[
				'label' => esc_attr__( 'Box Design', 'anomica' ),
			]
		);
		
		$this->add_control(
			'from_category',
			[
				'label' => esc_attr__( 'From Category ?', 'anomica' ),
				'type' => Controls_Manager::SELECT2,
				'options' => $this->select_category(),
				'multiple' => true,
				'label_block'	=> true,
				'placeholder' => esc_attr__( 'All Categories', 'anomica' ),
			]
		);
		
		$this->add_control(
			'show',
			[
				'label' => esc_attr__( 'Post to show', 'anomica' ),
				'description' => esc_attr__( 'How many item you want to show.', 'anomica' ),
				'separator' => 'before',
				'type' => Controls_Manager::NUMBER,
				'default' => '3',
			]
		);
		
		$this->add_control(
			'sortable',
			[
				'label' => esc_attr__( 'Show Sortable Category Links', 'anomica' ),
				'description' => esc_attr__( 'Show sortable category links above items so user can sort by just single click.', 'anomica' ),
				'type' => Controls_Manager::SWITCHER,
				'label_on' => esc_attr__( 'Yes', 'anomica' ),
				'label_off' => esc_attr__( 'No', 'anomica' ),
				'return_value' => 'yes',
				'default' => '',
			]
		);
		
		$this->add_control(
			'allword',
			[
				'label' => esc_attr__( 'Replace ALL word', 'anomica' ),
				'type' => Controls_Manager::TEXTAREA,
				'dynamic' => [
					'active' => true,
				],
				'default' => esc_attr__( 'All', 'anomica' ),
				'placeholder' => esc_attr__( 'Replace ALL word in sortable group links. Default is ALL word.', 'anomica' ),
				'label_block' => true,
				'condition' => [
					'sortable' => 'yes',
				],
			]
		);

		$this->add_control(
			'order',
			[
				'label' => esc_attr__( 'Order', 'anomica' ),
				'description' => esc_attr__( 'Designates the ascending or descending order.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'separator' => 'before',
				'default' => 'DESC',
				'options' => [
					'ASC'		=> esc_attr__( 'Ascending order (1, 2, 3; a, b, c)', 'anomica' ),
					'DESC'		=> esc_attr__( 'Descending order (3, 2, 1; c, b, a)', 'anomica' ),
				]
			]
		);
		
		$this->add_control(
			'orderby',
			[
				'label' => esc_attr__( 'Order By', 'anomica' ),
				'description' => esc_attr__( ' Sort retrieved posts by parameter.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'default' => 'DESC',
				'options' => [
					'none'		=> esc_attr__( 'No order', 'anomica' ),
					'ID'		=> esc_attr__( 'ID', 'anomica' ),
					'title'		=> esc_attr__( 'Title', 'anomica' ),
					'date'		=> esc_attr__( 'Date', 'anomica' ),
					'rand'		=> esc_attr__( 'Random', 'anomica' ),
				]
			]
		);
		
		$this->add_control(
			'gap',
			[
				'label' => esc_attr__( 'Box Gap', 'anomica' ),
				'description' => esc_attr__( 'Gap between each Post box.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'default' => '30px',
				'options' => [
					'0px'		=> esc_attr__( 'No Gap (0px)', 'anomica' ),
					'5px'		=> esc_attr__( '5px', 'anomica' ),
					'10px'		=> esc_attr__( '10px', 'anomica' ),
					'15px'		=> esc_attr__( '15px', 'anomica' ),
					'20px'		=> esc_attr__( '20px', 'anomica' ),
					'25px'		=> esc_attr__( '25px', 'anomica' ),
					'30px'		=> esc_attr__( '30px', 'anomica' ),
					'35px'		=> esc_attr__( '35px', 'anomica' ),
					'40px'		=> esc_attr__( '40px', 'anomica' ),
					'45px'		=> esc_attr__( '45px', 'anomica' ),
					'50px'		=> esc_attr__( '50px', 'anomica' ),
				]
			]
		);
		$this->end_controls_section();
		
		$this->start_controls_section(
			'appearance_section',
			[
				'label' => esc_attr__( 'Box Design', 'anomica' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			]
		);
	
		$this->add_control(
			'column',
			[
				'label'			=> esc_attr__( 'Select Column', 'anomica' ),
				'description'	=> esc_attr__( 'Select column.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'label_block'	=> true,
				'default'		=> 'three',
				'options'		=> [
					'three'				=> esc_attr__( 'Three Column', 'anomica' ),
					'four'				=> esc_attr__( 'Four Column', 'anomica' ),
					'five'				=> esc_attr__( 'Five Column', 'anomica' ),
				],
			]
		);
		
		$this->add_control(
			'view-type',
			[
				'label'			=> esc_attr__( 'Box View', 'anomica' ),
				'description'	=> esc_attr__( 'Select box view. Show as normal row and column or show with carousel effect.', 'anomica' ),
				'type' => Controls_Manager::SELECT,
				'label_block'	=> true,
				'default'		=> 'default',
				'options'		=> [
					'default'	=> esc_attr__( 'Row and Column', 'anomica' ),
				]
			]
		);


		$this->end_controls_section();

	}

	protected function render() {

		$settings	= $this->get_settings_for_display();
		extract($settings);

		$start_div = ttm_element_container( array(
			'position'	=> 'start',
			'cpt'		=> 'portfolio',
			'data'		=> $settings
		) );

		echo themetechmount_wp_kses($start_div);

		$args = array(
			'post_type'				=> 'tm_portfolio',
			'posts_per_page'		=> $show,
			'ignore_sticky_posts'	=> true,
		);

		if( !empty($orderby) && $orderby!='none' ){
			$args['orderby'] = $orderby;
			if( !empty($order) ){
				$args['order'] = $order;
			}
		}

		if( !empty($from_category) ){
			$args['tax_query'] = array(
				array(
					'taxonomy' => 'tm_portfolio_category',
					'field'    => 'slug',
					'terms'    => $from_category,
				),
			);
		};

		$posts = new \WP_Query( $args );
		
		$headingclass= '';
		if( empty($settings['heading']) ){
			$headingclass = 'tm-boxwithout-heading';
		}

		if ( $posts->have_posts() ) {
			?>

			<div class="themetechmount-box-heading-wrapper">
				<?php
				if( function_exists('themetechmount_get_filterbutton') ){
					$sortable_category_html = themetechmount_get_filterbutton( $settings, 'tm_portfolio_category' );
					echo  themetechmount_wp_kses($sortable_category_html);
				}
				?>
			</div>

			<div class="themetechmount-boxes-row-wrapper row multi-columns-row tm-box-masnory">

			<?php
			$x = 1;
		
			while ( $posts->have_posts() ) {
				$return = '';
				$posts->the_post();

			global $tm_global_sc_loop_count;
			$tm_global_sc_loop_count = $x;
			
				if( file_exists( locate_template( '/template-parts/portfoliobox-masonry/portfoliobox-'.esc_attr($style).'.php', false, false ) ) ){

					$return .= ttm_element_block_container( array(
						'position'	=> 'start',
						'column'	=> $column,
						'cpt'		=> 'portfolio',
						'taxonomy'	=> 'tm_portfolio_category',
						'style'		=> $style,
					) );
					wp_enqueue_style( 'anomica-portfolio' );
					ob_start();
					$r = include( locate_template( '/template-parts/portfoliobox-masonry/portfoliobox-'.esc_attr($style).'.php', false, false ) );
					$return .= ob_get_contents();
					ob_end_clean();

					$return .= ttm_element_block_container( array(
						'position'	=> 'end',
					) );

				}

				echo trim($return);
				$x++;
			}
			?>

			</div>

			<?php
		}

	    wp_reset_postdata();
		
		$end_div = ttm_element_container( array(
			'position'	=> 'end',
			'cpt'		=> 'portfolio',
			'data'		=> $settings
		) );
		echo themetechmount_wp_kses($end_div);
	}

	protected function _content_template() {}
	

	protected function select_category() {
	  	$category = get_terms( array( 'taxonomy' => 'tm_portfolio_category', 'hide_empty' => false ) );
	  	$cat = array();
	  	foreach( $category as $item ) {
			$cat_count = get_category( $item );

	     	if( $item ) {
	        	$cat[$item->slug] = $item->name . ' ('.$cat_count->count.')';
	     	}
	  	}
	  	return $cat;
	}
	
}
// Register widget
Plugin::instance()->widgets_manager->register( new Anomica_Masonry_Portfoliobox_Widget() );