<?php
/*
 * Elementor Confin ContactInfo Widget
 * Author & Copyright: themexshaper
*/

namespace Elementor;

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

class Confin_ContactInfo extends Widget_Base{

	/**
	 * Retrieve the widget name.
	*/
	public function get_name(){
		return 'tmx-confin_contactinfo';
	}

	/**
	 * Retrieve the widget title.
	*/
	public function get_title(){
		return esc_html__( 'ContactInfo', 'confin-core' );
	}

	/**
	 * Retrieve the widget item.
	*/
	public function get_item() {
		return 'eicon-icon-box';
	}

	/**
	 * Retrieve the list of categories the widget belongs to.
	*/
	public function get_categories() {
		return ['themexshaper-category'];
	}

	/**
	 * Retrieve the list of scripts the Confin ContactInfo widget depended on.
	 * Used to set scripts dependencies required to run the widget.
	*/
	public function get_script_depends() {
		return ['tmx-confin_contactinfo'];
	}
	
	/**
	 * Register Confin ContactInfo widget controls.
	 * Adds different input fields to allow the user to change and customize the widget settings.
	*/
	protected function register_controls(){
		
		$this->start_controls_section(
			'section_contactinfo',
			[
				'label' => esc_html__( 'ContactInfo Options', 'confin-core' ),
			]
		);
		$this->add_control(
			'section_subtitle',
			[
				'label' => esc_html__( 'Sub Title Text', 'confin-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Title Sub Text', 'confin-core' ),
				'placeholder' => esc_html__( 'Type sub title text here', 'confin-core' ),
				'label_block' => true,
			]
		);
		$this->add_control(
			'section_title',
			[
				'label' => esc_html__( 'Title Text', 'confin-core' ),
				'type' => Controls_Manager::TEXT,
				'default' => esc_html__( 'Title Text', 'confin-core' ),
				'placeholder' => esc_html__( 'Type title text here', 'confin-core' ),
				'label_block' => true,
			]
		);
		$repeater = new Repeater();
    $repeater->add_control(
			'contact_icon',
			[
				'label' => __( 'Icon', 'confin-core' ),
				'type' => \Elementor\Controls_Manager::ICONS,
				'default' => [
					'value' => 'ti-location-pin',
					'library' => 'solid',
				],
			]
		);
		$repeater->add_control(
			'contact_content',
			[
				'label' => esc_html__( 'Content Text', 'confin-core' ),
				'type' => Controls_Manager::TEXTAREA,
				'default' => esc_html__( '54, Dahs udin sorok, Melborn Austria', 'confin-core' ),
				'placeholder' => esc_html__( 'Type title text here', 'confin-core' ),
				'label_block' => true,
			]
		);
    
		$this->add_control(
			'contactinfoItems_groups',
			[
				'label' => esc_html__( 'Contact Info Items', 'confin-core' ),
				'type' => Controls_Manager::REPEATER,
				'default' => [
					[
						'contact_title' => esc_html__( 'Contact Info', 'confin-core' ),
					],
					
				],
				'fields' =>  $repeater->get_controls(),
				'title_field' => '{{{ contact_title }}}',
			]
		);
		$this->end_controls_section();// end: Section


		// Sub Title
		$this->start_controls_section(
			'contactinfo_section_subtitle_style',
			[
				'label' => esc_html__( 'Sub Title', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'contactinfo_sub_title_typography',
				'selector' => '{{WRAPPER}} .contact-page-section .section-title-s2 span',
			]
		);
		$this->add_control(
			'contactinfo_subtitle_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .contact-page-section .section-title-s2 span, .contact-page-section .section-title-s2 span:before' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'contactinfo_subtitle_padding',
			[
				'label' => __( 'Sub Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .contact-page-section .section-title-s2 span' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section
	
		// Title
		$this->start_controls_section(
			'contactinfo_section_title_style',
			[
				'label' => esc_html__( 'Title', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'label' => esc_html__( 'Typography', 'confin-core' ),
				'name' => 'confin_title_typography',
				'selector' => '{{WRAPPER}} .contact-page-section .section-title-s2 h2',
			]
		);
		$this->add_control(
			'contactinfo_title_color',
			[
				'label' => esc_html__( 'Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .contact-page-section .section-title-s2 h2' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'contactinfo_title_padding',
			[
				'label' => esc_html__( 'Title Padding', 'confin-core' ),
				'type' => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', 'em' ],
				'selectors' => [
					'{{WRAPPER}} .contact-page-section .section-title-s2 h2' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		// Info
		$this->start_controls_section(
			'contactinfo_section_content_style',
			[
				'label' => esc_html__( 'Info', 'confin-core' ),
				'tab' => Controls_Manager::TAB_STYLE,
			]
		);
		$this->add_control(
			'contactinfo_icon_color',
			[
				'label' => esc_html__( 'Icon Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .contact-info-area .contact-info ul .icon i:before' => 'color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'contactinfo_icon_bg_color',
			[
				'label' => esc_html__( 'BG Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .contact-info-area .contact-info ul .icon' => 'background-color: {{VALUE}};',
				],
			]
		);
		$this->add_control(
			'contactinfo_content_color',
			[
				'label' => esc_html__( 'Text Color', 'confin-core' ),
				'type' => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .contact-info-area .contact-info ul p' => 'color: {{VALUE}};',
				],
			]
		);
		$this->end_controls_section();// end: Section

		
	}

	/**
	 * Render ContactInfo widget output on the frontend.
	 * Written in PHP and used to generate the final HTML.
	*/
	protected function render() {
		$settings = $this->get_settings_for_display();
		$contactinfoItems_groups = !empty( $settings['contactinfoItems_groups'] ) ? $settings['contactinfoItems_groups'] : [];

		$section_subtitle = !empty( $settings['section_subtitle'] ) ? $settings['section_subtitle'] : '';
		$section_title = !empty( $settings['section_title'] ) ? $settings['section_title'] : '';

		// Turn output buffer on
		ob_start();
		?>
		<div class="contact-page-section">
	    <div class="contact-info-area">
	        <div class="section-title-s2">
	        	 <?php 
		            if( $section_subtitle ) { echo '<span>'.esc_html( $section_subtitle ).'</span>'; } 
		            if( $section_title ) { echo '<h2>'.wp_kses_post( $section_title ).'</h2>'; } 
            	?>
	        </div>
	        <div class="contact-info">
	            <ul>
	               <?php 	// Group Param Output
									if( is_array( $contactinfoItems_groups ) && !empty( $contactinfoItems_groups ) ){
									foreach ( $contactinfoItems_groups as $each_items ) { 
									$contact_content = !empty( $each_items['contact_content'] ) ? $each_items['contact_content'] : '';

									$contact_icon = !empty( $each_items['contact_icon']['value'] ) ? $each_items['contact_icon']['value'] : '';
									$contact_svg_url = !empty( $each_items['contact_icon']['value']['url'] ) ? $each_items['contact_icon']['value']['url'] : '';
									$svg_alt = get_post_meta( $contact_svg_url , '_wp_attachment_image_alt', true);

									$contact_content = preg_replace('~\s*<br ?/?>\s*~',"<br />",$contact_content);
    							$contact_content = nl2br( $contact_content );

									?>
	                <li>
	                    <div class="icon">
	                      <?php if ( $contact_svg_url ) { 
				            			 echo '<img src="'.esc_url( $contact_svg_url ).'" alt="'.esc_url( $svg_alt ).'">';
				            		} else {
				            			echo ' <i class="'.esc_attr( $contact_icon ).'"></i>';
				            		} ?>
	                    </div>
	                    <?php  if( $contact_content ) { echo '<p>'.wp_kses_post( $contact_content ).'</p>'; } ?>
	                </li>
								<?php } } ?>
	            </ul>
	        </div>
	    </div>
		</div>
		<?php
			// Return outbut buffer
			echo ob_get_clean();	
		}
	/**
	 * Render ContactInfo widget output in the editor.
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	*/
	
	//protected function _content_template(){}
	
}
Plugin::instance()->widgets_manager->register( new Confin_ContactInfo() );