<?php
/** 
 * Elementor Dexencore Contact From 7
 * @since 1.0.0
*/
use Elementor\Controls_Manager;
use Elementor\Group_Control_Image_Size;
use Elementor\Utils;


class ContactForm7 extends \Elementor\Widget_Base {
    public function get_name() {
        return 'dexen_contact_form7';
    }
    public function get_title(){
        return esc_html__( 'Dexencore Contact From 7', 'plugin-name' );
    }
    public function get_icon(){
        return 'eicon-mail';
    }
    public function get_categories(){
        return [ 'dexencore' ];
    }
    protected function _register_controls() {
 
		$this->start_controls_section(
            'banner_slider_content',
            [
                'label' => esc_html__( 'Dexen Contact Form 7', 'plugin-name' ),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
		);
		$this->add_control(
			'cf7',
			[
				'label' => esc_html__( 'Select Contact Form', 'dexencore' ),
                'description' => esc_html__('Contact form 7 - plugin must be installed and there must be some contact forms made with the contact form 7','dexencore'),
				'type' => Controls_Manager::SELECT2,
				'multiple' => false,
				'label_block' => 1,
				'options' => get_contact_form_7_posts(),
			]
		);

        
        $this->end_controls_section();
    }
	protected function render() {				//to show on the fontend 
		static $v_veriable=0;

		$settings = $this->get_settings();
        if(!empty($settings['cf7'])){
    	   echo'<div class="elementor-shortcode dexencore-cf7-'.$v_veriable.'">';
                echo do_shortcode('[contact-form-7 id="'.$settings['cf7'].'"]');    
           echo '</div>';  
    	}

		if(!empty($settings['cf7_redirect_page']) || !empty($settings['cf7_redirect_external']) ) {  ?>
 			<script>
 			        var theform = document.querySelector('.dexencore-cf7-<?php echo $v_veriable; ?>');
						theform.addEventListener( 'wpcf7mailsent', function( event ) {
					    location = '<?php 
					    if( !empty($settings['cf7_redirect_external']) ){
							echo $settings['cf7_redirect_external'];
					    }else{
					    	 echo get_permalink( $settings['cf7_redirect_page'] );
					    }
					    ?>';
					}, false );
			</script>

		<?php  $v_veriable++;
 		}

}	
}

\Elementor\Plugin::instance()->widgets_manager->register_widget_type( new \ContactForm7() );