<?php

/**
 * Product option
 * 
 */

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

$products_control = 'button';
if ( isset( $wpcb_field['field']['multiple_choice'] ) && 'yes' === $wpcb_field['field']['multiple_choice'] ) {
	$products_control = 'input';
}
?>

<div class="wpcb-option-item wpcb-option-item-<?php echo esc_attr( $wpcb_field['field']['product_template'] ); ?>">

	<?php if ( $swatch_html ) : ?>
		<div class="wpcb-option-thumbnail">
			<?php echo wp_kses_post( $zoom_html ); ?>
			<?php echo wp_kses_post( $swatch_html ); ?>
		</div>
		<?php
		if ( function_exists( 'wpcb_get_option_stock_html' ) ) :
			$stock_html = wpcb_get_option_stock_html( $wpcb_option );
			echo wp_kses_post( $stock_html );
		endif;
		?>
	<?php endif; ?>
	
	<div class="wpcb-option-details">
		<div class="wpcb-option-header">
			<strong class="wpcb-option-label">
				<?php echo esc_attr( $wpcb_option['label'] ); ?>
			</strong>
			<?php if ( isset( $wpcb_option['description'] ) && '' !== $wpcb_option['description'] ) : ?>
				<button data-id="<?php echo esc_attr( $wpcb_option_id ); ?>" class="wpcb-view-option-details">
					<?php echo wp_kses( wpcb_get_icon( 'info' ), wpcb_get_icon_kses_args() ); ?>
				</button>
			<?php endif; ?>
		</div>
		<?php
		if ( function_exists( 'wpcb_display_product_variation_form_html' ) ) {
			wpcb_display_product_variation_form_html( $wpcb_option );
		}
		?>
		<p class="wpcb-option-note">
			<?php echo esc_attr( $wpcb_option['note'] ); ?>
		</p>
		<?php
		if ( ! $swatch_html ) {
			if ( function_exists( 'wpcb_get_option_stock_html' ) ) :
				$stock_html = wpcb_get_option_stock_html( $wpcb_option );
				echo wp_kses_post( $stock_html );
			endif;
		}
		?>
	</div>
	<div class="wpcb-option-content">
		<div class="wpcb-option-totals">

			<?php 
			if ( ! isset( $wpcb_field['field']['price_hidden'] ) || 'yes' !== $wpcb_field['field']['price_hidden'] ) {
				wpcb_display_option_price_wrapper_html( $wpcb_option, true );
			}
			?>

		</div>
		<div class="wpcb-option-action">

			<?php
			if ( 'button' === $products_control ) :
				$suffix = wpcb_get_i18n_suffix();
				$add_label = wpcb_get_option( 'wpcb_button_product_add_text', $suffix ) ?: __( 'Add', 'wcb-configurator-builder' );
				$remove_label = wpcb_get_option( 'wpcb_button_product_remove_text', $suffix ) ?: __( 'Remove', 'wcb-configurator-builder' );
				?>
				<div class="wpcb-button add"><?php echo esc_attr( $add_label ); ?></div>
				<div class="wpcb-button remove"><?php echo esc_attr( $remove_label ); ?></div>
				<?php
			else : 
				$min = max( $wpcb_option['min'], 0 );
				$max = '';

				if ( ! get_option( '_wpcb_allow_backorders' ) && ( ! isset( $wpcb_option['max'] ) || $wpcb_option['max'] > $wpcb_option['stock'] ) ) {
					$wpcb_option['max'] = $wpcb_option['stock'];
				}

				if ( isset( $wpcb_option['max'] ) && 0 < $wpcb_option['max'] ) {
					$max = ' max=' . $wpcb_option['max'] . ' data-max=' . $wpcb_option['max'] . '';
				}
				?>
				<input type="number" 
					min="<?php echo esc_attr( $min ); ?>"<?php echo esc_attr( $max ); ?>
					value="<?php echo esc_attr( $min ); ?>"<?php
					if ( ! $in_stock ) {
						echo ' aria-disabled="true" disabled';
					}
					?>>
			<?php endif; ?>

		</div>
	</div>
</div>
