<?php
class Widget_Map_property extends \Elementor\Widget_Base {

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

	public function get_title() {
		return esc_html__( 'TF Map Property', '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(
			'properties_per_page',
			[ 
				'label'   => esc_html__( 'Properties Per Page', 'tf-real-estate' ),
				'type'    => \Elementor\Controls_Manager::NUMBER,
				'default' => '6',
			]
		);

		$this->add_control(
			'order_by',
			[ 
				'label'   => esc_html__( 'Order By', 'tf-real-estate' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'date',
				'options' => [ 
					'date'  => esc_html__( 'Date', 'tf-real-estate' ),
					'ID'    => esc_html__( 'Post ID', 'tf-real-estate' ),
					'title' => esc_html__( 'Title', 'tf-real-estate' ),
				],
			]
		);

		$this->add_control(
			'order',
			[ 
				'label'   => esc_html__( 'Order', 'tf-real-estate' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => 'desc',
				'options' => [ 
					'desc' => esc_html__( 'Descending', 'tf-real-estate' ),
					'asc'  => esc_html__( 'Ascending', 'tf-real-estate' ),
				],
			]
		);

		$this->add_control(
			'exclude',
			[ 
				'label'       => esc_html__( 'Exclude', 'tf-real-estate' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'description' => esc_html__( 'Post Ids Will Be Inorged. Ex: 1,2,3', 'tf-real-estate' ),
				'default'     => '',
				'label_block' => true,
			]
		);

		$this->add_control(
			'sort_by_id',
			[ 
				'label'       => esc_html__( 'Sort By ID', 'tf-real-estate' ),
				'type'        => \Elementor\Controls_Manager::TEXT,
				'description' => esc_html__( 'Post Ids Will Be Sort. Ex: 1,2,3', 'tf-real-estate' ),
				'default'     => '',
				'label_block' => true,
			]
		);

		$this->add_responsive_control(
			'map_height',
			[
				'label' => esc_html__( 'Map Height', 'tf-real-estate' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'vh' ],
				'range' => [
					'px' => [
						'min' => 1,
						'max' => 10000,
					],
					'vh' => [
						'min' => 1,
						'max' => 100,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .tf-map-property .map-container:has(#map)' => 'height: {{SIZE}}{{UNIT}} !important;',
				],
			]
		);

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

	protected function render( $instance = [] ) {

		$settings       = $this->get_settings_for_display();
		$taxonomy_query = array();

		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',
			'posts_per_page' => $settings['properties_per_page'],
			'paged'          => $paged
		);

        if ( ! empty( $settings['exclude'] ) ) {
			if ( ! is_array( $settings['exclude'] ) )
				$exclude = explode( ',', $settings['exclude'] );

			$query_args['post__not_in'] = $exclude;
		}

		$query_args['orderby'] = $settings['order_by'];
		$query_args['order']   = $settings['order'];

		if ( $settings['sort_by_id'] != '' ) {
			$sort_by_id             = array_map( 'trim', explode( ',', $settings['sort_by_id'] ) );
			$query_args['post__in'] = $sort_by_id;
			$query_args['orderby']  = 'post__in';
		}

		$query = new WP_Query( $query_args );

		$this->add_render_attribute( 'tf_map_property', [ 'id' => "tf-properties-{$this->get_id()}", 'class' => [ 'tf-map-property' ], 'data-tabid' => $this->get_id() ] );

		if ( $query->have_posts() ) : ?>
			<div <?php echo $this->get_render_attribute_string( 'tf_map_property' ); ?>>
				<div class="wrap-map-property">
					<div class="content-map">
						<div class="property-data">
                            <?php while ( $query->have_posts() ) :
								$query->the_post(); 
                                $property_id                  = get_the_ID();
                                $prop_price_value             = get_post_meta( $property_id, 'property_price_value', true );
                                $prop_price_unit              = get_post_meta( $property_id, 'property_price_unit', true );
                                $prop_price_prefix            = get_post_meta( $property_id, 'property_price_prefix', true );
                                $prop_price_postfix           = get_post_meta( $property_id, 'property_price_postfix', true );
                                $prop_address                 = get_post_meta( $property_id, 'property_address', true );
                                $prop_size                    = get_post_meta( $property_id, 'property_size', true ) ? get_post_meta( $property_id, 'property_size', true ) : 0;
                                $prop_bedrooms                = get_post_meta( $property_id, 'property_bedrooms', true ) ? get_post_meta( $property_id, 'property_bedrooms', true ) : 0;
                                $prop_bathrooms               = get_post_meta( $property_id, 'property_bathrooms', true ) ? get_post_meta( $property_id, 'property_bathrooms', true ) : 0;
				                $get_id_post_thumbnail = get_post_thumbnail_id( $property_id );
                                $prop_featured                = empty( $get_id_post_thumbnail ) ? TF_PLUGIN_URL . "includes/elementor-widget/assets/images/no-image.jpg" : tfre_image_resize_id( $get_id_post_thumbnail, 500, 500, true );
                                ?>
	                            <div class="tfre-image-map" title="<?php echo esc_attr( get_the_title()); ?>" data-image="<?php echo esc_attr( $prop_featured ); ?>" data-location="<?php echo esc_attr( $prop_address ); ?>" data-bed="<?php echo esc_attr( $prop_bedrooms ); ?>" data-bath="<?php echo esc_attr( $prop_bathrooms ); ?>"  data-size="<?php echo esc_attr( $prop_size ); ?>" data-measurement-units="<?php echo esc_attr( $prop_price_unit ); ?>" data-id="<?php echo esc_attr( $property_id ); ?>" data-price="<?php echo esc_attr( $prop_price_value ); ?>" data-price-prefix="<?php echo esc_attr( $prop_price_prefix ); ?>" data-price-postfix="<?php echo esc_attr( $prop_price_postfix ); ?>"></div>
						    <?php endwhile; ?>
						</div>
					    <div class="map-container">
					    	<div id="map" class=""></div>
					    </div>
					</div>
					<div class="fixed-map-stopper"></div>
					<?php wp_reset_postdata(); ?>
				</div>
			</div>
			<?php
		else :
			esc_html_e( 'No properties found', 'tf-real-estate' );
		endif;
	}
}