<?php
/**
 * @since 1.2
 * @var \GF_Field $field
 * @var \GravityKit\GravityActions\Actions\ActionInterface $bulk_action_object Which bulk action rendered this modal.
 */

/**
 * Allows showing inputs that aren't currently displayed in the form.
 *
 * @since  1.2
 *
 * @param bool $hide_hidden_inputs True: hide inputs hidden by the form. False: show inputs hidden by the form.
 */
$hide_hidden_inputs = apply_filters( 'gk/gravityactions/hide_hidden_inputs', true );

$time_field = new GF_Field_Number( $field );

foreach ( $field->inputs as $key => $input ) {

	if( $hide_hidden_inputs && true === rgar( $input, 'isHidden' ) ) {
		continue;
	}

	$type = 'number';
	$field_args = [
		'field'       => $field,
		'type'        => $type,
		'input'       => $input,
		'placeholder' => __( 'Leaving blank will empty field.', 'gk-gravityactions' ),
	];

	switch ( $key ) {
		case 0:
			$field_args['min'] = 0;
			$field_args['max'] = $field->timeFormat === '12' ? 12 : 23;
			break;
		case 1:
			$field_args['min'] = 0;
			$field_args['max'] = 59;
			break;
		case 2:
			if( '24' === $field->timeFormat ) {
				return;
			}
			// Only allow "AM" or "PM"
			$field_args['type'] = 'text';
			$field_args['pattern'] = '^(am|AM|pm|PM)$';
			break;
	}
?>
	<div class="gk-gravityactions-field-group">
		<?php
		$html_id = sanitize_html_class( "gk-gravityactions-edit-field-{$input['id']}" );
		$field_args['html_id'] = $html_id;
		?>
		<label
			for="<?php echo $html_id; ?>"
		>
			<?php echo esc_html( $input['label'] ); ?>
		</label>
		<?php
		$template = 'components/fields/number';
		if ( ! empty( $input['inputType'] ) ) {
			$template = $bulk_action_object->get_simple_field_template( $input['inputType'] );
		} elseif ( ! empty( $field_args['type'] ) ) {
			$template = $bulk_action_object->get_simple_field_template( $field_args['type'] );
		}

		$this->render( $template, $field_args );
		?>
	</div>
<?php } ?>