<?php

/**
 * Choice options
 */

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

global $wpcb_field, $wpcb_field_settings, $wpcb_slider, $wpcb_vertical_slider;
if ( ! $wpcb_field_settings ) {
	wpcb_define_field_settings();
}

$wpcb_slider = false;
$wpcb_vertical_slider = false;

$wpcb_field_note_above = '';
$wpcb_field_note_below = '';
if ( isset($wpcb_field['field']['note']) && '' !== $wpcb_field['field']['note'] ) {
	$wpcb_note_position = $wpcb_field['field']['note_position'];
	if ( 'inherit' === $wpcb_note_position ) {
		$wpcb_note_position = $wpcb_field_settings['wpcb_note_position'];
	}

	if ( 'above' === $wpcb_note_position ) {
		$wpcb_field_note_above = '<div class="wpcb-configurator-field-note">' . wp_kses_post( $wpcb_field['field']['note'] ) . '</div>';
	} else {
		$wpcb_field_note_below = '<div class="wpcb-configurator-field-note">' . wp_kses_post( $wpcb_field['field']['note'] ) . '</div>';
	}
}

$wpcb_field_summary = '<div class="wpcb-configurator-field-summary"></div>';
$wpcb_field_summary_above = '';
$wpcb_field_summary_below = '';
if ( isset($wpcb_field['field']['show_summary']) && 'yes' === $wpcb_field['field']['show_summary'] ) {
	$wpcb_summary_position = $wpcb_field['field']['summary_position'];

	if ( 'inherit' === $wpcb_summary_position ) {
		$wpcb_summary_position = $wpcb_field_settings['wpcb_summary_position'];
	}

	if ( 'above' === $wpcb_summary_position ) {
		$wpcb_field_summary_above = $wpcb_field_summary;
	} else {
		$wpcb_field_summary_below = $wpcb_field_summary;
	}
}

// Track if options have long descriptions
$has_option_descriptions = true;
$show_input = false;

$min = 0; // No selection required.
if ( ! isset( $wpcb_field['field']['multiple_choice'] ) || ( 'no' === $wpcb_field['field']['multiple_choice'] && 'products' !== $wpcb_field['field']['template'] ) ) {
	$input_type = 'radio';
	$max = 1; // Single choice. Max 1.
} else {
	$input_type = 'checkbox';
	$max = 0; // Unlimited selections allowed;
	if ( isset( $wpcb_field['field']['min'] ) ) {
		$min = $wpcb_field['field']['min'];
	} else if ( 'yes' === $wpcb_field['field']['required'] ) {
		$min = 1; // Required. Min 1 selection required
	}
	if ( isset( $wpcb_field['field']['max'] ) ) {
		$max = $wpcb_field['field']['max'];
	}
}

$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'] );

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

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['id'] ); ?>"
	data-label="<?php echo esc_attr( $wpcb_field['field']['label'] ); ?>"
	<?php
	if ( $wpcb_field['field']['tax_percent'] > 0 ) {
		echo ' data-tax="' . esc_attr( (float) $wpcb_field['field']['tax_percent'] ) . '"';
	}

	if ( isset( $wpcb_field['field']['required'] ) && 'yes' === $wpcb_field['field']['required'] ) {
		echo ' data-required="required"';
	}

	echo ' data-min="' . esc_attr( $min ) . '" data-max="' . esc_attr( $max ) . '"';

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

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

	if ( $wpcb_field['field']['template'] !== 'button-group' && $wpcb_field['field']['template'] !== 'dropdown' ) {
		if ( isset( $wpcb_field['field']['items_per_row'] ) && '' !== $wpcb_field['field']['items_per_row'] ) {
			echo ' data-rows="' . esc_attr( (int) $wpcb_field['field']['items_per_row'] + 1 ) . '"';
			
			if ( isset( $wpcb_field['field']['items_per_row_tablet'] ) && '' !== $wpcb_field['field']['items_per_row_tablet'] ) {
				echo ' data-rows-tablet="' . esc_attr( (int) $wpcb_field['field']['items_per_row_tablet'] + 1 ) . '"';
			} else {
				echo ' data-rows-tablet="' . esc_attr( (int) $wpcb_field['field']['items_per_row'] + 1 ) . '"';
			}
		
			if ( isset( $wpcb_field['field']['items_per_row_mobile'] ) && '' !== $wpcb_field['field']['items_per_row_mobile'] ) {
				echo ' data-rows-mobile="' . esc_attr( (int) $wpcb_field['field']['items_per_row_mobile'] + 1 ) . '"';
			} else {
				echo ' data-rows-mobile="' . esc_attr( (int) $wpcb_field['field']['items_per_row'] + 1 ) . '"';
			}
		}
	}

	if ( function_exists( 'wpcb_configurator_output_slider_data' ) ) {
		wpcb_configurator_output_slider_data();
	}

	if ( isset( $wpcb_field['visual'] ) && is_array( $wpcb_field['visual'] ) && count( $wpcb_field['visual'] ) > 0 ) {
		foreach ( $wpcb_field['visual'] as $visualkey => $visualval ) {
			if ( '' === $visualval ) {
				continue;
			}
			echo ' data-' . esc_attr( str_replace('_', '-', $visualkey) ) . '="' . esc_attr( $visualval ) . '"';
		}
	}
	?>>

	<?php if ( ! isset( $wpcb_field['field']['label_hidden'] ) || 'yes' !== $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 : ?>

				<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();
						}
						
						if ( ! isset( $wpcb_field_settings['wpcb_values_inline'] ) || 'no' !== $wpcb_field_settings['wpcb_values_inline'] ) {
							echo esc_attr( apply_filters( 'wpcb_inline_label_value_separator', ':' ) );
						}

						wpcb_display_inline_edit_link( $wpcb_field['post_id'] );
						?>
					</p>

					<?php if ( ! isset( $wpcb_field_settings['wpcb_values_inline'] ) || 'no' !== $wpcb_field_settings['wpcb_values_inline'] ) : ?>

						<p class="wpcb-configurator-field-value"></p>
						
					<?php endif; ?>

				</div>

				<?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; ?>

			<?php endif; ?>

		</div>

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

			<?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 endif; ?>

	<?php endif; ?>

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

	<?php echo wp_kses_post( $wpcb_field_note_above ); ?>

	<?php echo wp_kses_post( $wpcb_field_summary_above ); ?>

	<div class="wpcb-configurator-field-content<?php
		if ( $wpcb_field['field']['template'] === 'swatches' && $wpcb_slider && ! $wpcb_vertical_slider ) {
			echo ' swiper';
		}
		?>">

		<?php
		global $wpcb_stock_settings;
		if ( ! $wpcb_stock_settings ) {
			wpcb_define_stock_settings();
		}

		if ( 'dropdown' === $wpcb_field['field']['template'] ) {

			if ( file_exists( WPCB_BASE . 'public/choices/' . $wpcb_field['field']['template'] . '.php' ) ) {
				include WPCB_BASE . 'public/choices/' .  $wpcb_field['field']['template'] . '.php';
			}

		} else {

			if ( $wpcb_field['field']['template'] === 'swatches' && $wpcb_slider && ! $wpcb_vertical_slider ) {
				echo '<div class="swiper-wrapper">';
			}

			foreach ( $wpcb_field['options'] as $wpcb_option_index => $wpcb_option ) {
				$wpcb_option_id = wpcb_sanitize_title( $wpcb_option_index );

				$zoom_html = '';
				$swatch_html = '';
				if ( 'color' === $wpcb_field['field']['type'] ) {
					$swatch_html = '<span class="wpcb-swatch-fill" style="background: ' . $wpcb_option['color'] . '"></span>';
				} else {
					$swatch_html = wp_get_attachment_image( $wpcb_option['thumbnail'], 'full', null, array( 'class' => 'wpcb-swatch-image' ) );

					if ( isset( $wpcb_field['field']['image_zoom'] ) && 'yes' === $wpcb_field['field']['image_zoom'] ) {
						$zoom_url = wp_get_attachment_image_url( $wpcb_option['thumbnail'], 'full' );
						$data_attr = ' data-wpcb-lightbox="gallery"';
						if ( isset( $wpcb_field_settings['wpcb_lightbox'] ) && 'theme' === $wpcb_field_settings['wpcb_lightbox'] ) {
							$data_attr = '';
						}

						$zoom_html = '<a href="' . $zoom_url . '" class="wpcb-zoom-icon"' . $data_attr . '>' . wpcb_get_icon( 'zoom' ) . '</a>';
					}
				}

				if ( 'color' !== $wpcb_field['field']['type'] && ( ! isset( $wpcb_field['field']['show_thumbnail'] ) || 'no' === $wpcb_field['field']['show_thumbnail'] ) ) {
					$show_input = true;
					$swatch_html = '';
				}
	
				$option_class = '';
				if ( 'swatches' === $wpcb_field['field']['template'] ) {
					$option_class .= ' wpcb-swatch-option';
				}
				if ( 'products' === $wpcb_field['field']['template'] ) {
					$option_class .= ' wpcb-option-product';
					if ( isset( $wpcb_field['field']['multiple_choice'] ) && 'yes' === $wpcb_field['field']['multiple_choice'] ) {
						$option_class .= ' wpcb-option-product-input';
					}
				}

				if ( 'label' === $wpcb_field['field']['type'] && '' !== $swatch_html ) {
					$option_class .= ' wpcb-option-type-image';
				} else {
					$option_class .= ' wpcb-option-type-' . $wpcb_field['field']['type'];
				}
				if ( $show_input ) {
					$option_class .= ' wpcb-option-has-input';
				}

				if ( $wpcb_field_settings['wpcb_checked_option_style'] == 'icon' ) {
					$option_class .= ' wpcb-option-alt';
				}

				$visual_preview = ' data-id="' . wpcb_sanitize_title( $wpcb_option_id ) . '"';
				if ( isset( $wpcb_option['sale_price'] ) ) {
					$visual_preview .= ' data-sale="' . $wpcb_option['sale_price'] . '"';
				}
				if ( isset( $wpcb_option['discount_price'] ) ) {
					$visual_preview .= ' data-discount="' . $wpcb_option['discount_price'] . '"';
					$visual_preview .= ' data-discount-rules="' . str_replace( '"', "'", $wpcb_option['discount_rules'] ) . '"';
				}

				$data_keys = apply_filters( 
					'wpcb_field_data_keys', 
					array(
						'price',
						'key',
						'preview_value',
						'url',
						'sku',
						'field',
						'percent',
						'formula',
						'price_table',
						'table_field',
						'stock',
						'weight',
						'product_id'
					)
				);
				foreach ( $data_keys as $data_key ) {
					if ( isset( $wpcb_option[ $data_key ] ) && '' !== $wpcb_option[ $data_key ] ) {
						$visual_preview .= sprintf( 
							' data-%s="%s"',
							str_replace( '_', '-', $data_key ),
							$wpcb_option[ $data_key ]
						);
					}
				}

				$in_stock = true;
				if ( isset( $wpcb_option['stock'] ) && '' !== $wpcb_option['stock'] ) {
					if ( 0 >= (int) $wpcb_option['stock'] ) {
						if ( $wpcb_stock_settings['wpcb_hide_out_of_stock_options'] ) {
							continue; // Don't include out of stock option.
						}

						if ( ! $wpcb_stock_settings['wpcb_allow_backorders'] ) {
							$in_stock = false;
							$option_class .= ' wpcb-option-out-of-stock';
							$visual_preview .= ' disabled="disabled"';
						}
					}
				}

				$inline_style = '';
				if ( 'font' === $wpcb_field['field']['type'] && isset( $wpcb_option['preview_value'] ) && '' !== $wpcb_option['preview_value'] ) {
					$font_values = array_map( 'trim', explode(';', $wpcb_option['preview_value'] ) );

					if ( isset( $font_values[0] ) ) {
						$inline_style .= 'font-family: ' . $font_values[0] . ';';
					}
					if ( isset( $font_values[1] ) ) {
						$inline_style .= 'font-weight: ' . $font_values[1] . ';';
					}
					if ( isset( $font_values[2] ) ) {
						$inline_style .= 'font-style: ' . $font_values[2] . ';';
					}
				}

				if ( '' !== $inline_style ) {
					$inline_style = ' style="' . $inline_style . '"';
				}

				/**
				 * Hook to output content before the choice option
				 * 
				 * @hooked -
				*/
				do_action( 'wpcb_before_choice_option' );
				?>

				<label 
					for="<?php echo esc_attr( $wpcb_option_id ); ?>"
					class="wpcb-option<?php echo esc_attr( $option_class ); ?>"<?php 
					if ( $in_stock ) {
						echo ' aria-controls="' . esc_attr( $wpcb_option_id ) . '"';
						echo ' aria-label="' . esc_attr( $wpcb_field['field']['label'] ) . '"';
						echo ' aria-role="button" tabindex="0"';
					} else {
						echo ' title="' . esc_attr__( 'Out of stock', 'wcb-configurator-builder' ) . '"';
					}

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

					if ( isset( $wpcb_option['template'] ) && is_array( $wpcb_option['template'] ) ) {
						echo ' data-template="' . wp_kses_post( urldecode( str_replace( '"', "'", wp_json_encode( $wpcb_option['template'] ) ) ) ) . '"';
					}

					if ( isset( $wpcb_field['field']['default'] ) && is_array( $wpcb_field['field']['default'] ) &&
						( in_array( $wpcb_option_id, $wpcb_field['field']['default'] ) || in_array( $wpcb_option['label'], $wpcb_field['field']['default'] ) ) ) {
						echo ' data-default="true"';
					}
					?>>

					<?php
					$name = wpcb_sanitize_title( $wpcb_field['field']['label'] );
					if ( isset( $wpcb_field['name'] ) ) {
						$name = wpcb_sanitize_title( $wpcb_field['name'] );
					}

					echo '<input id="' . esc_attr( $wpcb_option_id ) . '" type="' . esc_attr( $input_type ) . '" name="' . esc_attr( $name ) . '" value="' . esc_attr( $wpcb_option['label'] ) . '"';
					echo ( 'yes' === $wpcb_field['field']['required'] && $input_type !== 'checkbox' ) ? 'required' : '';
					echo wp_kses_post( $visual_preview );
					echo ' data-label="' . esc_attr( $wpcb_option['label'] ) . '"';
					echo '>';

					if ( file_exists( WPCB_BASE . 'public/choices/' . $wpcb_field['field']['template'] . '.php' ) ) {
						include WPCB_BASE . 'public/choices/' .  $wpcb_field['field']['template'] . '.php';
					}

					/**
					 * Hook to output content in the choice option
					 * 
					 * @hooked -
					*/
					do_action( 'wpcb_choice_option' );

					if ( isset( $wpcb_option['description'] ) && '' !== $wpcb_option['description'] ) {
						$has_option_descriptions = true;
						$option_description_data = '';
						if ( isset( $wpcb_option['gallery'] ) && '' !== $wpcb_option['gallery'] ) {
							$option_description_data = ' data-images="' . str_replace( '"', "'", $wpcb_option['gallery'] ) . '"';
						}
						?>
						<div class="wpcb-option-description-html"<?php echo wp_kses_post( $option_description_data ); ?>>
							<?php echo wp_kses_post( wpautop( $wpcb_option['description'] ) ); ?>
						</div>
						<?php
					}
					?>
				</label>
				<?php
				/**
				 * Hook to output content after the choice option
				 * 
				 * @hooked -
				*/
				do_action( 'wpcb_after_choice_option' );
			}
		}
		?>
	</div>

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

	<?php echo wp_kses_post( $wpcb_field_summary_below ); ?>

	<?php echo wp_kses_post( $wpcb_field_note_below ); ?>

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

		<?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 = '';
		if ( isset( $wpcb_field['field']['description'] ) ) {
			$description = $wpcb_field['field']['description'];
		}

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

	<?php endif; ?>

</div>
