<?php
/**
 * @var int    $form_id          Which form we are dealing with.
 * @var int    $field_id         Which field ID we are dealing with.
 * @var string $html_id          Which field html this field has.
 * @var GF_Field_Number  $field  Which field object we are dealing with.
 * @var array  $entries          Which entries we are modifying.
 * @var array  $selected_fields  Which fields were selected.
 * @var string $view             Which view are in.
 * @var string $placeholder      Placeholder for this field.
 * @var mixed  $form             Form settings.
 * @var string $type             Optional. Input type to be used. By default, 'text' to resolve international decimal issues.
 * @var string $pattern          Optional. Pattern attribute to be used.
 * @var int    $min              Optional. Minimum value for this field.
 * @var int    $max              Optional. Maximum value for this field.
 */

$id = isset( $input['id'] ) ? $input['id'] : $field->id;
$type = isset( $type ) ? $type : 'text';
$min = isset( $min ) ? $min : rgar( $field, 'rangeMin', null );
$max = isset( $max ) ? $max : rgar( $field, 'rangeMax', null );

$has_calculation = ( '' !== rgar( $field, 'calculationFormula', '' ) );

if( $has_calculation ) {
	$placeholder = esc_attr__( 'Calculations will not be processed.', 'gk-gravityactions' );
}

if ( ! isset( $pattern ) ) {

	/**
	 * Instead of trying to use advanced logic, ask the field to format the value. Use that to determine its format.
	 * There's currency, number formats, WordPress internationalization...it gets complicated. So let's ask the field.
	 *
	 * So: does 10,000 look like ten or ten-thousand? That depends on the formatting!
	 */
	$check_number = is_callable( [ $field, 'clean_value' ] ) ? $field->clean_value( '10,000' ) : 10000;

	$number_format = intval( $check_number ) === 10 ? 'decimal_comma' : 'decimal_dot';

	/**
	 * We're using `type="text"` instead of `type="number"` because of issues with international decimal places.
	 * Using `pattern` attribute allows more flexibility here. In Safari, the browser will not show the `$title`
	 * message as the validation message.
	 */
	switch ( $number_format ) {
		case 'decimal_comma':
			$pattern = '^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]+)?)$';
			break;
		case 'decimal_dot':
		default:
			$pattern = '^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]+)?)$';
			break;
	}
}

?>
<input
	id="<?php echo esc_attr( $html_id ); ?>"
	type="<?php echo esc_attr( $type ); ?>"
	pattern="<?php echo esc_attr( $pattern ); ?>"
	title="<?php esc_attr_e( 'Please enter a valid number.', 'gk-gravityactions' ); ?>"
	step="any"
	class="gk-gravityactions-edit-field"
	name="field_values[<?php echo esc_attr( $id ); ?>]"
	<?php if ( $has_calculation ) : ?>
		disabled="disabled"
	<?php endif; ?>
	<?php if ( isset( $min ) ) : ?>
		min="<?php echo esc_attr( $min ); ?>"
	<?php endif; ?>
	<?php if ( isset( $max ) ) : ?>
		max="<?php echo esc_attr( $max ); ?>"
	<?php endif; ?>
	<?php if ( "" !== $placeholder ) : ?>
		placeholder="<?php echo esc_attr( $placeholder ); ?>"
	<?php endif; ?>
	<?php if ( isset( $field_values[ $id ] ) ) : ?>
		value="<?php echo esc_attr( $field_values[ $id ] ); ?>"
	<?php endif; ?>
/>
