<?php
namespace Cool_FormKit_Pro\Widgets\HelloPlusAddons;



if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
if(!class_exists('HelloPlus_CONFIRM_DIALOG')) {
class HelloPlus_CONFIRM_DIALOG {

    /**
	 * Plugin instance.
	 *
	 * @var HelloPlus_CONFIRM_DIALOG
	 *
	 * @access private
	 * @var null
	 */
	private static $instance = null;

	/**
	 * Get the plugin instance.
	 *
	 * @return HelloPlus_CONFIRM_DIALOG
	 * @static
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

    /**
	 * Constructor method to initialize actions and assets.
	 */
    public function __construct() {

        add_action( 'wp_enqueue_scripts', array( $this, 'add_assets_files' ) );
        add_action( 'elementor/frontend/widget/before_render', array( $this, 'elementor_form_render' ), 10, 3 );
        add_action( 'elementor/element/ehp-form/section_form_fields/after_section_end', [ $this, 'add_custom_controls' ], 10, 2 );
        // add_action( 'elementor/frontend/after_enqueue_styles', array( $this, 'enqueue_frontend_styles' ) ); 
        // add_action( 'elementor/frontend/after_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
    }

    public function elementor_form_render( $instance ) {
        if($instance->get_name() !== 'ehp-form'){
			return;
		}

        $instance = $instance->get_settings_for_display();

		if ( 'yes' === $instance['cdfef_confirm'] ) {
            wp_enqueue_script( 'helloplus-cdfef-frontend-js' );
            wp_enqueue_style('helloplus-cdfef-frontend-css');

			$title     = esc_attr($instance['cdfef_title']);
			$message     = wp_kses_post($instance['cdfef_msg']);
			$action_btn_txt     = esc_attr($instance['cdfef_action_btn_txt']);
			$action_btn_color     = esc_attr($instance['cdfef_action_btn_color']);
			$cancel_btn_txt     = esc_attr($instance['cdfef_cancel_btn_txt']);
			$cancel_btn_color     = esc_attr($instance['cdfef_cancel_btn_color']);
			$title_color     = esc_attr($instance['cdfef_title_color']);
			$msg_color     = esc_attr($instance['cdfef_msg_color']);
			$bg_color     = esc_attr($instance['cdfef_bg_color']);
			$btn_txt_color     = esc_attr($instance['cdfef_btn_txt_color']);
			$border_radi     = esc_attr($instance['cdfef_border_radius']['size']);

            echo '<div class="cdfef-input" style="display: none;">
           
           <!-- Dialog Box HTML Structure -->
           <div class="cdfef-dialog-overlay" style="display: block;">
               <div class="cdfef-dialog-box" style="background-color: ' . esc_attr( $bg_color ) . '; border-radius: ' . esc_attr( $border_radi ) . 'px">
                   <button class="cdfef-dialog-close-btn">&times;</button>
                   <h2 class="cdfef-dialog-title" style="color: ' . esc_attr( $title_color ) . ';">' . esc_html( $title ) . '</h2>
                   <p class="cdfef-dialog-message" style="color: ' . esc_attr( $msg_color ) . ';">' . wp_kses_post( $message ) . '</p>
                   <div class="cdfef-dialog-actions">
                       <button class="cdfef-dialog-confirm-btn" style="background-color: ' . esc_attr( $action_btn_color ) . '; color: ' . esc_attr( $btn_txt_color ) . ';">' . esc_html( $action_btn_txt ) . '</button>
                       <button class="cdfef-dialog-cancel-btn" style="background-color: ' . esc_attr( $cancel_btn_color ) . ';color: ' . esc_attr( $btn_txt_color ) . ';">' . esc_html( $cancel_btn_txt ) . '</button>
                   </div>   
               </div>
           </div>
       </div>';
		}
    }
	

    public function add_custom_controls( $element, $args ) {
        // Ensure it's the correct widget
        if ( 'ehp-form' === $element->get_name() ) {
            
            // Start new custom section after form fields section
            $element->start_controls_section(
                'cdfef_custom_section',
                [
                    'label' => __( 'Confirm Dialog Before Submit', 'cool-formkit' ),
                    'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
                ]
            );

            // Add a new checkbox control
            $element->add_control(
                'cdfef_confirm',
                [
                    'label' => __( 'Confirm Dialog', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::SWITCHER,
                    'label_on' => __( 'Yes', 'cool-formkit' ),
                    'label_off' => __( 'No', 'cool-formkit' ),
                    'return_value' => 'yes',
                    'default' => 'no',
                ]
            );

            $element->add_control(
                'cdfef_title',
                [
                    'label' => __( 'Title', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::CODE,
                    'default' => __( 'Confirm Submission', 'cool-formkit' ),
                    'placeholder' => __( 'Enter custom text', 'cool-formkit' ),
                    'dynamic' => [
                        'active' => true, 
                    ],
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],  
                    'label_block' => 'true',    
                    'language' => 'html',   
                ]
            );

            $element->add_control(
                'cdfef_title_color',
                [
                    'label' => __( 'Title Text Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#2c2a2f', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_msg',
                [
                    'label' => __( 'Message', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::CODE,
                    'default' => __( 'Are you sure you want to submit this form?', 'cool-formkit' ),
                    'dynamic' => [
                        'active' => true, 
                    ],
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_msg_color',
                [
                    'label' => __( 'Message Text Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#7a7595', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_action_btn_heading',
                [
                    'label' => __( 'Action Button', 'cool-formkit' ), 
                    'type' => \Elementor\Controls_Manager::HEADING,           
                    'separator' => 'before',                                     
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_action_btn_txt',
                [
                    'label' => __( 'Text', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::TEXT,
                    'default' => __( 'Confirm', 'cool-formkit' ),
                    'dynamic' => [
                        'active' => true, 
                    ],
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_action_btn_color',
                [
                    'label' => __( 'Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#29a321', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_cancel_btn_heading',
                [
                    'label' => __( 'Cancel Button', 'cool-formkit' ), 
                    'type' => \Elementor\Controls_Manager::HEADING,           
                    'separator' => 'before',                                     
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_cancel_btn_txt',
                [
                    'label' => __( 'Text', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::TEXT,
                    'default' => __( 'Cancel', 'cool-formkit' ), 
                    'dynamic' => [
                        'active' => true, 
                    ],
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_cancel_btn_color',
                [
                    'label' => __( 'Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#d92e2e', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_bg_color',
                [
                    'label' => __( 'Background Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#fff',
                    'separator' => 'before', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_btn_txt_color',
                [
                    'label' => __( 'Button Text Color', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::COLOR,
                    'default' => '#fff', 
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'cdfef_border_radius',
                [
                    'label' => __( 'Border Radius', 'cool-formkit' ),
                    'type' => \Elementor\Controls_Manager::SLIDER,
                    'condition' => [
                        'cdfef_confirm' => 'yes',
                    ],
                    'size_units'   => array( 'px' ),
                    'range'        => array(
                        'px' => array(
                            'min' => 0,
                            'max' => 20,
                        ),
    
                    ),
                    'default'        => array(
                        'size' => 0,
                        'unit' => 'px',
                    ),
                ]
            );

            $element->end_controls_section(); 
        }
    }

    public function add_assets_files(){
        wp_register_script( 'helloplus-cdfef-frontend-js', CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-confirm-dialog-box.js',  array( 'jquery' ), CFKEF_PLUGIN_VERSION, true );

        wp_register_style( 'helloplus-cdfef-frontend-css', CFKEF_PLUGIN_URL . 'assets/css/confirm-dialog-box.css', null, CFKEF_PLUGIN_VERSION );
    }
    // Method to enqueue custom CSS for the Elementor editor
    public function enqueue_frontend_scripts() {
        wp_register_script( 'helloplus-cdfef-frontend-js', CFKEF_PLUGIN_URL . 'assets/helloplus-addons/js/helloplus-confirm-dialog-box.js',  array( 'jquery' ), CFKEF_PLUGIN_VERSION, true );
    }

    public function enqueue_frontend_styles() { 
        wp_register_style( 'helloplus-cdfef-frontend-css', CFKEF_PLUGIN_URL . 'assets/css/confirm-dialog-box.css', null, CFKEF_PLUGIN_VERSION );
    }
}

// Instantiate the class to register the action
// new CoolForm_CONFIRM_DIALOG();
HelloPlus_CONFIRM_DIALOG::get_instance();

}