<?php
/* Template Name: Cart shortcode */

use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Platform_Settings;
use Prodigy\Includes\Helpers\Prodigy_Url_Helper;
use Prodigy\Includes\Helpers\Prodigy_Widget_Options;
use Prodigy\Includes\Prodigy_User;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>

<?php $prodigy_is_external_checkout = Prodigy_User::get_external_checkout_id() !== null; ?>

<div class="prodigy-cart-slide prodigy-cart-slide-js prodigy-custom-template"
	data-auto-open="<?php echo esc_attr( $attr_shortcode['cart_content_gen_auto_open'] ); ?>"
	id="cart-drawer"
	role="dialog"
	aria-labelledby="cart-toggle-button"
	aria-modal="true"
>
	<button class="prodigy-cart__toggle prodigy-cart-slide__toggle"
		aria-label="<?php esc_attr_e( 'Open cart', 'prodigy-commerce' ); ?>"
		aria-haspopup="dialog"
		aria-expanded="false"
		id="cart-toggle-button"
	>
		<span class="prodigy-navbar-cart__txt"><?php esc_html_e( 'Cart', 'prodigy-commerce' ); ?></span>

		<?php if ( ! empty( $attr_shortcode['icon_utf'] ) ) : ?>
			<span class="divi-icon-output prodigy-navbar-cart__icon"
					data-icon-type="<?php echo esc_attr( $attr_shortcode['icon_type'] ); ?>"
					data-icon-utf="<?php echo esc_html( htmlspecialchars( $attr_shortcode['icon_utf'] ) ); ?>"
					data-icon-font-weight="<?php echo esc_attr( $attr_shortcode['icon_weight'] ); ?>"
					data-icon="<?php echo esc_attr( $attr_shortcode['icon_utf'] ); ?>"></span>
		<?php elseif ( empty( $attr_shortcode['icon_utf'] ) && empty( $attr_shortcode['cart_icon_type'] ) ) : ?>
			<i class="<?php echo esc_attr( $attr_shortcode['cart_icon_class'] ); ?> prodigy-navbar-cart__icon" aria-hidden="true" role="presentation"></i>
		<?php endif; ?>

		<?php if ( isset( $attr_shortcode['cart_icon_type'] ) && $attr_shortcode['cart_icon_type'] === 'icon' ) : ?>
			<?php if ( ! empty( $attr_shortcode['cart_icon_class'] ) ) : ?>
				<i class="<?php echo esc_attr( $attr_shortcode['cart_icon_class'] ); ?> prodigy-navbar-cart__icon" aria-hidden="true" role="presentation"></i>
			<?php endif; ?>
		<?php elseif ( isset( $attr_shortcode['cart_icon_type'] ) && $attr_shortcode['cart_icon_type'] === 'svg' ) : ?>
			<img class="<?php echo esc_attr( $attr_shortcode['cart_svg_class'] ); ?>"
				src="<?php echo esc_attr( $attr_shortcode['cart_content_icon_url'] ); ?>" alt="<?php esc_html_e( 'Cart icon', 'prodigy-commerce' ); ?>">
		<?php endif; ?>
		<?php if ( isset( $attr_shortcode['cart_content_icon_items_indicator'] ) && Prodigy_Widget_Options::$bool_mapper[ $attr_shortcode['cart_content_icon_items_indicator'] ] ) : ?>
			<span class="<?php echo esc_attr( $attr_shortcode['count_classname'] ); ?> prodigy-cart__count-items-indicator cart-count-js"
					style="display: none;"
					id="cart-items-indicator"
					aria-hidden="true"
					data-hide-empty="<?php echo esc_attr( $attr_shortcode['cart_content_icon_hide_empty'] ); ?>">0</span>
		<?php endif; ?>
	</button>

	<div class="prodigy-cart-slide__container">
		<div class="prodigy-cart-slide__main">
			<div class="prodigy-cart__head">
				<div class="d-flex align-items-center pl-16 pr-16 prodigy-cart-dropdown__title-wrap">
					<div class="flex-1 pr-16">
						<div class="prodigy-cart-dropdown__title"><?php echo esc_html( $attr_shortcode['heading_text'] ); ?></div>
					</div>
					<button
						class="prodigy-cart__close prodigy-cart-slide__close"
						type="button"
						aria-label="<?php esc_attr_e( 'Close cart', 'prodigy-commerce' ); ?>">
						<i class="icon icon-close" aria-hidden="true" role="presentation"></i>
					</button>
				</div>
			</div>
			<div class="prodigy-cart__body">
				<div class="prodigy-cart-dropdown__alert prodigy-cart-dropdown__alert-added widget-cart-add-item-message-js"
					style="display: none"
					role="status"
					aria-live="polite">
					<div class="d-flex align-items-center">
						<i class="icon icon-check font-20 in-green-100 mr-8"></i>
						<span class="prodigy-cart-dropdown__alert-txt"><?php echo esc_html( $add_to_cart_text ); ?></span>
					</div>
				</div>
				<div class="prodigy-cart-dropdown__alert widget-cart-remove-item-message-js" style="display: none"
					role="status"
					aria-live="polite">
					<div class="d-flex align-items-center">
						<i class="icon icon-check font-20 in-green-100 mr-8" aria-hidden="true" role="presentation"></i>
						<span class="prodigy-cart-dropdown__alert-txt"><?php esc_html_e( 'has been removed from your cart.', 'prodigy-commerce' ); ?></span>
					</div>
				</div>
				<div class="prodigy-cart-dropdown__alert prodigy-cart-dropdown__alert-empty widget-cart-empty-cart-message-js"
					style="display: <?php echo empty( $cart_products ) ? 'flex' : 'none'; ?>"
					role="status"
					aria-live="polite"
				>
					<i class="icon icon-info font-20 mr-8" aria-hidden="true" role="presentation"></i>
					<span class="prodigy-cart-dropdown__alert-txt"><?php echo esc_html( $attr_shortcode['empty_cart_text'] ); ?></span>
				</div>

				<?php
				Prodigy\Includes\Helpers\Prodigy_Template::prodigy_get_template(
					'cart-items.php',
					array(
						'cart_products' => $cart_products,
						'is_dropdown'   => $is_dropdown,
						'total_price'   => $cart_total ?? 0,
					)
				);
				?>

				<!-- Here the needed class to add 'prodigy-cart-loading__element' for loading element -->
				<?php if ( Prodigy_Platform_Settings::is_redemption_store_status() ) : ?>
					<div class="prodigy-cart-total prodigy-cart-total__widget-subtotal widget-cart-subtotal-js"
						style="display: <?php echo empty( $cart_products ) ? 'none' : 'flex'; ?>;">
						<span class="prodigy-cart-total__text">
							<?php esc_html_e( 'Subtotal:', 'prodigy-commerce' ); ?>
						</span>
						<span class="prodigy-cart-total__value prodigy-cart-total__value-subtotal-price widget-subtotal-price-js"
							role="status"
							aria-live="polite">
							$<?php echo esc_html( Prodigy_Formatting::prodigy_price_format( $cart_total ?? 0 ) ); ?>
						</span>
					</div>
				<?php endif; ?>
			</div>
			<div class="prodigy-cart__footer widget-cart-checkout-block-js"
				style="display: <?php echo empty( $cart_products ) ? 'none' : 'flex'; ?>;">
				<a class="prodigy-main-button prodigy-main-button--outline prodigy-cart__cart-btn"
					href="<?php echo esc_url( Prodigy_Url_Helper::prodigy_get_cart_url() ); ?>">
					<?php esc_html_e( 'View cart', 'prodigy-commerce' ); ?>
				</a>
				<button class="prodigy-main-button prodigy-cart__checkout-btn checkout-url-js"
					type="button"
					aria-label="<?php echo $prodigy_is_external_checkout ? esc_attr__( 'Return to SAP Ariba', 'prodigy-commerce' ) : esc_attr__( 'Proceed to Secure Checkout', 'prodigy-commerce' ); ?>">
					<?php echo $prodigy_is_external_checkout ? esc_html__( 'Return to SAP Ariba', 'prodigy-commerce' ) : esc_html__( 'Checkout', 'prodigy-commerce' ); ?>
				</button>
			</div>
		</div>
	</div>
</div>
