<?php
namespace Cool_FormKit_Pro\Widgets\Addons;

/**
 * Class CoolForm_Cfkef_Conditional_Submit_Button_Pro
 */
if ( ! defined( 'ABSPATH' ) ){
    exit;
} 

use Elementor\Controls_Manager;

if(!class_exists('CoolForm_Cfkef_Conditional_Submit_Button_Pro')) {
class CoolForm_Cfkef_Conditional_Submit_Button_Pro{

    private $validate_form = false;

    public function __construct(){
        add_action( 'elementor/element/cool-form/section_buttons/after_section_start', array($this,'inject_custom_control_inside_section_buttons'), 10, 2 );
        add_action( 'cool_formkit/forms/pre_render', array( $this, 'field_conditions_for_submit_button' ), 100, 3 );
		add_action( 'wp_enqueue_scripts', array( $this, 'add_assets_files' ) );
        add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'add_editor_js' ) );
    }


    /**
	 *
	 * Js and css files loaded for elementor editor mode for add dynamic tags
	 */
	public function add_editor_js() {
		wp_register_script( 'coolform_cfefp_logic_editor', CFKEF_PLUGIN_URL . 'assets/addons/js/coolform-editor.js', array( 'jquery' ), CFKEF_PLUGIN_VERSION, true );
		wp_register_style( 'coolform_cfefp_logic_editor', CFKEF_PLUGIN_URL . 'assets/addons/css/coolform-editor.css', null, CFKEF_PLUGIN_VERSION );
		wp_enqueue_script( 'coolform_cfefp_logic_editor' );
	}

    public function add_assets_files(){
        wp_register_script( 'coolform_cfefp_logic_submit_button', CFKEF_PLUGIN_URL . 'assets/addons/js/coolform-logic_frontend_submit_button.js', array( 'jquery' ), CFKEF_PLUGIN_VERSION, true );
        
        // Add hidden class CSS
        wp_register_style( 'hide_field_class_style', false );
        wp_enqueue_style( 'hide_field_class_style' );
        wp_add_inline_style(
           'hide_field_class_style',
           '.cfef-hidden {
           display: none !important;
           }'
        );
    }

    public function controler_new_id_for_submit_button($control_id){
        return $control_id . '_cfefp_submit';
    }

    public function inject_custom_control_inside_section_buttons($widget, $args){
        $cfef_conditional_logic_id_for_submit = $this->controler_new_id_for_submit_button( 'cfef_logic' );
		$widget->add_control(
			$cfef_conditional_logic_id_for_submit,
			array(
				'label' => esc_html__( 'Enable Conditions', 'cool-formkit' ),
				'render_type' => 'none',
				'type' => Controls_Manager::SWITCHER,
            )
		);

        $widget->add_control(
            'cfef_notice_unique_button_id',
            [
                'type'            => \Elementor\Controls_Manager::RAW_HTML,
                'raw'             => '<div class="elementor-panel-alert elementor-panel-alert-info">' 
                . esc_html__( 'Please add a unique button ID on the submit button to add conditions on the submit button.', 'cool-formkit' ) . '</div>',
                'condition' => array(
                    $cfef_conditional_logic_id_for_submit => 'yes'
                ),
            ]
        );

		$widget->add_control(
			$this->controler_new_id_for_submit_button( 'cfef_logic_mode' ),
			array(
				'label' => esc_html__( 'Enable If / Disable If', 'cool-formkit' ),
                'type' => Controls_Manager::CHOOSE,
                'options' => array(
                    'show' => array(
                        'title' => esc_html__( 'Show', 'cool-formkit' ),
                        'icon'  => 'fa fa-eye',
                    ),
                    'hide' => array(
                        'title' => esc_html__( 'Hide', 'cool-formkit' ),
                        'icon'  => 'fa fa-eye-slash',
                    ),
                    'enable' => array(
                        'title' => esc_html__( 'Enable', 'cool-formkit' ),
                        'icon' => 'fa fa-toggle-on',
                    ),
                    'disable' => array(
                        'title' => esc_html__( 'Disable', 'cool-formkit' ),
                        'icon' => 'fa fa-toggle-off',
                    ),
                ),
                'default' => 'show',
                'condition' => array(
                    $cfef_conditional_logic_id_for_submit => 'yes'
                ),
            )
		);
		$widget->add_control(
			$this->controler_new_id_for_submit_button( 'cfef_logic_meet' ),
			array(
				'label' => esc_html__( 'Conditions Trigger', 'cool-formkit' ),
                'type' => Controls_Manager::SELECT,
                'options' => array(
                    "All"=> esc_html__('All - AND Conditions','cool-formkit'),
                    "Any"=> esc_html__('Any - OR Conditions','cool-formkit')
                ),
                'default' => 'All',
                'condition' => array(
                    $cfef_conditional_logic_id_for_submit => 'yes'
                ),
            )
		);

		$repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'cfef_logic_field_id',
            array(
                'label' => esc_html__( 'Field ID', 'cool-formkit' ),
                'type' => Controls_Manager::TEXT,
                'label_block' => true,
                'default' => '',
                'ai' => array(
                    'active' => false,
                ),
            )
        );

        $repeater->add_control(
            'cfef_logic_field_is',
            array(
                'label' => esc_html__( 'Operator', 'cool-formkit' ),
                'type' => Controls_Manager::SELECT,
                'label_block' => true,
                'options' => array(
                    '==' => esc_html__( 'is equal ( == )', 'cool-formkit' ),
                    '!=' => esc_html__( 'is not equal (!=)', 'cool-formkit' ),
                    '>'  => esc_html__( 'greater than (>)', 'cool-formkit' ),
                    '<'  => esc_html__( 'less than (<)', 'cool-formkit' ),
                    '>=' => esc_html__( 'greater than equal (>=)', 'cool-formkit' ),
                    '<=' => esc_html__( 'less than equal (<=)', 'cool-formkit' ),
                    'e'  => esc_html__( "empty ('')", 'cool-formkit' ),
                    '!e' => esc_html__( 'not empty', 'cool-formkit' ),
                    'c'  => esc_html__( 'contains', 'cool-formkit' ),
                    '!c' => esc_html__( 'does not contain', 'cool-formkit' ),
                    '^'  => esc_html__( 'starts with', 'cool-formkit' ),
                    '~'  => esc_html__( 'ends with', 'cool-formkit' ),
                ),
                'default' => '==',
                'ai' => array(
                    'active' => false,
                ),
            )
        );

        $repeater->add_control(
            'cfef_logic_compare_value',
            array(
                'label' => esc_html__( 'Value to compare', 'cool-formkit' ),
                'type' => Controls_Manager::TEXT,
                'label_block' => true,
                'default' => '',
                'ai' => array(
                    'active' => false,
                ),
            )
        );

        $widget->add_control(
            $this->controler_new_id_for_submit_button( 'cfef_repeater_data' ),
            array(
                'name' => 'cfef_repeater_data',
                'label' => esc_html__( 'Enable If', 'cool-formkit' ),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'condition' => array(
                    $cfef_conditional_logic_id_for_submit => 'yes'
                ),
                'style_transfer' => false,
                'title_field' => '{{{ cfef_logic_field_id }}} {{{ cfef_logic_field_is }}} {{{ cfef_logic_compare_value }}}',
                'default' => array(
                    array(
                        'cfef_logic_field_id' => '',
                        'cfef_logic_field_is' => '==',
                        'cfef_logic_compare_value' => '',
                    ),
                ),
            )
        );

        
    }

    public function field_conditions_for_submit_button($instance){
        $logic_object = array();
        $repeater_data = $instance[$this->controler_new_id_for_submit_button('cfef_repeater_data')];
        if(!empty($repeater_data)){
            wp_enqueue_script( 'coolform_cfefp_logic_submit_button' );
            
            foreach ($repeater_data as $data) {
            $logic_data_item = array();
                foreach ($data as $key => $value) {
                    $logic_data_item[$key] = esc_html($value);
                }
                $logic_object['logic_data'][] = $logic_data_item;
                $button_css_id = $instance['button_css_id'];
                $newObj = array($button_css_id => $logic_object);
            }
            if(!empty($button_css_id)){
                $newObj[$button_css_id]['display_mode'] = $instance[$this->controler_new_id_for_submit_button( 'cfef_logic_mode' )]; 
                $newObj[$button_css_id]['fire_action'] = $instance[$this->controler_new_id_for_submit_button( 'cfef_logic_meet' )];         
            }
            
            $condition = !empty($newObj) ? wp_json_encode($newObj) : '';
            echo '<div class="cfef_logic_data_submit_button_js cfef-hidden">' . wp_kses_post($condition) . '</div>';

        }
    }
    
    
    function cfefp_check_field_redirection_logic($value_id, $operator, $value) {

        // Sanitize and escape dynamic values.
		$value_id = esc_html( $value_id );
		$value    = trim( $value );
		$value    = esc_html( $value );

        switch($operator) {
            case "==":
                return $value_id == $value;
            case "!=":
                return $value_id != $value;
            case "e":
                return $value_id == "";
            case "!e":
                return $value_id != "";
            case "c":
                return str_contains($value_id, $value);
            case "!c":
                return !str_contains($value_id, $value);
            case "^":
                return str_starts_with($value_id, $value);
            case "~":
                return str_ends_with($value_id, $value);
            case ">":
                return $value_id > $value;
            case "<":
                return $value_id < $value;
            case "<=":
                return $value_id <= $value;
            case ">=":
                return $value_id >= $value;
            default:
                return false; // Default case if operator is not recognized
        }
    } 
    
}
}
