<?php

/***************************************** for dropdown inputs *****************************************/

	$select_icon = $dom->createElement('i');
	$select_icon->setAttribute( 'class', 'mdc-select__dropdown-icon' );

if ( in_array( $field->type, $select_fields ) ) {
	$dropdown_classes_types = array( 'ginput_container_select' );

	$divs = $dom->getElementsByTagName( 'div' );

	// $div_classes = $div->getAttribute( 'class' );

	// $div_classes_array = explode( ' ', $div_classes );
	$contains_error = false;
	foreach ( $divs as $div ) {
		$div_classes = $div->getAttribute( 'class' );
		if ( strpos( $div_classes, 'validation_message' ) !== false ) {
			$contains_error = true;
			$div->setAttribute( 'class', $div->getAttribute( 'class' ) . ' mdc-text-field-helper-text mdc-text-field-helper-text--persistent mdc-text-field-helper-text--validation-msg' );
		}
	}

	foreach ( $divs as $div ) {
		$is_valid_input_field = false;
		$div_classes = $div->getAttribute( 'class' );
		$div_classes_array = explode( ' ', $div_classes );
		foreach ( $div_classes_array as $div_class_value ) {
			if ( in_array( $div_class_value, $dropdown_classes_types ) ) {
				$is_valid_input_field = true;
			}
		}
	
		if ( $is_valid_input_field ) {
			//add class to input
			$get_selects = $dom->getElementsByTagName( 'select' );
			//var_dump($get_input->getAttribute('placeholder'));

			foreach ( $get_selects as $get_select ) {
				$select_classes = $get_select->getAttribute( 'class' );
				$select_id = $get_select->getAttribute( 'id' );
				$select_classes .=  ' mdc-select__native-control';
				if ( $contains_error ) {
					$select_classes .= ' mdc-text-field--invalid';
				}
				$get_select->setAttribute( 'class', $select_classes );
			}

			//find the size of field
			if ( strpos( $select_classes, 'large' ) !== false ) {
				$field_size_class = 'stla_material_large';
			}
			else if ( strpos( $select_classes, 'medium' ) !== false ) {
				$field_size_class = 'stla_material_medium';
			}
			else {
				$field_size_class = 'stla_material_small';
			}

			$container_classes = $div->getAttribute( 'class' ).' mdc-select '.$field_size_class;
			$container_classes .= $type === 'outlined' ? ' mdc-select--outlined':'';

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

			// $icon_element = $dom->createElement( 'i' );
			// $icon_element->setAttribute( 'class', 'mdc-notched-outline' );
			// $div->appendChild( $notched_outline );

			//get the first label
			$labels = $dom->getElementsByTagName( 'label' );

			foreach ( $labels as $label ) {
				$label_for = $label->getAttribute( 'for' );


				if ( $select_id === $label_for ) {
					$add_label_classes = ' mdc-floating-label';
				   
					// if ( $contains_placeholder ) {
					// 	$add_label_classes .= ' mdc-floating-label--float-above';
					// }

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

						$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 );
							$div->appendChild( $select_icon );
						$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 );
					}
				}
			}

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

			// $label->setAttribute( 'class', $label->getAttribute( 'class' ) . ' mdc-floating-label  mdc-floating-label--float-above' );
			// //move label to be the last child of div
			// $div->appendChild( $label );

			// //create a new div
			// $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 ) );
		}
	}

}


/***************************************** for time dropdown ****************************************************************************/

if ( $field->type === 'time' ) {
	
	$get_divs = $dom->getElementsByTagName( 'div' );
	foreach ( $get_divs as $get_div ) {
		$div_classes = $get_div->getAttribute( 'class' );

		// Remove shim ( empty div added by gravity forms )
		if ( strpos( $div_classes, 'gfield_time_ampm_shim' ) !== false ) { 
			$get_div->parentNode->removeChild( $get_div );
			continue;
		}
		
		if ( strpos( $div_classes, 'gfield_time_ampm' ) !== false ) {
			$get_div_classes = $get_div->getAttribute( 'class' ). ' mdc-select';
			$get_div_classes .= $type === 'outlined' ? ' mdc-select--outlined mdc-text-field--no-label':'';
			$get_div->setAttribute( 'class', $get_div_classes );
			// $get_div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

			$get_selects = $get_div->getElementsByTagName( 'select' );

			foreach( $get_selects as $get_select ) {
				$get_select->setAttribute( 'class',
				$get_select->getAttribute( 'class' ) . ' mdc-select__native-control' );
			}
			// outlined design
			if( $type === 'outlined' ) {

				$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' );
				// }
				$get_div->appendChild( $notched_outline );
				$get_div->appendChild( $select_icon );
				$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{
				//create a new div
				$bottom_line_div = $dom->createElement( 'div' );
				$bottom_line_div->setAttribute( 'class', 'mdc-line-ripple' );
				$get_div->appendChild( $select_icon );
				$get_div->appendChild( $bottom_line_div );
			}
		}
	}
	$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
}

/***************************************** for address dropdown inputs ****************************************************************************/
if ( $field->type == 'address' ) {
	$allowed_address_fields = array( 'address_country', 'address_state' );
	$get_spans = $dom->getElementsByTagName( 'span' );
	$contains_error = false;
	foreach ( $get_spans as $get_span ) {
		$address_classes = $get_span->getAttribute( 'class' );
		$address_classes_array = explode( ' ', $address_classes );
		$is_allowed_field = false;
		foreach ( $address_classes_array as $address_class ) {
			if ( in_array( $address_class, $allowed_address_fields ) ) {
				$is_allowed_field = true;
			}
		}
		if ( $is_allowed_field ) {

			$all_divs = $dom->getElementsByTagName( 'div' );
			//create container div
			// $container_div = $dom->createElement( 'div' );
			// $container_div_classes = 'mdc-select stla_material_large';
			// $container_div_classes .= $type === 'outlined' ? ' mdc-select--outlined':'';
			// $container_div->setAttribute('class', $container_div_classes );
			// $container_div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

			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' );
				}
			}

			$get_selects = $get_span->getElementsByTagName( 'select' );
			foreach ( $get_selects as $get_select ) {
				$span_classes = $get_span->getAttribute( 'class' );
				$span_classes .= $type === 'outlined' ? ' mdc-select mdc-select--outlined':' mdc-select';
				if ( $contains_error ) {
					$span_classes .= ' mdc-text-field--invalid';
				}
				$get_span->setAttribute( 'class', $span_classes );
				// $get_span->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );
			//	$container_div->appendChild( $get_select );
				//add class to select

				$select_classes = $get_select->getAttribute( 'class' ) . ' mdc-select__native-control';

				$get_select->setAttribute( 'class', $select_classes );

				$get_labels = $get_span->getElementsByTagName( 'label' );
                foreach ($get_labels as $label) {
					$label->setAttribute('class', $label->getAttribute('class') . ' mdc-floating-label  mdc-floating-label--float-above');
					
					if( $type === 'outlined' ) {
						$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' );
						// }
						$get_span->appendChild( $notched_outline );
						$get_span->appendChild( $select_icon );
						$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{
						//check if the country field has placeholder
						if (isset($field->inputs[5]['placeholder']) && $field->inputs[5]['placeholder'] !== '') {
							$option = $get_span->getElementsByTagName('option')->item(0);
							$option->setAttribute('class', $option->getAttribute('class') . ' stla-material-placeholder');
						}
						//move label to be the last child of div
						$get_span->appendChild($select_icon);
						$get_span->appendChild($label);
						
					}
                    
				}
				
				if( $type === 'filled' ) {
					//create a new div
					$bottom_line_div = $dom->createElement( 'div' );
					$bottom_line_div->setAttribute( 'class', 'mdc-line-ripple' );
					$get_span->appendChild( $select_icon );
					$get_span->appendChild( $bottom_line_div );
				}
			}
		}
	}


	$content = utf8_decode( $dom->saveHTML( $dom->documentElement ) );
}


/***************************************** for name prefix dropdown  ****************************************************************************/

if ( $field->type === 'name' ) {
	$get_spans = $dom->getElementsByTagName( 'span' );
	foreach ( $get_spans as $get_span ) {
		$span_classes = $get_span->getAttribute( 'class' );
		if ( strpos( $span_classes, 'name_prefix_select' ) !== false ) {

			//create container div
			$container_div = $dom->createElement( 'div' );
		
			$container_classes = $type === 'outlined' ? 'mdc-select stla_material_large mdc-select--outlined':'mdc-select stla_material_large';
			$container_div->setAttribute('class', $container_classes );
			// $container_div->setAttribute( 'data-mdc-auto-init', 'MDCTextField' );

			$get_span->appendChild($container_div);

			$get_selects = $get_span->getElementsByTagName( 'select' );
			foreach( $get_selects as $get_select){
				$get_select->setAttribute( 'class',
				$get_select->getAttribute( 'class' ) . ' mdc-select__native-control' );
				$container_div->appendChild( $get_select );
			}
			
			//get labels 
			$get_labels = $get_span->getElementsByTagName( 'label' );
			foreach ( $get_labels as $label){
				$add_label_classes = ' mdc-floating-label';
				if( $type === 'outlined' ) {
					$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' );
					// }
					$container_div->appendChild( $notched_outline );
					$container_div->appendChild( $select_icon );
					$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 {
					$label->setAttribute( 'class', $label->getAttribute( 'class' ) . ' mdc-floating-label  mdc-floating-label--float-above' );
					$container_div->appendChild( $select_icon );
					$container_div->appendChild( $label );
				}
				
			}

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