<?php
class TFPostMetaBox_Widget_Free extends \Elementor\Widget_Base {

	public function get_name() {
        return 'tf-meta-box';
    }
    
    public function get_title() {
        return esc_html__( 'TF Meta Box', 'themesflat-elementor' );
    }

    public function get_icon() {
        return 'eicon-post-info';
    }
    
    public function get_categories() {
        return [ 'themesflat_addons_single_post' ];
    }

    public function get_style_depends(){
	    return ['tf-font-awesome','tf-regular','tf-style'];
	}

	protected function register_controls() {
        // Start Tab Setting        
			$this->start_controls_section( 'section_tabs',
	            [
	                'label' => esc_html__('Meta Box', 'themesflat-elementor'),
	            ]
	        );			

			$this->add_control(
				'client',
				[
					'label' => esc_html__( 'Client', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::TEXTAREA,
					'default' => esc_html__( 'Johnson Group, US', 'themesflat-elementor' ),
				]
			);

			$this->add_control(
				'website',
				[
					'label' => esc_html__( 'Website', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::TEXTAREA,
					'rows' => 3,
					'default' => esc_html__( 'yourwebsite.com', 'themesflat-elementor' ),
				]
			);

			$this->add_responsive_control(
				'width',
				[
					'label' => esc_html__( 'Width Box', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::SLIDER,
					'size_units' => [ 'px', '%' ],
					'range' => [
						'px' => [
							'min' => 0,
							'max' => 1000,
							'step' => 5,
						],
						'%' => [
							'min' => 0,
							'max' => 100,
						],
					],
					'default' => [
						'unit' => 'px',
						'size' => 360,
					],
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta' => 'width: {{SIZE}}{{UNIT}};',
					],
					'separator' => 'before',
				]
			);

			$this->add_control(
				'text_align',
				[
					'label' => esc_html__( 'Alignment', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::CHOOSE,
					'options' => [
						'flex-start' => [
							'title' => esc_html__( 'Left', 'themesflat-elementor' ),
							'icon' => 'fa fa-align-left',
						],
						'center' => [
							'title' => esc_html__( 'Center', 'themesflat-elementor' ),
							'icon' => 'fa fa-align-center',
						],
						'flex-end' => [
							'title' => esc_html__( 'Right', 'themesflat-elementor' ),
							'icon' => 'fa fa-align-right',
						],
					],
					'default' => 'flex-end',
					'toggle' => true,
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box' => 'justify-content: {{VALUE}};',
					],
				]
			);

			$this->add_control(
				'bg_color_box',
				[
					'label' => esc_html__( 'Background Color Box', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta' => 'background-color: {{VALUE}}',
					],
				]
			);

			$this->add_responsive_control(
				'padding_box',
				[
					'label' => esc_html__( 'Padding', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'default' =>	[
						'top' => '20',
						'right' => '20',
						'bottom' => '20',
						'left' => '20',
						'unit' => 'px',
						'isLinked' => false,
					],
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_responsive_control(
				'margin_box',
				[
					'label' => esc_html__( 'Margin', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::DIMENSIONS,
					'size_units' => [ 'px', '%', 'em' ],
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
					],
				]
			);

			$this->add_group_control(
				\Elementor\Group_Control_Typography::get_type(),
				[
					'name' => 'label_typography',
					'label' => esc_html__( 'Label Typography', 'themesflat-elementor' ),
					'selector' => '{{WRAPPER}} .tf-meta-box .box-meta li h6',
				]
			);

			$this->add_control(
				'label_color',
				[
					'label' => esc_html__( 'Label Color', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'default' => '#000000',
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta li h6' => 'color: {{VALUE}}',
					],
				]
			);

			$this->add_group_control(
				\Elementor\Group_Control_Typography::get_type(),
				[
					'name' => 'text_typography',
					'label' => esc_html__( 'Text Typography', 'themesflat-elementor' ),
					'selector' => '{{WRAPPER}} .tf-meta-box .box-meta li',
				]
			);

			$this->add_control(
				'text_color',
				[
					'label' => esc_html__( 'Text Color', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'default' => '#666666',
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta li' => 'color: {{VALUE}}',
						'{{WRAPPER}} .tf-meta-box .box-meta li a' => 'color: {{VALUE}}',
					],
				]
			);

			$this->add_control(
				'color_link_hover',
				[
					'label' => esc_html__( 'Color Link Hover', 'themesflat-elementor' ),
					'type' => \Elementor\Controls_Manager::COLOR,
					'default' => '#ec712d',
					'selectors' => [
						'{{WRAPPER}} .tf-meta-box .box-meta li a:hover' => 'color: {{VALUE}}',
					],
				]
			);
	        
			$this->end_controls_section();
        // /.End Tab Setting 	           
	}

	protected function render($instance = []) {
		$settings = $this->get_settings_for_display();

		$this->add_render_attribute( 'tf_meta_box_wrapper', ['id' => "tf-meta-box-{$this->get_id()}", 'class' => ['tf-meta-box'], 'data-tabid' => $this->get_id()] );

		$author = '';
		$post    = get_post();
		if ( ! empty( $post ) ) {
			$author = get_the_author_meta( 'display_name', $post->post_author );
		}

		?>
		<div <?php echo $this->get_render_attribute_string('tf_meta_box_wrapper'); ?>>
			<ul class="box-meta">
				<li>
					<h6><?php esc_html_e( 'Client:', 'themesflat-elementor' ) ?></h6>
					<p><?php esc_html_e($settings['client']); ?></p>
				</li>
				<li class="author">
					<h6><?php esc_html_e( 'Author:', 'themesflat-elementor' ) ?></h6>
					<p><?php esc_html_e($author); ?></p>
				</li>
				<li>
					<h6><?php esc_html_e( 'Tag:','themesflat-elementor' ); ?></h6>
					<p><?php echo esc_attr ( the_terms( get_the_ID(), 'portfolios_tag', '', ', ', '' ) ); ?></p>
				</li>
				<?php if ( themesflat_get_opt('show_social_share') == 1 ): ?>
				<li>
					<h6><?php esc_html_e( 'Share:', 'themesflat-elementor' ) ?></h6>					
					<?php 
					$value = themesflat_get_json('social_links');
					$sharelink = themesflat_available_social_icons();	
					?>							
					<ul class="themesflat-socials">
						 <?php
						foreach ( $value as $key => $val ) {
							if ( $key != '__ordering__') {
								$link = $sharelink[$key]['share_link'].get_the_permalink();
							    printf(
							        '<li class="%1$s">
							            <a href="%2$s" target="_blank" rel="alternate" title="%1$s">
							                <i class="fab fa-%4$s"></i>
							            </a>
							        </li>',
							        esc_attr( $key ),
							        esc_url( $link ),
							        esc_attr( $link ),
							        esc_attr( $key )
							    );
							}
						}
						    ?>
					</ul>
				</li>
				<?php endif; ?>
				<li>
					<h6><?php esc_html_e( 'Website:', 'themesflat-elementor' ) ?></h6>
					<p><?php esc_html_e($settings['website']); ?></p>
				</li>
				<li>
					<h6><?php esc_html_e( 'Date:', 'themesflat-elementor' ) ?></h6>
					<p><?php echo get_the_date();?></p>
				</li>
			</ul>
		</div>
		<?php	
		
	}

	

}