<?php
use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Platform_Settings;
use Prodigy\Includes\Helpers\Prodigy_Template;
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; ?>

	<?php if ( $is_elementor_header ) : ?>
		<div class="prodigy-cart-dropdown__wrap prodigy-custom-template">
	<?php else : ?>
		<li class="prodigy-cart-dropdown__wrap prodigy-custom-template">
	<?php endif; ?>
		<div class="prodigy-cart-dropdown">
			<?php if ( ! empty( $is_dropdown ) ) : ?>
				<a href="<?php echo esc_url( Prodigy_Url_Helper::prodigy_get_cart_url() ); ?>"
					class="prodigy-navbar-cart header-cart-block-js prodigy-cart__toggle prodigy-cart-dropdown__toggle"
					role="button"
					aria-haspopup="dialog"
					aria-expanded="false"
					aria-controls="cartDropdownPanel"
					aria-label="<?php esc_attr_e( 'Open cart', 'prodigy-commerce' ); ?>"
					id="cartToggleDropdown"
				>
					<span class="prodigy-navbar-cart__txt prodigy-navbar-cart__text"><?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_url( $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['cart_icon_type'] ) ) : ?>
						<i class="<?php echo esc_attr( $attr_shortcode['cart_icon_class'] ?? 'icon icon-cart' ); ?> 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' ) : ?>
						<i class="<?php echo esc_attr( $attr_shortcode['cart_icon_class'] ?? '' ); ?> prodigy-navbar-cart__icon"  aria-hidden="true" role="presentation"></i>
					<?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"
								data-hide-empty="<?php echo esc_attr( $attr_shortcode['cart_content_icon_hide_empty'] ); ?>">0</span>
					<?php else : ?>
						<span class="prodigy-navbar-cart__count prodigy-cart__count-items-indicator cart-count-js"
							id="cart-item-indicator"
							aria-hidden="true"
							style="display: none">0</span>
					<?php endif; ?>
				</a>
			<?php endif; ?>

			<div class="<?php echo esc_attr( $attr_shortcode['container_class'] ?? '' ); ?>"
				id="cartDropdownPanel"
				role="dialog"
				aria-modal="true"
				aria-labelledby="cartToggleDropdown"
			>
				<div class="prodigy-cart-dropdown__main prodigy-cart-dropdown__menu dropdown-menu dropdown-menu-right prodigy-cart-widget prodigy-<?php echo $is_dropdown ? 'dropdown-' : ''; ?>cart-widget-js"
					style="display: <?php echo ! empty( $is_dropdown ) ? 'none' : 'block'; ?>">
					<div class="prodigy-cart__body">
						<?php if ( $is_dropdown ) : ?>
							<div class="prodigy-cart__head">
								<div
										class="d-flex align-items-center justify-content-between prodigy-cart__heading-title prodigy-cart-dropdown__title-wrap pl-16 pr-16">
									<div class="flex-1 pr-16">
										<div class="prodigy-cart-dropdown__title">
											<?php esc_html_e( 'Shopping Cart', 'prodigy-commerce' ); ?></div>
									</div>
									<button class="prodigy-cart-dropdown__header-close icon icon-close close-cart-widget-js"
											type="button"
											aria-label="<?php esc_attr_e( 'Close cart', 'prodigy-commerce' ); ?>"></button>
								</div>
							</div>
						<?php endif; ?>

						<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"
						>
							<i class="icon icon-check mr-8 font-20 in-green-100" aria-hidden="true" role="presentation"></i>
							<span
									class="prodigy-cart-dropdown__alert-txt"><?php echo esc_html( $add_to_cart_text ); ?></span>
						</div>

						<div class="prodigy-cart-dropdown__alert widget-cart-remove-item-message-js"
							style="display: none"
							role="status"
							aria-live="polite"
						>
							<i class="icon icon-check mr-8 font-20 in-green-100" 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 class="prodigy-cart-dropdown__alert prodigy-cart-dropdown__alert-error widget-cart-message-error-js mb-20 prodigy-cart-dropdown__alert--default mt-16"
							style="display: <?php echo $is_deleted_product ? 'flex' : 'none'; ?>"
							role="status"
							aria-live="polite"
						>
							<i class="icon icon-error mr-8" aria-hidden="true" role="presentation"></i>
							<span class="prodigy-cart-dropdown__alert-txt"></span>
						</div>
						<?php delete_option( 'prodigy_widget_cart_removed_products' ); ?>

						<?php
						Prodigy_Template::prodigy_get_template(
							'cart-items.php',
							array(
								'cart_products' => $cart_products,
								'is_dropdown'   => $is_dropdown,
								'total_price'   => $cart_total ?? 0,
							)
						);
						?>
						<?php if ( Prodigy_Platform_Settings::is_redemption_store_status() ) : ?>
							<p 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-widget__subtotal-txt prodigy-widget__subtotal-text"><?php esc_html_e( 'Subtotal:', 'prodigy-commerce' ); ?></span>
								<span
										class="widget-subtotal-price-js prodigy-widget__subtotal-txt-price prodigy-cart-total__value"
										role="status" aria-live="polite"><?php echo esc_html( get_option( 'pg_currency_type' ) . Prodigy_Formatting::prodigy_price_format( $cart_total ?? 0 ) ); ?></span>
							</p>
						<?php endif; ?>
					</div>

					<div class="prodigy-cart-dropdown__footer widget-cart-checkout-block-js"
						style="display: <?php echo empty( $cart_products ) ? 'none' : 'flex'; ?>  ">
						<a class="prodigy-main-button prodigy-main-button--outline justify-content-center mb-16 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 justify-content-center checkout-url-js prodigy-cart__checkout-btn"
							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 class="prodigy-cart-dropdown__alert prodigy-cart-dropdown__alert-empty widget-cart-empty-cart-message-js"
						style="display: <?php echo empty( $cart_products ) ? 'block' : 'none'; ?>">
						<div class="d-flex align-items-center">
							<i class="icon icon-info mr-8" aria-hidden="true" role="presentation"></i>
							<span
									class="prodigy-cart-dropdown__alert-txt"><?php esc_html_e( 'No products added yet', 'prodigy-commerce' ); ?></span>
						</div>
					</div>
				</div>
			</div>
		</div>
	<?php if ( $is_elementor_header ) : ?>
		</div>
	<?php else : ?>
		</li>
	<?php endif; ?>
