<?php
/***************************************** for input fields like text, numbers, email and website **************************************/

if ( in_array( $field->type, $date_field ) ) {
	//$input_classes_types = array( 'ginput_container_date' );
	$contains_error = false;
	$contains_placeholder = false;
	$input_id = '';
	$all_divs = $dom->getElementsByTagName( 'div' );
	$is_date_boxes_field = false;
	$date_boxes_classes = array( 'gfield_date_day', 'gfield_date_month', 'gfield_date_year' );
	$is_date_dropdown_field = false;
	$date_dropdown_classes = array( 'gfield_date_dropdown_day', 'gfield_date_dropdown_month', 'gfield_date_dropdown_year' );

	//get the first div
	foreach ( $all_divs as $div ) {
		$div_classes = $div->getAttribute( 'class' );

		$div_classes_array = explode( ' ', $div_classes );


		$is_valid_input_field = false;

		foreach ( $div_classes_array as $div_class_value ) {
			// if ( in_array( $div_class_value, $input_classes_types ) ) {
			//  $is_valid_input_field = true;
			// }

				if ( strpos( $div_classes, 'validation_message' ) !== false ) {
					$contains_error = true;
					$div->setAttribute( 'class', $div->getAttribute( 'class' ) . ' invalid-feedback' );
				}

			if ( in_array( $div_class_value, $date_boxes_classes ) ) {
				$is_date_boxes_field = true;
			}
			if ( in_array( $div_class_value, $date_dropdown_classes ) ) {
				$is_date_dropdown_field = true;
			}
		}
	}
	foreach ( $all_divs as $div ) {
		//var_dump( $is_date_boxes_field );
		// for datepicker date field
		if (  ! $is_date_dropdown_field && ! $is_date_boxes_field ) {

			//add class to input
			$inputs = $div->getElementsByTagName( 'input' );
			foreach ( $inputs as $input ) {
				$id = $input->getAttribute( 'id' );
				//$input_type = $input->getAttribute( 'type' );
				if ( preg_match( '/^input_/', $id ) ) {
					$input_id = $id;
					$input_classes = $input->getAttribute( 'class' );
					
					//if has error then add the error class
					if ( $contains_error ) {
						$input_classes .= ' is-invalid' ;
					}
					$input->setAttribute( 'class', $input_classes . ' form-control' );
				}
			}
			//add classes to div
			$add_div_classes = ' form-group ';
			
			$div->setAttribute( 'class', $div->getAttribute( 'class' ) . $add_div_classes );

		}

		//for date boxes date field

		if ( $is_date_boxes_field ) {
			$div_classes = $div->getAttribute( 'class' );
		$div_classes_array = explode( ' ', $div_classes );
			$is_valid_box_div = false;
			foreach ( $div_classes_array as $div_class_value ) {
				if ( in_array( $div_class_value, $date_boxes_classes ) ) {
					$is_valid_box_div = true;
				}
			}
			if ( $is_valid_box_div ) {
				//add class to input
				$inputs = $div->getElementsByTagName( 'input' );
				foreach ( $inputs as $input ) {
					$id = $input->getAttribute( 'id' );

					//$input_type = $input->getAttribute( 'type' );
					if ( preg_match( '/^input_/', $id ) ) {
						$input_id = $id;
						$input_classes = $input->getAttribute( 'class' );
						//if has error then add the error class
						if ( $contains_error ) {
							$input_classes .= ' is-invalid';
						}
						$input->setAttribute( 'class', $input_classes . ' form-control' );
					
					}
				}



				//add classes to div
				$add_div_classes = ' form-group ';
				
				$div->setAttribute( 'class', $div->getAttribute( 'class' ) . $add_div_classes );


				
			}
		}


		//for date dropdown fields

		if ( $is_date_dropdown_field ) {
			$div_classes = $div->getAttribute( 'class' );
			$div_classes_array = explode( ' ', $div_classes );
			$is_valid_dropdown_div = false;
			foreach ( $div_classes_array as $div_class_value ) {
				if ( in_array( $div_class_value, $date_dropdown_classes ) ) {
					$is_valid_dropdown_div = true;
				}
			}
			//var_dump($is_valid_dropdown_div);
			if ( $is_valid_dropdown_div ) {
				//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 .= ' form-control';
				if ( $contains_error ) {
					$select_classes .= ' is-invalid';
				}
				$get_select->setAttribute( 'class', $select_classes );
			}

	

			$div->setAttribute( 'class', $div->getAttribute( 'class' ) . ' form-group' );

		}
	}

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

}
