<?php 

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

global $product;

$suffix = wpcb_get_i18n_suffix();

if ( $has_review_page ) {
	/**
	 * Review action
	 */
	$button_text = wpcb_get_option( 'wpcb_button_review_text', $suffix ) ?: __( 'Review configuration', 'wcb-configurator-builder' );
	?>

	<button name="review-configuration" id="single_review_configuration_button" class="wpcb-button single_review_configuration_button">
		<?php echo esc_attr( $button_text ); ?>
	</button>

	<?php 
} else {
	$json_rules = [];
	if ( 'custom' === get_post_meta( $wpcb_configuration_id, '_totals_display', true ) ) {
		if ( $rules = get_post_meta( $wpcb_configuration_id, '_totals_button_rules', true ) ) {
			foreach ( $rules as $rule ) {
				$json_rules[] = [
					'choice' => wpcb_get_value( $rule['choice'] ),
					'compare' => wpcb_get_value( $rule['choice_compare'] ),
					'value' => wpcb_get_value( $rule['option'] )
				];
			}

			if ( count( $json_rules ) > 0 ) {
				$json_rules_html = str_replace( '"', "'", json_encode( $json_rules ) );
				echo '<div class="wpcb-button-wrapper" data-rules="' . $json_rules_html . '">';
			}
		}
	}

	/**
	 * Main actions
	 */
	if ( 'cart' === $button_action ) : 
		$woo_product_id = get_post_meta( $wpcb_configuration_id, '_woo_product_id', true );

		if ( ! $product && $woo_product_id && function_exists( 'wc_get_product' ) ) {
			$product = wc_get_product( $woo_product_id  );
		}

		if ( $product && ( 'popup' === get_post_meta( $wpcb_configuration_id, '_template_display', true )
			|| 'tabs' === get_post_meta( $wpcb_configuration_id, '_page_display', true )
			|| 'shortcode' === get_post_meta( $wpcb_configuration_id, '_page_display', true )
			|| 'shortcode' === get_post_meta( $wpcb_configuration_id, '_product_display', true )
			|| get_post_type() !== 'product' ) // Prevent duplicate outputs
			|| $is_review_page ) :

			if ( function_exists( 'woocommerce_template_single_add_to_cart' ) ) :
				global $wpcb_cart_action;
				$wpcb_cart_action = true;

				do_action( 'woocommerce_simple_add_to_cart' );
			endif;
		endif;

	elseif ( 'quote' === $button_action ) : 
		$button_text = wpcb_get_option( 'wpcb_button_quote_text', $suffix ) ?: __( 'Request a quote', 'wcb-configurator-builder' );
		$quote_type = carbon_get_post_meta( $wpcb_configuration_id, 'quote_type' );
		?>

		<button name="request-quote" id="single_request_quote_button" class="wpcb-button single_request_quote_button" data-quote-type="<?php echo esc_attr( implode( ',', $quote_type ) ); ?>">
			<?php echo esc_attr( $button_text ); ?>
		</button>

		<?php 
	elseif ( 'email' === $button_action ) : 
		$button_text = wpcb_get_option( 'wpcb_button_email_text', $suffix ) ?: __( 'Send an email', 'wcb-configurator-builder' );

		$mail_link = 'mailto:';
		if ( wpcb_get_option( 'wpcb_mail_recipient', $suffix ) ) {
			$mail_link .= wpcb_get_option( 'wpcb_mail_recipient', $suffix );
		} else {
			$mail_link .= get_option( 'admin_email' );
		}

		if ( wpcb_get_option( 'wpcb_mail_contact_subject', $suffix ) ) {
			$mail_link .= '?subject=' . str_replace( ' ', '%20', wpcb_get_option( 'wpcb_mail_contact_subject', $suffix ) ) . '&body=';
		} else {
			$mail_link .= '?body=';
		}
		?>
		<a href="<?php echo esc_attr( $mail_link ); ?>" name="send-email" id="single_send_email_button" class="wpcb-button single_send_email_button">
			<?php echo esc_attr( $button_text ); ?>
		</a>
		<?php 
	elseif ( 'pdf' === $button_action ) : 
		$button_text = wpcb_get_option( 'wpcb_button_pdf_text', $suffix ) ?: __( 'Download to PDF', 'wcb-configurator-builder' );;
		?>

		<button name="download-pdf" id="single_download_pdf_button" class="wpcb-button single_download_pdf_button">
			<?php echo esc_attr( $button_text ); ?>
		</button>

		<?php
	endif;

	if ( count( $json_rules ) > 0 ) {
		echo '</div>';
	}
}

?>
