<?php

namespace Elementor_Pay_Addons\Form\Fields;

use Elementor_Pay_Addons\Form\Field_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;

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

class Decimal_Field extends Field_Base
{

	static $control_prefix = '';

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

	public function get_name()
	{
		return esc_html__('Decimal');
	}

	/**
	 * Render field output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 *
	 * @since 1.0.0
	 * @access public
	 * @param mixed $item
	 * @param mixed $item_index
	 * @param mixed $form
	 * @return void
	 */
	public function render($item, $item_index, $form)
	{
    $form->add_render_attribute( 'input' . $item_index, 'class', 'elementor-field-textual' );

		if ( isset( $item[self::$control_prefix . 'field_min'] ) ) {
			$form->add_render_attribute( 'input' . $item_index, 'min', esc_attr( $item[self::$control_prefix . 'field_min'] ) );
		}
		if ( isset( $item[self::$control_prefix . 'field_max'] ) ) {
			$form->add_render_attribute( 'input' . $item_index, 'max', esc_attr( $item[self::$control_prefix . 'field_max'] ) );
		}
		if ( isset( $item[self::$control_prefix . 'field_step'] ) ) {
			$form->add_render_attribute( 'input' . $item_index, 'step', esc_attr( $item[self::$control_prefix . 'field_step'] ) );
		}
		if ( isset( $item[self::$control_prefix . 'field_placeholder'] ) ) {
			$form->add_render_attribute( 'input' . $item_index, 'placeholder', esc_attr( $item[self::$control_prefix . 'field_placeholder'] ) );
		}

		echo '<input type="number" ' . $form->get_render_attribute_string( 'input' . $item_index ) . ' >';
	}

		/**
	 * @param Widget_Base $widget
	 */
	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 = [
			self::$control_prefix . 'field_placeholder' => [
				'label' => esc_html__( 'Placeholder', 'pay-addons-for-elementor' ),
				'name' => self::$control_prefix . 'field_placeholder',
				'type' => Controls_Manager::TEXT,
				'default' => '',
				'condition' => [
					'field_type' => $this->get_type(),
				],
				'tab' => 'content',
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'dynamic' => [
					'active' => true,
				],
			],
			self::$control_prefix . 'field_min' => [
				'name' => self::$control_prefix . 'field_min',
				'label' => esc_html__( 'Min. Value', 'pay-addons-for-elementor' ),
				'type' => Controls_Manager::NUMBER,
				'condition' => [
					'field_type' => $this->get_type(),
				],
				'tab' => 'content',
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'dynamic' => [
					'active' => true,
				],
			],
			self::$control_prefix . 'field_max' => [
				'name' => self::$control_prefix . 'field_max',
				'label' => esc_html__( 'Max. Value', 'pay-addons-for-elementor' ),
				'type' => Controls_Manager::NUMBER,
				'condition' => [
					'field_type' => $this->get_type(),
				],
				'tab' => 'content',
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
				'dynamic' => [
					'active' => true,
				],
			],
			self::$control_prefix . 'field_step' => [
				'name' => self::$control_prefix . 'field_step',
				'label' => esc_html__( 'Step', 'pay-addons-for-elementor' ),
				'type' => Controls_Manager::NUMBER,
				'default' => '0.01',
				'condition' => [
					'field_type' => $this->get_type(),
				],
				'tab' => 'content',
				'inner_tab' => 'form_fields_content_tab',
				'tabs_wrapper' => 'form_fields_tabs',
			],
			self::$control_prefix . 'field_value' => [
				'name' => self::$control_prefix . 'field_value',
				'label' => esc_html__( 'Default Value', 'pay-addons-for-elementor' ),
				'type' => Controls_Manager::TEXT,
				'default' => '',
				'dynamic' => [
					'active' => true,
				],
				'condition' => [
					'field_type' => $this->get_type(),
				],
				'tab' => 'content',
				'inner_tab' => 'form_fields_advanced_tab',
				'tabs_wrapper' => 'form_fields_tabs',
			],
		];

		$control_data['fields'] = $this->inject_field_controls( $control_data['fields'], $field_controls );
		$widget->update_control( 'form_fields', $control_data );
	}

	public function validation( $field, $record, $ajax_handler ) {

		if ( ! empty( $field[self::$control_prefix . 'field_max'] ) && $field[self::$control_prefix . 'field_max'] < (float) $field[self::$control_prefix . 'field_value'] ) {
			/* translators: %s: The value of max field. */
			$ajax_handler->add_error( $field['id'], sprintf( esc_html__( 'The field value must be less than or equal to %s.', 'pay-addons-for-elementor' ), $field[self::$control_prefix . 'field_max'] ) );
		}

		if ( ! empty( $field[self::$control_prefix . 'field_min'] ) && $field[self::$control_prefix . 'field_min'] > (float) $field[self::$control_prefix . 'field_value'] ) {
			/* translators: %s: The value of min field. */
			$ajax_handler->add_error( $field['id'], sprintf( esc_html__( 'The field value must be greater than or equal to %s.', 'pay-addons-for-elementor' ), $field[self::$control_prefix . 'field_min'] ) );
		}
	}

	public function sanitize_field( $value, $field ) {
		return floatval( $value );
	}

	public function __construct() {
		parent::__construct();
		// add_action( 'wp_footer', [ $this, 'content_template_script' ] );
		add_action( 'elementor/preview/init', [ $this, 'editor_preview_footer' ] );
	}

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

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

			elementor.hooks.addFilter(
				'elementor_pay_addons/forms/content_template/field/<?php echo $this->get_type(); ?>',
				function ( inputField, item, i ) {
					const fieldId    = `form_field_${i}`;
					const fieldClass = `elementor-field elementor-field-textual`;

					return `<input id="${fieldId}" name="form_fields[${fieldId}]"]" value="${item['field_value']}" placeholder="${item['field_placeholder']}" type="number" step="0.01" class="${fieldClass}" >`;
				}, 10, 3
			);

		});
		</script>
		<?php
	}
}
