<?php

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

if(!class_exists('Cfkef_State_Field')) {	
class Cfkef_State_Field extends ElementorPro\Modules\Forms\Fields\Field_Base {

	public $depended_scripts = array(
		'state-list-script',
		'state-field-script'
	);

	public $depended_styles = array(
		'state-field-css',
	);

	public function get_type() {
		return 'state';
	}

	public function get_name() {
		return __( 'State', 'cool-formkit' );
	}

	public function get_script_depends(): array {
		return $this->depended_scripts;
	}

	public function get_style_depends(): array {
		return $this->depended_styles;
	}
	/**
	 * @param      $item
	 * @param      $item_index
	 * @param Form       $form
	 */

	public function render( $item, $item_index, $form ) {


		$form->add_render_attribute(
			'input' . $item_index,
			array(
				'class' => esc_attr( 'elementor-state-field elementor-field-textual' ),
			)
		);

        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $form->get_render_attribute_string() is safe
		echo '<input ' . $form->get_render_attribute_string( 'input' . $item_index ) . '>';

		if ( 'state' === $item['field_type'] ) {
			$country_field_id     = $item['sfef-field-id'];
			echo '<span class="sfef-editor-intl-input" data-id="form-field-' . esc_attr( $item['custom_id'] ) . '" data-field-id="' . esc_attr( $item['_id'] ) . '" data-country-field-id="' . esc_attr( $country_field_id ) . '" style="display: none;"></span>';
		}
	}

	public function __construct() {
		parent::__construct();
		add_action( 'wp_enqueue_scripts', array( $this, 'add_assets_files' ) );
		add_action( 'elementor/preview/init', array( $this, 'editor_preview_footer' ) );
	}

	/**
     * Get script dependencies for this field.
     *
     * @return array
     */

	/**
	 * Register common assets for the plugin.
	 */
	public function add_assets_files() {
		$js_common_dep = array( 'jquery', 'state-list-script', 'elementor-frontend' );
        if ( ! \Elementor\Plugin::$instance->preview->is_preview_mode() && is_user_logged_in() ) {
            $js_common_dep = array_merge(array( 'elementor-common' ), $js_common_dep);
        }
		
		wp_register_script( 'state-list-script', CFKEF_PLUGIN_URL . 'assets/js/state-list.min.js', array(), CFKEF_PLUGIN_VERSION, true );
		wp_register_script( 'state-field-script', CFKEF_PLUGIN_URL . 'assets/js/state-field.js', $js_common_dep, CFKEF_PLUGIN_VERSION, true );
		wp_register_style( 'state-field-css', CFKEF_PLUGIN_URL . 'assets/css/state-field.css', CFKEF_PLUGIN_VERSION, true );
	}

	public function editor_preview_footer() {
			add_action( 'wp_footer', array( $this, 'content_template_script' ) );
	}

	public function content_template_script() {
		?>
		<script>
		jQuery( document ).ready( () => {

			elementor.hooks.addFilter(
				'elementor_pro/forms/content_template/field/<?php echo esc_attr( $this->get_type() ); ?>',
				function ( inputField, item, i ) {
					<?php if ( 'state' === esc_attr( $this->get_type() ) ) { ?>
					const country_field_id  = '' !== item['sfef-field-id'] && undefined !== item['sfef-field-id'] ? item['sfef-field-id'] : '';
					<?php } ?> 
			
					const fieldType  = 'state';
					const fieldId    = `form_field_${i}`;
					const fieldClass = `elementor-field-textual elementor-field ${item.css_classes}`;
					const title      = "<?php echo esc_html__( 'State name', 'cool-formkit' ); ?>";

					return `<input type="${fieldType}" id="${fieldId}" class="${fieldClass}" title="${title}"><br>
					<span class="sfef-editor-intl-input" data-id="form_field_${i}" data-field-id="${item['_id']}" data-country-field-id="${country_field_id}" style="display: none;"></span>`;					
				}, 10, 3
			);

		});
		</script>
		<?php
	}


	/**
	 * Update form widget controls to include the country field control.
	 *
	 * Adds country field control to allow users to customize the country field.
	 *
	 * @since 1.0.0
	 * @access public
	 * @param \Elementor\Widget_Base $widget The form widget instance.
	 * @return void
	 */
	public function update_controls( $widget ) {
		$elementor    = \Elementor\Plugin::instance();
		$control_data = $elementor->controls_manager->get_control_from_stack( $widget->get_unique_name(), 'form_fields' );
		if ( is_wp_error( $control_data ) ) {
				return;
		}

		$field_controls = array(
			'sfef-field-id'             => array(
				'name'         => 'sfef-field-id',
				'label'        => esc_html__( 'Country Field Id', 'cool-formkit' ),
				'type'         => \Elementor\Controls_Manager::TEXT,
				'condition'    => array(
					'field_type' => 'state',
				),
				'tab'          => 'content',
				'inner_tab'    => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'ai'           => array(
					'active' => false,
				),
			),
		);

		$control_data['fields'] = \array_merge( $control_data['fields'], $field_controls );
		$widget->update_control( 'form_fields', $control_data );
	}
}
}
?>
