<?php

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

?>
<div class="wpcb-configurator-product-summary" data-wpcb-id="<?php echo esc_attr( $wpcb_configuration_id ); ?>">

	<?php 
	global $product;
	$price_visible = 'no' !== get_post_meta( $wpcb_configuration_id, '_show_price', true );
	if ( ! is_user_logged_in() && wpcb_get_option( 'wpcb_show_prices_logged_in_users' ) ) {
		$price_visible = false;
	}

	$product_details = wpcb_get_product_attributes( $wpcb_configuration_id );
	$product_name = $product_details['title'];
	$price = $product_details['price'];
	$sale_price = $product_details['sale'];
	
	if ( 'page' == get_post_meta( $wpcb_configuration_id, '_template_display', true ) ) :
		if ( $product_details['sku'] || $product_details['sku_format'] ) :
			?>

			<p class="wpcb-configurator-product-sku"<?php 
				if ( $product_details['sku_format'] ) {
					echo ' data-format="' . esc_attr( $product_details['sku_format'] ) . '"';
				} ?>>
				<?php echo esc_attr( $product_details['sku'] ); ?>
			</p>

			<?php 
		endif;
	else :
		?>

		<div class="wpcb-configurator-product-header">
			<div class="wpcb-configurator-product-title">
				<strong><?php echo esc_attr( wpcb__( $product_name ) ); ?></strong>

				<?php if ( $product_details['sku'] || $product_details['sku_format'] ) : ?>

					<p class="wpcb-configurator-product-sku"<?php 
						if ( $product_details['sku_format'] ) {
							echo ' data-format="' . esc_attr( $product_details['sku_format'] ) . '"';
						} ?>>
						<?php echo esc_attr( $product_details['sku'] ); ?>
					</p>
					
				<?php endif; ?>

				<?php if ( $price_visible ) : ?>

					<p class="wpcb-configurator-product-price">
						<?php
						if (!get_post_meta( $wpcb_configuration_id, '_change_price_display', true)){
							echo wp_kses_post( wpcb_get_price_html( $price, $sale_price, true ) );
						} else {
							$format = get_post_meta( $wpcb_configuration_id, '_price_display', true );
							$max_price = get_post_meta( $wpcb_configuration_id, '_max_price', true );
							if ( '' === $max_price ) {
								if ( is_a( $product, 'WC_Product_Variable' ) ) {
									$max_price = $product->get_variation_price('max');
								} else {
									$max_price = wpcb_get_configuration_price_max( $wpcb_configuration_id );
								}
							}
							
							$min_price = wpcb_get_configuration_price_min( $wpcb_configuration_id );
							if ( 0 == $min_price ) {
								$min_price = $price;
							}

							$format = str_replace( '${price}', wpcb_get_formatted_price( $price ), $format );
							$format = str_replace( '${min_price}', wpcb_get_formatted_price( $min_price ), $format );
							$format = str_replace( '${sale_price}', wpcb_get_formatted_price( $sale_price ), $format );
							$format = str_replace( '${max_price}', wpcb_get_formatted_price( $max_price ), $format );

							echo esc_attr( $format );
						}
						?>
					</p>

				<?php endif; ?>

			</div>

			<?php
			if ( 'popup' === get_post_meta( $wpcb_configuration_id, '_template_display', true ) ) {
				?>
				<button id="wpcb_close_configuration_popup" class="wpcb-configurator-popup-close">
					<?php echo wp_kses( wpcb_get_icon( 'close' ), wpcb_get_icon_kses_args() ); ?>
				</button>
				<?php
			}
			?>

		</div>
		<div class="wpcb-configurator-product-description">
			<?php echo wp_kses_post( wpcb__( get_post_meta( $wpcb_configuration_id, '_short_description', true ) ) ); ?>
		</div>

	<?php endif; ?>

</div>
