<?php
class Widget_Properties_With_Map extends \Elementor\Widget_Base {

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

	public function get_title() {
		return esc_html__( 'TF Properties Type With Map', 'tf-real-estate' );
	}

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

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

	public function get_keywords() {
		return [ 'properties', 'list' ];
	}

	public function get_style_depends() {
		return [ 'owl-carousel','map-css', 'properties-styles' ];
	}

	public function get_script_depends() {
		return [ 'owl-carousel','mapbox-js','ggmap-js','properties-js', 'properties-script' ];
	}

	protected function register_controls() {
		// Start Properties Query        
		$this->start_controls_section(
			'section_properties_query',
			[ 
				'label' => esc_html__( 'Query', 'tf-real-estate' ),
			]
		);

		

		$this->add_control(
			'property-type',
			[ 
				'label'       => esc_html__( 'Property Type', 'tf-real-estate' ),
				'type'        => \Elementor\Controls_Manager::SELECT2,
				'options'     => tfre_get_taxonomies( 'property-type' ),
				'label_block' => true,
				'multiple'    => true,
			]
		);

		
		$this->add_control(
			'show_label',
			[ 
				'label'        => esc_html__( 'Show Label', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->add_control(
			'show_label_bottom',
			[ 
				'label'        => esc_html__( 'Show Label Bottom', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->add_control(
			'show_address',
			[ 
				'label'        => esc_html__( 'Show Address', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_price',
			[ 
				'label'        => esc_html__( 'Show Price', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_bedrooms',
			[ 
				'label'        => esc_html__( 'Show Bedrooms', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->add_control(
			'show_bathrooms',
			[ 
				'label'        => esc_html__( 'Show Bathrooms', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->add_control(
			'show_size',
			[ 
				'label'        => esc_html__( 'Show Sizes', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->add_control(
			'show_rooms',
			[ 
				'label'        => esc_html__( 'Show Rooms', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
			]
		);

		$this->add_control(
			'show_land_area',
			[ 
				'label'        => esc_html__( 'Show Land Area', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
			]
		);


		$this->add_control(
			'show_garages',
			[ 
				'label'        => esc_html__( 'Show Garages', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
			]
		);

		$this->add_control(
			'show_garages_size',
			[ 
				'label'        => esc_html__( 'Show Garages Size', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
			]
		);

		$this->add_control(
			'show_agent',
			[ 
				'label'        => esc_html__( 'Show Agent', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'show_action',
			[ 
				'label'        => esc_html__( 'Show Action', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'Show', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Hide', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes'
			]
		);

		$this->end_controls_section();
		// /.End Properties Query

		
	// Start Carousel        
		$this->start_controls_section(
			'section_properties_carousel',
			[ 
				'label' => esc_html__( 'Carousel', 'tf-real-estate' ),
			]
		);

		$this->add_control(
			'carousel',
			[ 
				'label'        => esc_html__( 'Carousel', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Off', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'yes',
			]
		);

		$this->add_control(
			'carousel_loop',
			[ 
				'label'        => esc_html__( 'Loop', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Off', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'condition'    => [ 
					'carousel' => 'yes',
				],
			]
		);

		$this->add_control(
			'carousel_auto',
			[ 
				'label'        => esc_html__( 'Auto Play', 'tf-real-estate' ),
				'type'         => \Elementor\Controls_Manager::SWITCHER,
				'label_on'     => esc_html__( 'On', 'tf-real-estate' ),
				'label_off'    => esc_html__( 'Off', 'tf-real-estate' ),
				'return_value' => 'yes',
				'default'      => 'no',
				'condition'    => [ 
					'carousel' => 'yes',
				],
			]
		);

		$this->add_control(
			'carousel_spacing',
			[ 
				'label'   => esc_html__( 'Spacing', 'tf-real-estate' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'default' => '10',
			]
		);

		$this->add_control(
			'carousel_column_desk',
			[ 
				'label'     => esc_html__( 'Columns Desktop', 'tf-real-estate' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => '12',
				'options'   => [ 
					'1' => esc_html__( '1', 'tf-real-estate' ),
					'2' => esc_html__( '2', 'tf-real-estate' ),
					'3' => esc_html__( '3', 'tf-real-estate' ),
					'4' => esc_html__( '4', 'tf-real-estate' ),
					'5' => esc_html__( '5', 'tf-real-estate' ),
					'6' => esc_html__( '6', 'tf-real-estate' ),
					'7' => esc_html__( '7', 'tf-real-estate' ),
					'8' => esc_html__( '8', 'tf-real-estate' ),
					'9' => esc_html__( '9', 'tf-real-estate' ),
					'10' => esc_html__( '10', 'tf-real-estate' ),
					'11' => esc_html__( '11', 'tf-real-estate' ),
					'12' => esc_html__( '12', 'tf-real-estate' ),
				],
				'condition' => [ 
						'carousel' => 'yes',
					],
			]
		);

		$this->add_control(
			'carousel_column_laptop',
			[ 
				'label'     => esc_html__( 'Columns Small Desktop', 'tf-real-estate' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => '5',
				'options'   => [ 
					'1' => esc_html__( '1', 'tf-real-estate' ),
					'2' => esc_html__( '2', 'tf-real-estate' ),
					'3' => esc_html__( '3', 'tf-real-estate' ),
					'4' => esc_html__( '4', 'tf-real-estate' ),
					'5' => esc_html__( '5', 'tf-real-estate' ),
					'6' => esc_html__( '6', 'tf-real-estate' ),
					'7' => esc_html__( '7', 'tf-real-estate' ),
					'8' => esc_html__( '8', 'tf-real-estate' ),
					'9' => esc_html__( '9', 'tf-real-estate' ),
					'10' => esc_html__( '10', 'tf-real-estate' ),
					'11' => esc_html__( '11', 'tf-real-estate' ),
					'12' => esc_html__( '12', 'tf-real-estate' ),
				],
				'condition' => [ 
						'carousel' => 'yes',
					],
			]
		);

		$this->add_control(
			'carousel_column_tablet',
			[ 
				'label'     => esc_html__( 'Columns Tablet', 'tf-real-estate' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => '4',
				'options'   => [ 
					'1' => esc_html__( '1', 'tf-real-estate' ),
					'2' => esc_html__( '2', 'tf-real-estate' ),
					'3' => esc_html__( '3', 'tf-real-estate' ),
					'4' => esc_html__( '4', 'tf-real-estate' ),
					'5' => esc_html__( '5', 'tf-real-estate' ),
					'6' => esc_html__( '6', 'tf-real-estate' ),
					'7' => esc_html__( '7', 'tf-real-estate' ),
					'8' => esc_html__( '8', 'tf-real-estate' ),
					'9' => esc_html__( '9', 'tf-real-estate' ),
					'10' => esc_html__( '10', 'tf-real-estate' ),
					'11' => esc_html__( '11', 'tf-real-estate' ),
				],
				'condition' => [ 
					'carousel' => 'yes',
				],
			]
		);

		$this->add_control(
			'carousel_column_mobile',
			[ 
				'label'     => esc_html__( 'Columns Mobile', 'tf-real-estate' ),
				'type'      => \Elementor\Controls_Manager::SELECT,
				'default'   => '2',
				'options'   => [ 
					'1' => esc_html__( '1', 'tf-real-estate' ),
					'2' => esc_html__( '2', 'tf-real-estate' ),
					'3' => esc_html__( '3', 'tf-real-estate' ),
					'4' => esc_html__( '4', 'tf-real-estate' ),
					'5' => esc_html__( '5', 'tf-real-estate' ),
					'6' => esc_html__( '6', 'tf-real-estate' ),
					'7' => esc_html__( '7', 'tf-real-estate' ),
					'8' => esc_html__( '8', 'tf-real-estate' ),
					'9' => esc_html__( '9', 'tf-real-estate' ),
					'10' => esc_html__( '10', 'tf-real-estate' ),
					'11' => esc_html__( '11', 'tf-real-estate' ),
				],
				'condition' => [ 
					'carousel' => 'yes',
				],
			]
		);

		$this->end_controls_section();
	// /.End Carousel	

		// Start Layout Style
		$this->start_controls_section(
			'section_layout_style',
			[ 
				'label' => esc_html__( 'Layout Style', 'tf-real-estate' ),
			]
		);

		$this->add_control(
			'layout_grid',
			[ 
				'label'   => esc_html__( 'Columns', 'tf-real-estate' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'column-3',
				'options' => [ 
					'column-2' => esc_html__( '2', 'tf-real-estate' ),
					'column-3' => esc_html__( '3', 'tf-real-estate' ),
					'column-4' => esc_html__( '4', 'tf-real-estate' ),
				],
	
			]
		);

		

		$this->add_responsive_control(
			'taxonomy_tabs_align',
			[ 
				'label'       => esc_html__( 'Taxonomy Tabs Alignment', 'tf-real-estate' ),
				'type'        => \Elementor\Controls_Manager::CHOOSE,
				'label_block' => true,
				'options'     => [ 
					'flex-start' => [ 
						'title' => esc_html__( 'Left', 'tf-real-estate' ),
						'icon'  => 'eicon-text-align-left',
					],
					'center'     => [ 
						'title' => esc_html__( 'Center', 'tf-real-estate' ),
						'icon'  => 'eicon-text-align-center',
					],
					'flex-end'   => [ 
						'title' => esc_html__( 'Right', 'tf-real-estate' ),
						'icon'  => 'eicon-text-align-right',
					],
				],
				'default'     => 'center',
				'toggle'      => true,
				'selectors'   => [ 
					'{{WRAPPER}} .tf-properties-wrap .wrap-properties-post .filter-bar' => 'justify-content: {{VALUE}};',
				],
				'condition'   => [ 
					'taxonomy_tabs' => 'yes',
					'carousel' => 'no',
				],
			]
		);


		$this->end_controls_section();
		// /.End Layout Style




		
	}

	protected function render( $instance = [] ) {

		$settings       = $this->get_settings_for_display();
		$taxonomy_query = array();
		$has_carousel = ( $settings['carousel'] == 'yes' ) ? 'taxo-carousel' : '';

		if ( get_query_var( 'paged' ) ) {
			$paged = get_query_var( 'paged' );
		} elseif ( get_query_var( 'page' ) ) {
			$paged = get_query_var( 'page' );
		} else {
			$paged = 1;
		}
		$query_args = array(
			'post_type'      => 'real-estate',
			'post_status'    => 'publish',
			'paged'          => $paged
		);

		if ( ! empty( $settings['property-type'] ) ) {
			$taxonomy_query[] = array(
				'taxonomy' => 'property-type',
				'field'    => 'slug',
				'terms'    => $settings['property-type']
			);
		}
		if ( count( $taxonomy_query ) > 0 ) {
			$query_args['tax_query'] = array(
				'relation' => 'AND',
				$taxonomy_query
			);
		}


		// }

		$query = new WP_Query( $query_args );

		/* Taxonomy Tabs */
		$show_filter_tabs = 'show_filter_tabs';

		/* End Taxonomy Tabs */

		/* Style & Layout */
		$style_layout = 'map';
		$style_layout_column = $settings['layout_grid'];

		$rtl_carousel = '';
		if( is_rtl() ){
			$rtl_carousel = 'yes';
		}


		$this->add_render_attribute( 'tf_properties_wrap', [ 'id' => "tf-properties-{$this->get_id()}", 'class' => [ 'tf-properties-wrap type-with-map grid1', 'themesflat-properties-taxonomy', $style_layout, $has_carousel, $show_filter_tabs ], 'data-tabid' => $this->get_id() ] );

		if ( $query->have_posts() ) : ?>
			<div <?php echo $this->get_render_attribute_string( 'tf_properties_wrap' ); ?>>
				<div class="wrap-properties-post <?php echo esc_attr( $style_layout_column ); ?>">
					<div class="filter-bar">
					<?php if ( $settings['carousel'] == 'yes' ) : ?>
					<div class="owl-carousel" data-loop="<?php echo esc_attr( $settings['carousel_loop'] ); ?>"
						data-auto="<?php echo esc_attr( $settings['carousel_auto'] ); ?>"
						data-column="<?php echo esc_attr( $settings['carousel_column_desk'] ); ?>"
						data-column2="<?php echo esc_attr( $settings['carousel_column_tablet'] ); ?>"
						data-column3="<?php echo esc_attr( $settings['carousel_column_mobile'] ); ?>"
						data-spacing="<?php echo esc_attr( $settings['carousel_spacing'] ) ?>"
						data-rtl="<?php echo esc_attr( $rtl_carousel ) ?>"
						data-bullets="false">
					<?php endif; ?>
					<?php 
						$taxonomy_selected = 'property-type';
						$taxonomies        = $settings[ $taxonomy_selected ];						
						if ( is_array( $taxonomies ) ) {
							foreach ( $taxonomies as $key => $tax ) {
								$term = get_term_by( 'slug', $tax, $taxonomy_selected );
								if ( $term ) {
									$args_tab_tax             = array(
										'post_type'      => 'real-estate',
										'post_status'    => 'publish',
										'tax_query'      => array(
											array(
												'taxonomy' => $taxonomy_selected,
												'field'    => 'slug',
												'terms'    => $term->slug
											)
										),
									);
									$term_icon_id    = get_term_meta($term->term_id , 'type_icon', true );
									$svgImage = !empty(wp_get_attachment_image_src( $term_icon_id, 'full' )[0]) ? wp_get_attachment_image_src( $term_icon_id, 'full' )[0] : '' ;
									$imgExts = array("svg");
									$urlExt = pathinfo($svgImage, PATHINFO_EXTENSION);
									
									$query_properties_tab_tax = new WP_Query( $args_tab_tax );
									
									?>
									
									<a class="filter-properties hv-tool" data-slug="<?php echo esc_attr( $term->slug ) ?>">
										<span class="feature-image">
											<?php
												if(!empty( $term_icon_id ) && in_array($urlExt, $imgExts)) {
													echo file_get_contents($svgImage);
												}else {
													echo sprintf( '<img loading="lazy" class="image-taxonomy" src="%s" alt="image" />', empty( $term_icon_id ) ? TF_PLUGIN_URL . "includes/elementor-widget/assets/images/no-image.jpg" : wp_get_attachment_image_src( $term_icon_id, 'full' )[0] );
												}
											?>
										</span>
										<?php echo esc_html( $term->name ); ?>
									</a>
									
									<?php
								}
							}
						}
						echo '</div>';
					?>
					<?php if ( $settings['carousel'] == 'yes' ) : ?>
						</div>
					<?php endif; ?>
					<div class="content-inner">
						<div class="content-left content-tab-inner-none"  >

							<div class="content-tab">
								
									<?php $taxonomy_selected = 'property-type';
									$taxonomies        = $settings[ $taxonomy_selected ];
									if ( is_array( $taxonomies ) ) {
										foreach ( $taxonomies as $key => $tax ) {
											$term = get_term_by( 'slug', $tax, $taxonomy_selected );
											if ( $term ) {
												$args_tab_tax             = array(
													'post_type'      => 'real-estate',
													'post_status'    => 'publish',
													'posts_per_page' => -1,
													'tax_query'      => array(
														array(
															'taxonomy' => $taxonomy_selected,
															'field'    => 'slug',
															'terms'    => $term->slug
														)
													),
												);
												$query_properties_tab_tax = new WP_Query( $args_tab_tax );
												?>
												<div class="content-tab-inner tab-inner-<?php echo esc_attr( $term->slug ); ?>" style="display:none">
													
														<div class="properties row">
															<?php if ( $query_properties_tab_tax->have_posts() == '') {
																echo '<h6 class="no-item">' . esc_html__( 'No item', 'tf-real-estate' ) . '</h6>'; } else { ?>
															<?php while ( $query_properties_tab_tax->have_posts() ) :
																$query_properties_tab_tax->the_post(); ?>
																<?php
																$attr['settings']     = $settings;
																$attr['elementor_id'] = $this->get_id();
																tfre_get_template_widget_elementor( "templates/property/{$style_layout}", $attr );
																?>
																
															<?php endwhile; } ?>
															<?php wp_reset_postdata(); ?>
															
														</div>
														
													
												</div>

										<?php }
										}
									} ?>

									
							</div>
						</div>
						<div class="content-right">
							<div class="map-container">
								<div id="map" class=""></div>
							</div>
						</div>
					</div>
					<div class="fixed-map-stopper"></div>
					<?php wp_reset_postdata(); ?>
				</div>
				<?php
				tfre_get_template_with_arguments( 'global/property-quick-view-modal-elementor.php', array( 'elementor_id' => $this->get_id() ) );
				?>
			</div>

			<?php
		else :
			esc_html_e( 'No properties found', 'tf-real-estate' );
		endif;
	}
}