<?php

/**
 * Choice options
 */

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

global $wpcb_field;

$wpcb_field_id = wpcb_sanitize_title( $wpcb_field['id'] );

$class = 'wpcb-configurator-field wpcb-configurator-input wpcb-configurator-' . $wpcb_field['field']['type'];
if ( isset( $wpcb_field['field']['class'] ) && '' !== $wpcb_field['field']['class'] ) {
	$class .= ' ' . $wpcb_field['field']['class'];
}

$label_visible = ( ! isset( $wpcb_field['field']['label_hidden'] ) || 'yes' !== $wpcb_field['field']['label_hidden'] );
$price_visible = ( ! isset( $wpcb_field['field']['price_hidden'] ) || 'yes' !== $wpcb_field['field']['price_hidden'] );

if ( isset( $wpcb_field['field']['collapsible'] ) && $wpcb_field['field']['collapsible'] ) {
	$class .= ' wpcb-collapsible';

	if ( 'no' !== $wpcb_field['field']['collapsed'] ) {
		$class .= ' wpcb-collapsed';
	}
}

if ( isset( $wpcb_field['field']['hidden'] ) && $wpcb_field['field']['hidden'] ) {
	$class .= ' wpcb-hidden';
}
?>
<div 
	id="wpcb-configurator-field-<?php echo esc_attr( $wpcb_field['id'] ); ?>"
	class="<?php echo esc_attr( $class ); ?>"
	data-id="<?php echo esc_attr( $wpcb_field['post_id'] ); ?>"
	data-label="<?php echo esc_attr( $wpcb_field['field']['label'] ); ?>"
	<?php 
	if ( isset( $wpcb_field['field']['required'] ) && 'yes' === $wpcb_field['field']['required'] ) {
		echo ' data-required="required"';
	}

	if ( ! $label_visible ) {
		echo ' data-label-hidden="1"';
	}
	if ( ! $price_visible ) {
		echo ' data-price-hidden="1"';
	}

	if ( $wpcb_field['field']['tax_percent'] > 0 ) {
		echo ' data-tax="' . esc_attr( (float) $wpcb_field['field']['tax_percent'] ) . '"';
	}

	if ( isset( $wpcb_field['rules'] ) ) {
		echo ' data-display-rules="' . wp_kses_post( urldecode( str_replace( '"', "'", wp_json_encode( $wpcb_field['rules'] ) ) ) ) . '"';
	}
	?>>

	<?php
	/**
	 * Hook to output content before the field opening wrapper
	 * 
	 * @hooked -
	*/
	do_action( 'wpcb_before_input_field_content' );
	?>

	<label for="<?php echo esc_attr( $wpcb_field_id ); ?>" class="wpcb-input-field">

		<?php if ( 'true-false' !== $wpcb_field['field']['type'] ) : ?>

			<?php if ( ! isset( $wpcb_field['field']['label_hidden'] ) || ! $wpcb_field['field']['label_hidden'] ) : ?>

				<div class="wpcb-configurator-field-header">

					<?php if ( isset( $wpcb_field['field']['collapsible'] ) && $wpcb_field['field']['collapsible'] ) : ?>

						<?php if ( isset( $wpcb_field['field']['description'] ) && '' !== $wpcb_field['field']['description'] && $wpcb_field_settings['wpcb_description_position'] == 'above' ) : ?>

							<?php echo wpcb_display_description_button_html( $wpcb_field_settings ); ?>
							
						<?php endif; ?>

						<div class="wpcb-configurator-field-title">

							<p class="wpcb-configurator-field-label">
								<?php
								echo esc_attr( $wpcb_field['field']['label'] );
								
								if ( isset( $wpcb_field['field']['required'] ) && 'yes' === $wpcb_field['field']['required'] ) {
									wpcb_display_required_icon_html();
								}

								wpcb_display_inline_edit_link( $wpcb_field['post_id'] );
								?>
							</p>
							<p class="wpcb-configurator-field-value"></p>
						</div>

						<a href="#" class="wpcb-configurator-field-collapse-button">
							<?php echo wp_kses( wpcb_get_icon( 'chevron-down' ), wpcb_get_icon_kses_args() ); ?>
						</a>

					<?php else : ?>
							
						<strong class="wpcb-configurator-field-label">
							<?php
							echo esc_attr( $wpcb_field['field']['label'] );
							
							if ( isset( $wpcb_field['field']['required'] ) && 'yes' === $wpcb_field['field']['required'] ) {
								wpcb_display_required_icon_html();
							}
							
							wpcb_display_inline_edit_link( $wpcb_field['post_id'] );
							?>
						</strong>

						<div class="wpcb-configurator-field-totals">

							<?php if ( ! isset( $wpcb_field['field']['price_hidden'] ) || 'yes' !== $wpcb_field['field']['price_hidden'] ) : ?>

								<?php if ( isset( $wpcb_field['field']['price'] ) && '' !== $wpcb_field['field']['price'] ) : ?>

									<p class="wpcb-input-field-price">
										<?php 
										$sale = isset( $wpcb_field['field']['sale'] ) ? $wpcb_field['field']['sale'] : false;
										echo wp_kses_post( wpcb_get_price_html( $wpcb_field['field']['price'], $sale ) );
										?>
									</p>

								<?php elseif ( isset( $wpcb_field['field']['price_unit'] ) && '' !== $wpcb_field['field']['price_unit'] ) : ?>

									<p class="wpcb-input-field-price">
										<?php 
										$unit_sale = isset( $wpcb_field['field']['price_unit_sale'] ) ? $wpcb_field['field']['price_unit_sale'] : false;
										echo wp_kses_post( wpcb_get_price_html( $wpcb_field['field']['price_unit'], $unit_sale ) ); 
										?>
									</p>

								<?php elseif ( isset( $wpcb_field['field']['price_table'] ) && '' !== $wpcb_field['field']['price_table']  ) : ?>

									<p class="wpcb-input-field-price"></p>

								<?php endif; ?>

							<?php endif; ?>

							<?php if ( isset( $wpcb_field['field']['description'] ) && '' !== $wpcb_field['field']['description'] && $wpcb_field_settings['wpcb_description_position'] == 'above' ) : ?>

								<?php echo wpcb_display_description_button_html( $wpcb_field_settings ); ?>
								
							<?php endif; ?>

						</div>

					<?php endif; ?>

				</div>

			<?php endif; ?>

		<?php endif; ?>

		<?php
		$wpcb_input_content_class = '';
		if ( isset( $wpcb_field['field']['unit'] ) && '' !== $wpcb_field['field']['unit'] ) {
			$wpcb_input_content_class .= ' wpcb-input-content-unit';
		}
		?>

		<div class="wpcb-input-content<?php echo esc_attr( $wpcb_input_content_class ); ?>"<?php 
			if ( 'range-slider' === $wpcb_field['field']['type'] ) {
				echo ' style="--min:' . esc_attr( $wpcb_field['field']['min'] ) . ';--max:' . esc_attr( $wpcb_field['field']['max'] ) . ';--val:' . esc_attr( $wpcb_field['field']['value'] ) . '"';
			}
			?>>

			<?php
			/**
			 * Hook to output content before the field
			 * 
			 * @hooked -
			*/
			do_action( 'wpcb_before_input_field' );
			?>

			<?php
			if ( file_exists( WPCB_BASE . 'public/inputs/' . $wpcb_field['field']['type'] . '.php' ) ) {
				include WPCB_BASE . 'public/inputs/' .  $wpcb_field['field']['type'] . '.php';
			}
			?>

			<?php
			/**
			 * Hook to output content after the field
			 * 
			 * @hooked -
			*/
			do_action( 'wpcb_after_input_field' );
			?>

		</div>

		<?php if ( 'true-false' !== $wpcb_field['field']['type'] ) : ?>

			<div class="wpcb-input-footer">

				<?php if ( isset( $wpcb_field['field']['note'] ) ) : ?>

					<small class="wpcb-input-field-note">
						<?php echo wp_kses_post( $wpcb_field['field']['note'] ); ?>
					</small>

				<?php endif; ?>

			</div>

		<?php endif; ?>

	</label>

	<?php 
	/**
	 * Hook to output content before the field opening wrapper
	 * 
	 * @hooked -
	*/
	do_action( 'wpcb_after_input_field_content' );
	?>

	<?php if ( isset( $wpcb_field['field']['description'] ) && '' !== $wpcb_field['field']['description'] ) : ?>

		<?php if ( isset( $wpcb_field['field']['description'] ) && '' !== $wpcb_field['field']['description'] && $wpcb_field_settings['wpcb_description_position'] == 'below' ) : ?>

			<?php echo wpcb_display_description_button_html( $wpcb_field_settings ); ?>
			
		<?php endif; ?>

		<?php 
		$description_data = '';
		if ( isset( $wpcb_field['field']['gallery'] ) && '' !== $wpcb_field['field']['gallery'] ) {
			$gallery_ids = [];
			foreach ( $wpcb_field['field']['gallery'] as $gallery_id ) {
				$gallery_ids[] = wp_get_attachment_image( $gallery_id, 'full' );
			}
			$description_data = ' data-images="' . str_replace( '"', "'", implode('', $gallery_ids) ) . '"';
		}
		?>

		<div class="wpcb-configurator-field-description"<?php echo wp_kses_post( $description_data ); ?>>
			<?php echo wp_kses( wpautop( $wpcb_field['field']['description'] ), wpcb_get_html_kses_args() ); ?>
		</div>

		<?php
		wpcb_display_panel_html_safe( 
			$wpcb_field['field']['label'],
			array(
				'description' => wpautop( $wpcb_field['field']['description'] )
			)
		);
		?>

	<?php endif; ?>

</div>