<?php

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

do_action( 'wpcb_before_configuration_form_total_price' );

$totals_visible = 'no' !== get_post_meta( $wpcb_configuration_id, '_show_price_totals', true );
if ( $totals_visible ) :
	?>
	<div class="wpcb-configurator-form-totals-details">
		<?php 
		do_action( 'wpcb_configuration_form_total_price' );

		$suffix = wpcb_get_i18n_suffix();
		$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;
		}

		$show_tax = wpcb_get_option( 'wpcb_show_totals_incl_excl_tax' );
		$class = $price_visible ? '' : ' wpcb-price-hidden';
		?>
		<div class="wpcb-configurator-form-totals-price<?php echo esc_attr( $class ); ?>">
			<?php if ( ! wpcb_get_option( 'wpcb_enable_option_summary_inline' ) ) : ?>
				<a href="#" class="wpcb-configurator-view-price-details">
					<?php 
					echo wp_kses( wpcb_get_icon( 'price-summary' ), wpcb_get_icon_kses_args() );

					$view_label = wpcb_get_option( 'wpcb_button_configuration_text', $suffix ) ?: __( 'View configuration', 'wcb-configurator-builder' );
					echo sprintf( '<span class="wpcb-configurator-form-action-label">%s</span>', esc_attr( wpcb__( $view_label ) ) );
					?>
				</a>
			<?php endif; ?>
			<?php if ( $price_visible ) : ?>
				<div class="wpcb-configurator-totals-price-summary">
					<div class="wpcb-configurator-totals-price-main">
						<?php if ( '' !== wpcb_get_option( 'wpcb_price_summary_text' ) ) : ?>
							<strong><?php echo esc_attr( wpcb_get_option( 'wpcb_price_summary_text', $suffix ) ); ?></strong>
						<?php endif; ?>
						<div class="wpcb-configurator-total-price">
							<?php 
							echo wp_kses_post( wpcb_get_price_html( get_post_meta( $wpcb_configuration_id, '_price', true ) ) );
							?>
						</div>
						<?php 
						if ( $tax_label = wpcb_get_option( 'wpcb_product_tax_label', $suffix ) ) {
							echo sprintf( '<p class="wpcb-configurator-total-tax-label">%s</p>', esc_attr( $tax_label ) );
						}
						?>
					</div>
					<?php if ( $show_tax ) : ?>
						<div class="wpcb-configurator-totals-price-tax">
							<div class="wpcb-configurator-total-tax">
								<?php 
								echo wp_kses_post( wpcb_get_price_html( get_post_meta( $wpcb_configuration_id, '_price', true ) ) );
								?>
							</div>
							<?php 
							if ( $tax_label = wpcb_get_option( 'wpcb_product_ex_tax_label', $suffix ) ) {
								echo sprintf( '<p class="wpcb-configurator-total-tax-label">%s</p>', esc_attr( $tax_label ) );
							}
							?>
						</div>
					<?php endif; ?>
				</div>
			<?php endif; ?>
		</div>
		<?php
		global $wpcb_stock_settings;
		if ( ! $wpcb_stock_settings ) {
			wpcb_define_stock_settings();
		}

		$show_stock = 'no' !== get_post_meta( $wpcb_configuration_id, '_wpcb_show_configurator_stock', true );
		if ( $wpcb_stock_settings['wpcb_manage_stock'] && $show_stock && '' !== $wpcb_stock_settings['wpcb_in_stock_label'] ) {
			?>
			<div class="wpcb-configurator-form-totals-stock">
				<p class="wpcb-configurator-total-stock in-stock">
					<?php echo esc_attr( $wpcb_stock_settings['wpcb_in_stock_label'] ); ?>
				</p>
				<p class="wpcb-configurator-total-stock on-backorder">
					<?php echo esc_attr( $wpcb_stock_settings['wpcb_on_backorder_label'] ); ?>
				</p>
				<p class="wpcb-configurator-total-stock out-of-stock">
					<?php echo esc_attr( $wpcb_stock_settings['wpcb_out_of_stock_label'] ); ?>
				</p>
			</div>
			<?php
		}

		$manage_weight = wpcb_get_option( 'wpcb_manage_weight' );
		if ( $manage_weight ) {
			$weight_display = wpcb_get_option( 'wpcb_weight_totals_display' );
			$custom_weight = get_post_meta( $wpcb_configuration_id, '_change_weight', true ) ?: 'no';
			$weight_formula = false;
			$weight_formula_rounding = false;
			if ( $custom_weight ) {
				$weight_formula_id = get_post_meta( $wpcb_configuration_id, '_configurator_weight', true );
				$weight_formula_rounding = wpcb_get_formula_rounding( $weight_formula_id );
				$weight_formula = wpcb_get_formula_equasion( $weight_formula_id );
			}

			if ( 'price' === $weight_display ) {
				$weight_label = wpcb_get_option( 'wpcb_weight_totals_label', $suffix ) ?: __( 'Weight', 'wcb-configurator-builder' );
				$weight_unit  = get_option( 'woocommerce_weight_unit' ) ?: 'kg';
				$base_weight  = 0;

				if ( function_exists( 'is_product' ) && is_product() ) {
					global $product;
					if ( ! is_object( $product ) ) {
						$product = wc_get_product( get_the_ID() );
					}
					$base_weight = $product->get_weight() ?: 0;
				}
				?>
				<div class="wpcb-configurator-form-totals-weight">
					<?php echo esc_attr( $weight_label ); ?>
					<p class="wpcb-configurator-total-weight"<?php
					 	if ( $weight_formula ) {
							echo ' data-formula="' . esc_attr( $weight_formula ) . '"';
						}
						if ( $weight_formula_rounding ) {
							echo ' data-formula-rounding="' . esc_attr( $weight_formula_rounding ) . '"';
						}
						?>>
						<?php echo esc_attr( $base_weight ) . ' ' . esc_attr( $weight_unit ); ?>
					</p>
				</div>
				<?php
			} else if ( $weight_formula ) {
				echo '<span class="wpcb-configurator-total-weight" data-formula="' . esc_attr( $weight_formula ) . '"';
				if ( $weight_formula_rounding ) {
					echo ' data-formula-rounding="' . esc_attr( $weight_formula_rounding ) . '"';
				}
				echo '></span>';
			}
		}
		?>
	</div>
	<?php
endif;

do_action( 'wpcb_after_configuration_form_total_price' );
