<?php
/**
 * Main options form
 * 
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $wpcb_configuration_id, $wpcb_form_fields, $wpcb_field, $wpcb_step_count;

$wpcb_form_fields = WPCB_Formatter::get_configurator_form_fields( $wpcb_configuration_id );

$wpcb_step_index = 1;
$wpcb_step_count = 0;

$wpcb_tab_index = 0;
$wpcb_tab_count = 0;
$wpcb_tab_lists = array();
$wpcb_tab_closed = true;

$wpcb_form_class = '';
if ( wpcb_get_option( 'wpcb_border_style' ) ) {
	$wpcb_form_class .= ' wpcb-border-' . wpcb_get_option( 'wpcb_border_style' );
}
if ( 'vertical' === wpcb_get_option( 'wpcb_tab_display' ) ) {
	$wpcb_form_class .= ' wpcb-tabbed-vertical';
}

if ( function_exists( 'wpcb_configurator_form_leave_popup' ) && wpcb_get_option('wpcb_show_leave_popup') ) {
	wpcb_configurator_form_leave_popup();
}
?>

<div class="wpcb-configurator-form<?php echo esc_attr( $wpcb_form_class ); ?>"<?php echo ' data-wpcb-id="' . esc_attr( $wpcb_configuration_id ) . '"'; ?>>

	<?php
	foreach ( $wpcb_form_fields as $wpcb_index => $wpcb_field ) :
		if ( 'step' === $wpcb_field['template'] ) {
			if ( $wpcb_step_count > 0 ) {
				echo '</div>';
			}
			
			echo '<div id="step-' . esc_attr( $wpcb_field['id'] ) . '" data-step-index="' . esc_attr( $wpcb_step_index ) . '" class="wpcb-configurator-step"';
			if ( isset( $wpcb_field['button'] ) && '' !== $wpcb_field['button'] ) {
				echo 'data-step-button="' . esc_attr( $wpcb_field['button'] ) . '"';
			}
			echo '>';

			if ( 'yes' !== $wpcb_field['hidden'] ) {
				echo '<div class="wpcb-configurator-step-header">';
				echo '<p class="wpcb-configurator-step-title">' . esc_attr( $wpcb_field['title'] ) . '</p>';
				echo '<div class="wpcb-configurator-step-description">' . wp_kses( wpautop( $wpcb_field['description'] ), wpcb_get_html_kses_args() ) . '</div>';
				echo '</div>';
			}

			$step_field = $wpcb_field;

			if ( isset( $step_field['subfields'] ) && is_array( $step_field['subfields'] ) ) {

				foreach ( $step_field['subfields'] as $subindex => $wpcb_field ) {
					if ( 'tab' === $wpcb_field['template'] ) {

						if ( isset( $wpcb_form_fields[ $wpcb_index -1 ] ) && 'tab' !== $wpcb_form_fields[ $wpcb_index -1 ]['template'] ) {
							$wpcb_tab_index++;
						}

						if ( ! array_key_exists( 'tablist-' . $wpcb_tab_index, $wpcb_tab_lists ) ) {
							$wpcb_tab_lists[ 'tablist-' . $wpcb_tab_index ] = array();
							$wpcb_tab_closed = false;

							echo '<div class="wpcb-configurator-tabs">';
							echo '<nav class="wpcb-configurator-tab-list"><ul id="tablist-' . esc_attr( $wpcb_tab_index ) . '"></ul></nav>';
						}

						$tab_control = '<li><a href="#wpcb-tab-' . esc_attr( $wpcb_field['id'] ) . '">';
						if ( isset( $wpcb_field['icon'] ) && '' !== $wpcb_field['icon'] ) {
							$tab_control .= wp_kses_post( $wpcb_field['icon'] );
						}
						if ( isset( $wpcb_field['title'] ) && '' !== $wpcb_field['title'] ) {
							if ( isset( $wpcb_field['icon'] ) && '' !== $wpcb_field['icon'] ) {
								$tab_control .= '<span>' . esc_attr( $wpcb_field['title'] ) . '</span>';
							} else {
								$tab_control .= esc_attr( $wpcb_field['title'] );
							}
						}
						$tab_control .= '</a></li>';

						$wpcb_tab_lists[ 'tablist-' . $wpcb_tab_index ][] = $tab_control;

						echo '<div id="wpcb-tab-' . esc_attr( $wpcb_field['id'] ) . '" class="wpcb-configurator-tab-content">';

						$tab_field = $wpcb_field;

						if ( isset( $tab_field['subfields'] ) && is_array( $tab_field['subfields'] ) ) {
							foreach ( $tab_field['subfields'] as $wpcb_field ) {
								$template_path = WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php';
								$template_path = apply_filters( 'wpcb_form_options_template_path', $template_path, $wpcb_field );

								if ( file_exists( $template_path ) ) {
									include $template_path;
								}
							} 
						}

						echo '</div><!-- /.wpcb-configurator-tab-content -->';

						$wpcb_tab_count++;
					} else {
						if ( ! $wpcb_tab_closed ) {
							echo '</div>';
							$wpcb_tab_closed = true;
						}
						
						$template_path = WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php';
						$template_path = apply_filters( 'wpcb_form_options_template_path', $template_path, $wpcb_field );

						if ( file_exists( $template_path ) ) {
							include $template_path;
						}
					}
				}
			}

			$wpcb_step_count++;
			$wpcb_step_index++;

		} else if ( 'tab' === $wpcb_field['template'] ) {

			if ( isset( $wpcb_form_fields[ $wpcb_index -1 ] ) && 'tab' !== $wpcb_form_fields[ $wpcb_index -1 ]['template'] ) {
				$wpcb_tab_index++;
			}

			if ( ! array_key_exists( 'tablist-' . $wpcb_tab_index, $wpcb_tab_lists ) ) {
				$wpcb_tab_lists[ 'tablist-' . $wpcb_tab_index ] = array();
				$wpcb_tab_closed = false;

				echo '<div class="wpcb-configurator-tabs">';
				echo '<nav class="wpcb-configurator-tab-list"><ul id="tablist-' . esc_attr( $wpcb_tab_index ) . '"></ul></nav>';
			}

			$tab_control = '<li><a href="#wpcb-tab-' . esc_attr( $wpcb_field['id'] ) . '">';
			if ( isset( $wpcb_field['icon'] ) && '' !== $wpcb_field['icon'] ) {
				$tab_control .= wp_kses_post( $wpcb_field['icon'] );
			}
			if ( isset( $wpcb_field['title'] ) && '' !== $wpcb_field['title'] ) {
				if ( isset( $wpcb_field['icon'] ) && '' !== $wpcb_field['icon'] ) {
					$tab_control .= '<span>' . esc_attr( $wpcb_field['title'] ) . '</span>';
				} else {
					$tab_control .= esc_attr( $wpcb_field['title'] );
				}
			}
			$tab_control .= '</a></li>';

			$wpcb_tab_lists[ 'tablist-' . $wpcb_tab_index ][] = $tab_control;

			echo '<div id="wpcb-tab-' . esc_attr( $wpcb_field['id'] ) . '" class="wpcb-configurator-tab-content">';

			$tab_field = $wpcb_field;

			if ( isset( $tab_field['subfields'] ) && is_array( $tab_field['subfields'] ) ) {
				foreach ( $tab_field['subfields'] as $wpcb_field ) {
					$template_path = WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php';
					$template_path = apply_filters( 'wpcb_form_options_template_path', $template_path, $wpcb_field );

					if ( file_exists( $template_path ) ) {
						include $template_path;
					}
				} 
			}

			echo '</div><!-- /.wpcb-configurator-tab-content -->';

			$wpcb_tab_count++;
		} else {
			if ( ! $wpcb_tab_closed ) {
				echo '</div>';
				$wpcb_tab_closed = true;
			}

			$template_path = WPCB_BASE . 'public/partials/' . $wpcb_field['template'] . '.php';
			$template_path = apply_filters( 'wpcb_form_options_template_path', $template_path, $wpcb_field );

			if ( file_exists( $template_path ) ) {
				include $template_path;
			}
		}
	endforeach;

	if ( ! $wpcb_tab_closed ) {
		echo '</div>';
		$wpcb_tab_closed = true;
	}

	if ( $wpcb_step_count > 0 ) {
		echo '</div>';
	}
	
	if ( count( $wpcb_tab_lists ) > 0 ) {
		echo '<script>';
		foreach ( $wpcb_tab_lists as $wpcb_tab_index => $tab_list_html ) {
			echo 'document.getElementById("' . esc_attr( $wpcb_tab_index ) . '").innerHTML = `' . wp_kses_post( implode( '', $tab_list_html ) ) . '`;';
		}
		echo '</script>';
	}

	if ( is_user_logged_in() && current_user_can('administrator') ) {
		?>
		<a href="<?php echo esc_url( admin_url() ) . 'post.php?post=' . esc_attr( $wpcb_configuration_id ) . '&action=edit'; ?>" class="admin-edit-link">
			<?php echo esc_attr__( 'Edit configurator', 'wcb-configurator-builder' ); ?>
		</a>
		<?php
	}
	?>
	<div class="wpcb-configurator-form-control-actions">
		<?php
		$wpcb_actions = carbon_get_post_meta( $wpcb_configuration_id, 'form_actions' );

		if ( in_array( 'reset', $wpcb_actions ) && function_exists( 'wpcb_configurator_form_reset_button' ) ) :
			wpcb_configurator_form_reset_button();
		endif;

		if ( in_array( 'save', $wpcb_actions ) && function_exists( 'wpcb_configurator_form_save_button' ) ) :
			wpcb_configurator_form_save_button();
		endif;
		?>
	</div>
	<div class="wpcb-notice-wrapper">
		<!-- dynamically set notices -->
	</div>
</div>