<?php
/**
 * Field render
 * @var Form $form
 * @var string $type
 * @var array $item
 * @var string $prefix
 * @var int $item_index
 * @var string $formId
 * @var string $itemId
 * @var string $key
 * @var array $settings
 */

use ElementorPro\Modules\Forms\Widgets\Form;

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

$form->remove_render_attribute( 'input' . $item_index, 'type' );
$form->remove_render_attribute( 'input' . $item_index, 'required' );

$value = esc_attr( $item[ $prefix . 'default_value' ] );
$min = esc_attr( $item[ $prefix . 'min' ] );
$max = esc_attr( $item[ $prefix . 'max' ] );
$step = esc_attr( $item[ $prefix . 'step' ] );

$form->add_render_attribute(
	'input' . $item_index,
	[
		'class' => 'elementor-use-native ' . $prefix . 'field',
		'value' => $value,
		'min'   => $min,
		'max'   => $max,
		'step'  => $step,
	]
);

$settings['attrs'] = $form->get_render_attributes( 'input' . $item_index );
$settings['showInput'] = esc_attr( $item[ $prefix . 'show_input' ] ) === 'yes';
$settings['itemId'] = esc_attr( $item['_id'] );
$settings['formId'] = $form->get_id();

$defaultValue = $item[ $prefix . 'default_value' ];
if ( empty( $defaultValue ) ) {
	$defaultValue = $max < $min ? $min : $min + ($max - $min) / 2;
}
?>
<div
	class="<?php echo esc_attr( $type ); ?>_wrap elementor-repeater-item-<?php echo esc_attr( $itemId ); ?>"
	data-key="<?php echo esc_attr( $key ); ?>"
>
	<div class="hulk-range-input-wrap">
		<input aria-label="" type="range" <?php $form->print_render_attribute_string( 'input' . $item_index ); ?>>
	</div>
	<?php if ( $settings['showInput'] ) : ?>
		<div class="hulk-show-input-wrap">
			<input
				aria-label=""
				type="number"
				class="elementor-field elementor-field-textual hulk-show-input"
				value="<?php echo esc_attr( $defaultValue ); ?>"
			>
		</div>
	<?php endif; ?>
</div>
