<?php
/***************************************** for time field ****************************************************************************/
//var_dump($field->type);
if ( $field->type === 'time' ) {

	$contains_error = false;

	//check if error is present
	$all_divs = $dom->getElementsByTagName( 'div' );
	foreach ( $all_divs as $current_div ) {
		$current_div_classes = $current_div->getAttribute( 'class' );
		if ( strpos( $current_div_classes, 'validation_message' ) !== false ) {
			$contains_error = true;
			//$current_div->setAttribute( 'class', $current_div->getAttribute( 'class').' mdc-text-field-helper-text mdc-text-field-helper-text--persistent mdc-text-field-helper-text--validation-msg' );
		}
	}

	foreach ( $all_divs as $div) {
		$div_classes = $div->getAttribute( 'class' );
		if ( strpos( $div_classes, 'gfield_time_hour' ) !== false || strpos( $div_classes, 'gfield_time_minute' ) !== false ) {

			if ( $contains_error ) {
				$div_classes .= ' mdc-text-field--invalid';
			}

			$div_classes .= $type === 'outlined' ? ' mdc-text-field mdc-text-field--outlined': ' mdc-text-field';

			$div->setAttribute( 'class', $div_classes );
			$div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

			$inputs = $div->getElementsByTagName( 'input' );
			foreach ( $inputs as $input ) {
				$input_id = $input->getAttribute( 'id' );
				$input_type = $input->getAttribute( 'type' );
				//partial match id to make sure its the input we want to tag
				if ( strpos(  $input_id, 'input_' ) !== false && $input_type === 'text' ) {
					$input->setAttribute( 'class',
						$input->getAttribute( 'class' ) . ' mdc-text-field__input' );
				}

				//check if placeholer is present then add float above class
				$add_label_classes = ' mdc-floating-label';
				if ( $input->getAttribute( 'placeholder' ) !== '' && $type !== 'outlined' ) {
					$add_label_classes .= ' mdc-floating-label--float-above';
				}

				$labels = $div->getElementsByTagName( 'label' );
				foreach ( $labels as $label ) {
					$label_for = $label->getAttribute( 'for' );
					//check if the label is meant to be for the input we have tagged with mdc
					if ( $label_for === $input_id ) {

						// outlined design
						if( $type === 'outlined' ) {

							// Remove 'i' element
							foreach($div->getElementsByTagName('i') as $separator ) {
								$separator->parentNode->removeChild($separator);
							}
							$notched_outline = $dom->createElement('div');
							$notched_outline->setAttribute( 'class', 'mdc-notched-outline' );

							$notched_outline_leading = $dom->createElement( 'div' );
							$notched_outline_leading->setAttribute( 'class', 'mdc-notched-outline__leading' );
							
							$notched_outline_notch = $dom->createElement( 'div' );
							$notched_outline_notch->setAttribute( 'class', 'mdc-notched-outline__notch' );

							$notched_outline_trailing = $dom->createElement( 'div' );
							$notched_outline_trailing->setAttribute( 'class', 'mdc-notched-outline__trailing' );
							
							// if ($contains_placeholder) {
							// 	$notched_outline->setAttribute( 'class', 'mdc-notched-outline mdc-notched-outline--notched' );
							// }
							$div->appendChild( $notched_outline );

							$notched_outline->appendChild( $notched_outline_leading );
							$notched_outline->appendChild( $notched_outline_notch );
							$notched_outline->appendChild( $notched_outline_trailing );

							$label->setAttribute('class', $label->getAttribute( 'class' ) . $add_label_classes );
							$notched_outline_notch->appendChild( $label );
						}
						else { // Filled style
							$label->setAttribute( 'class', $label->getAttribute( 'class' ) . $add_label_classes );
							$div->appendChild( $label );
						}
					}
				}
			}

			//if current field is either for hours or minutes then consider it as input type text and add bottom line

			//create a new div
            if ($type === 'filled') {
                $bottom_line_div = $dom->createElement('div');
                $bottom_line_div->setAttribute('class', 'mdc-line-ripple');
                $div->appendChild($bottom_line_div);
			}
			
			//$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
			$content = utf8_decode($dom->saveHTML($dom->documentElement));
		}

	}


}
