<?php
defined( 'ABSPATH' ) || exit;
use Quicker\Core\Frontend;

$bump_wrapper = ( class_exists( 'QuickerPro' ) && $cart_count > 0 && ! empty( $up_sell_products ) )
	? 'bump-template-single-' . $cart_order_bump_style : '';

?>
<div class="side-cart-wrapper cart-template-<?php echo esc_attr( $side_cart_style ) . ' ' . esc_attr( $bump_wrapper ); ?>">
	<div class="side-cart-content cart-content-<?php echo esc_attr( $side_cart_style ) . ' ' . esc_attr( $bump_wrapper ); ?>">
		<?php
		if (
			$cart_count > 0 &&
			class_exists( 'QuickerPro' ) &&
			'yes' === $enable_cart_upsell &&
			1 == $cart_order_bump_style
		) {
			include \QuickerPro::template_dir() . 'side-cart/order-bump/template-' . $cart_order_bump_style . '.php';
		}
		?>
		<div class="cart-details">
			<?php
			if ( intval( $cart_count ) !== 0 ) :
				?>
				<h3 class="side-cart-title"><?php esc_html_e( 'Review Your Cart', 'quicker' ); ?></h3>
				<?php
				$_qk_settings = \Quicker\Utils\Helper::get_settings();
				if ( ( $_qk_settings['mix_max_order_qty'] ?? '' ) === 'yes' ) {
					$_qk_min      = isset( $_qk_settings['min_order_quantity'] ) && $_qk_settings['min_order_quantity'] !== '' ? (int) $_qk_settings['min_order_quantity'] : 0;
					$_qk_max      = isset( $_qk_settings['max_order_quantity'] ) && $_qk_settings['max_order_quantity'] !== '' ? (int) $_qk_settings['max_order_quantity'] : 0;
					$_qk_qty      = \Quicker\Core\Frontend\Frontend::get_scoped_cart_qty( $_qk_settings );
					$_qk_msg_tpl  = isset( $_qk_settings['mix_max_order_qty_msg'] ) ? trim( $_qk_settings['mix_max_order_qty_msg'] ) : '';
					$_qk_notice   = '';
					if ( $_qk_min > 0 && $_qk_qty > 0 && $_qk_qty < $_qk_min ) {
						$_qk_notice = $_qk_msg_tpl !== ''
							? str_replace( array( '{min}', '{max}', '{qty}' ), array( $_qk_min, $_qk_max ?: '∞', $_qk_qty ), $_qk_msg_tpl )
							/* translators: 1: minimum quantity, 2: current quantity */
							: sprintf( __( 'Your order must have at least %1$s items. Current: %2$s.', 'quicker' ), $_qk_min, $_qk_qty );
					} elseif ( $_qk_max > 0 && $_qk_qty > $_qk_max ) {
						$_qk_notice = $_qk_msg_tpl !== ''
							? str_replace( array( '{min}', '{max}', '{qty}' ), array( $_qk_min ?: '0', $_qk_max, $_qk_qty ), $_qk_msg_tpl )
							/* translators: 1: maximum quantity, 2: current quantity */
							: sprintf( __( 'Your order cannot exceed %1$s items. Current: %2$s.', 'quicker' ), $_qk_max, $_qk_qty );
					}
					if ( $_qk_notice !== '' ) {
						?>
						<div class="quicker-qty-warning wc-block-store-notice wc-block-components-notice-banner is-error is-dismissible" role="alert">
							<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
								<path d="M12 3.2c-4.8 0-8.8 3.9-8.8 8.8 0 4.8 3.9 8.8 8.8 8.8 4.8 0 8.8-3.9 8.8-8.8 0-4.8-4-8.8-8.8-8.8zm0 16c-4 0-7.2-3.3-7.2-7.2C4.8 8 8 4.8 12 4.8s7.2 3.3 7.2 7.2c0 4-3.2 7.2-7.2 7.2zM11 17h2v-6h-2v6zm0-8h2V7h-2v2z"></path>
							</svg>
							<div class="wc-block-components-notice-banner__content">
								<?php echo esc_html( $_qk_notice ); ?>
							</div>
							<button type="button" class="wc-block-components-notice-banner__dismiss" aria-label="<?php esc_attr_e( 'Dismiss', 'quicker' ); ?>" onclick="this.closest('.quicker-qty-warning').style.display='none'">
								<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
									<path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.7 6.5 6.7 1-1z"></path>
								</svg>
							</button>
						</div>
						<?php
					}
				}

				// Per-product qty rules warnings (QuickerPro).
				if ( class_exists( 'QuickerPro' ) && ! WC()->cart->is_empty() ) {
					foreach ( WC()->cart->get_cart() as $_qk_item ) {
						$_qk_product = $_qk_item['data'] ?? null;
						if ( ! $_qk_product ) {
							continue;
						}
						$_qk_rules = (array) $_qk_product->get_meta( '_quicker_qty_rules' );
						if ( empty( $_qk_rules ) ) {
							continue;
						}
						$_qk_iq  = (int) $_qk_item['quantity'];
						$_qk_imin = isset( $_qk_rules['min'] ) && $_qk_rules['min'] !== '' ? (int) $_qk_rules['min'] : 0;
						$_qk_imax = isset( $_qk_rules['max'] ) && $_qk_rules['max'] !== '' ? (int) $_qk_rules['max'] : 0;
						$_qk_iname = $_qk_product->get_name();
						$_qk_inotice = '';
						if ( $_qk_imin > 0 && $_qk_iq < $_qk_imin ) {
							/* translators: 1: product name, 2: minimum quantity */
							$_qk_inotice = sprintf( __( '"%1$s" requires a minimum of %2$d.', 'quicker' ), $_qk_iname, $_qk_imin );
						} elseif ( $_qk_imax > 0 && $_qk_iq > $_qk_imax ) {
							/* translators: 1: product name, 2: maximum quantity */
							$_qk_inotice = sprintf( __( '"%1$s" allows a maximum of %2$d.', 'quicker' ), $_qk_iname, $_qk_imax );
						}
						if ( $_qk_inotice !== '' ) {
							?>
							<div class="quicker-qty-warning wc-block-store-notice wc-block-components-notice-banner is-error is-dismissible" role="alert">
								<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
									<path d="M12 3.2c-4.8 0-8.8 3.9-8.8 8.8 0 4.8 3.9 8.8 8.8 8.8 4.8 0 8.8-3.9 8.8-8.8 0-4.8-4-8.8-8.8-8.8zm0 16c-4 0-7.2-3.3-7.2-7.2C4.8 8 8 4.8 12 4.8s7.2 3.3 7.2 7.2c0 4-3.2 7.2-7.2 7.2zM11 17h2v-6h-2v6zm0-8h2V7h-2v2z"></path>
								</svg>
								<div class="wc-block-components-notice-banner__content">
									<?php echo esc_html( $_qk_inotice ); ?>
								</div>
								<button type="button" class="wc-block-components-notice-banner__dismiss" aria-label="<?php esc_attr_e( 'Dismiss', 'quicker' ); ?>" onclick="this.closest('.quicker-qty-warning').style.display='none'">
									<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false">
										<path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.7 6.5 6.7 1-1z"></path>
									</svg>
								</button>
							</div>
							<?php
						}
					}
				}
			endif;

			do_action( 'woocommerce_before_mini_cart' );

			if ( ! empty( WC()->cart ) && ! WC()->cart->is_empty() ) :
				?>
				<div class="quicker-items <?php echo esc_attr( $bump_section ); ?>">
					<?php
					do_action( 'woocommerce_before_mini_cart_contents' );
					foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) :
						$data = Frontend\Frontend::instance()->side_cart_data( $cart_item, $cart_item_key, $bump_product_type );
						extract( $data );
						?>
						<?php $is_bogo_free = ! empty( $cart_item['bogo_x_x_free'] ) || ! empty( $cart_item['bogo_x_y_free'] ); ?>
						<?php if ( $is_bogo_free ) : ?>
						<div class="cart-row">
							<div class="cart-item cart-row-<?php echo esc_attr( $cart_item_key ); ?>">
								<?php if ( empty( $link ) ) : ?>
									<?php echo Quicker\Utils\Helper::kses( $thumbnail ); ?>
								<?php else : ?>
									<a href="<?php echo esc_url( $link ); ?>">
										<?php echo Quicker\Utils\Helper::kses( $thumbnail ); ?>
									</a>
								<?php endif; ?>
							</div>
							<div class="cart-item quicker-sidecart-quantity">
								<div class="item-details">
									<div class="product-name"><?php echo esc_html( $product->get_name() ); ?></div>
								</div>
							</div>
							<div class="cart-item subtotal-block">
								<div class="product-price">
									<div class="discountify-bogo-free-price"><?php esc_html_e( 'FREE', 'quicker' ); ?></div>
								</div>
							</div>
						</div>
						<?php else : ?>
						<div class="cart-row">
							<div class="cart-item cart-row-<?php echo esc_attr( $cart_item_key ); ?>">
								<?php
								// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WooCommerce filter returns safe HTML
								echo apply_filters(
									'woocommerce_cart_item_remove_link',
									sprintf(
										'<a href="%s" class="remove quicker_remove_item" aria-label="%s" data-product_id="%s" data-cart_item_key="%s" data-product_sku="%s">
                                            <svg width="22px" height="22px" class="cart-remove" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <circle opacity="0.5" cx="12" cy="12" r="10" stroke="#1C274C" stroke-width="1.5"/>
                                                <path d="M15 12H9" stroke="#1C274C" stroke-width="1.5" stroke-linecap="round"/>
                                            </svg>
                                        </a>',
										esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
										esc_attr__( 'Remove this item', 'quicker' ),
										esc_attr( $product_id ),
										esc_attr( $cart_item_key ),
										esc_attr( $product->get_sku() )
									),
									$cart_item_key
								);
								?>
								<?php if ( empty( $link ) ) : ?>
									<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Sanitized by kses ?>
									<?php echo Quicker\Utils\Helper::kses( $thumbnail ); ?>
								<?php else : ?>
									<a href="<?php echo esc_url( $link ); ?>">
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Sanitized by kses ?>
										<?php echo Quicker\Utils\Helper::kses( $thumbnail ); ?>
									</a>
								<?php endif; ?>
							</div>
							<div class="cart-item quicker-sidecart-quantity">
								<div class="item-details">
									<?php if ( empty( $link ) ) : ?>
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Sanitized by kses ?>
										<div class="product-name"> <?php echo Quicker\Utils\Helper::kses( $product_name ); ?></div>
									<?php else : ?>
										<a href="<?php echo esc_url( $link ); ?>">
											<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Sanitized by kses ?>
											<div class="product-name"> <?php echo Quicker\Utils\Helper::kses( $product_name ); ?></div>
										</a>
									<?php endif; ?>
									<div class="mini-qty">
										<span class="qty-handle minus">-</span>
										<input type="text" class="qty"
											value="<?php echo esc_attr( $cart_item['quantity'] ); ?>"
											data-cart_item_key="<?php echo esc_attr( $cart_item_key ); ?>"
											data-product_id="<?php echo esc_attr( $cart_item['product_id'] ); ?>"
										/>
										<span class="qty-handle plus">+</span>
									</div>
								</div>
							</div>
							<div class="cart-item subtotal-block">
								<div class="product-price">
									<div> <?php echo esc_html( (float) $product->get_price() . '*' . $cart_item['quantity'] ); ?></div>
									<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wc_price returns safe HTML ?>
									<div><?php echo wc_price( (float) $product->get_price() * $cart_item['quantity'] ); ?></div>
								</div>
							</div>
						</div>
						<?php endif; ?>
						<?php
					endforeach;
					do_action( 'woocommerce_mini_cart_contents' );
					?>
				</div>
				<!-- Footer Section -->
				<div class="subtotal-content">
					<div class="woocommerce-notices-wrapper"></div>
					<?php
					if ( class_exists( 'QuickerPro' ) && file_exists( \QuickerPro::template_dir() . 'side-cart/parts/coupon.php' ) ) {
						include \QuickerPro::template_dir() . 'side-cart/parts/coupon.php';
					}
					if ( class_exists( 'QuickerPro' ) && 'yes' === $enable_cart_upsell &&
						( 2 == $cart_order_bump_style || 3 == $cart_order_bump_style ) ) {
						include \QuickerPro::template_dir() . 'side-cart/order-bump/bump-grid.php';
					}
					?>
					<div class="cart-block">
						<!-- Subtotal -->
						<div class="sub-total woocommerce-mini-cart__total total">
							<strong><?php esc_html_e( 'Subtotal', 'quicker' ); ?></strong>
							<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WC returns safe HTML ?>
							<?php echo WC()->cart->get_cart_subtotal(); ?>
						</div>


						<?php // WooCommerce coupons (dynamic) ?>
						<?php if ( WC()->cart->get_coupons() ) : ?>
							<div class="fees-item mini-cart-coupons">
								<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
									<div class="quicker-coupon coupon-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
										<span><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WC returns safe HTML ?>
										<span><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
									</div>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>

						<?php
						// Fees (extra fees, etc.) — before shipping
						WC()->cart->calculate_fees();
						$cart_fees = WC()->cart->get_fees();

						if ( ! empty( $cart_fees ) ) :
						?>
							<div class="fees-item mini-cart-fees">
								<?php foreach ( $cart_fees as $fee ) :
									if ( 0 == $fee->amount ) { continue; }
								?>
									<p class="quicker-mini-cart-fee">
										<span class="fee-label"><?php echo esc_html( $fee->name ); ?></span>
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wc_price returns safe HTML ?>
										<span class="fee-amount"><?php echo wc_price( $fee->total ); ?></span>
									</p>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>

						<?php // Shipping ?>
						<?php if ( WC()->cart->needs_shipping() ) :
							$shipping_total = (float) WC()->cart->get_shipping_total();
							$shipping_tax   = (float) WC()->cart->get_shipping_tax();
							$shipping_display = $shipping_total + $shipping_tax;
							?>
							<div class="fees-item mini-cart-shipping">
								<div class="quicker-shipping">
									<span><?php esc_html_e( 'Shipping', 'quicker' ); ?></span>
									<?php if ( $shipping_display > 0 ) : ?>
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wc_price returns safe HTML ?>
										<span><?php echo wc_price( $shipping_display ); ?></span>
									<?php else : ?>
										<span><?php esc_html_e( 'Free shipping', 'quicker' ); ?></span>
									<?php endif; ?>
								</div>
							</div>
						<?php endif; ?>

						<?php // Taxes ?>
						<?php if ( wc_tax_enabled() && WC()->cart->get_tax_totals() ) : ?>
							<div class="fees-item mini-cart-taxes">
								<?php foreach ( WC()->cart->get_tax_totals() as $code => $tax ) : ?>
									<div class="quicker-tax tax-rate-<?php echo esc_attr( sanitize_title( $code ) ); ?>">
										<span><?php echo esc_html( $tax->label ); ?></span>
										<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WC returns safe HTML ?>
										<span><?php echo wp_kses_post( $tax->formatted_amount ); ?></span>
									</div>
								<?php endforeach; ?>
							</div>
						<?php endif; ?>

						<!-- Total -->
						<div class="side-cart-extra mt-2 mb-5">
							<div class="side-cart-extra-total">
								<div class="">
									<strong><?php esc_html_e( 'Total', 'quicker' ); ?></strong>
									<span class="extra-text"><?php esc_html_e( '(including all charges)', 'quicker' ); ?></span>
								</div>
								<?php // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WC returns safe HTML ?>
								<b class="cart-total"><?php wc_cart_totals_order_total_html(); ?></b>
							</div>
						</div>
					</div>
					<?php
					do_action( 'woocommerce_widget_shopping_cart_before_buttons' );
					?>
					<div class="shopping-button">
						<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ); ?>">
							<button class="continue-shopping"><?php esc_html_e( 'Continue Shopping', 'quicker' ); ?></button>
						</a>
						<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'checkout' ) ) ); ?>">
							<button class="quicker-checkout"><?php esc_html_e( 'Checkout', 'quicker' ); ?></button>
						</a>
					</div>
					<?php do_action( 'woocommerce_widget_shopping_cart_after_buttons' ); ?>
				</div>
				<?php
				if ( class_exists( 'QuickerPro' ) && 'yes' === $enable_cart_upsell &&
					( 4 == $cart_order_bump_style || 5 == $cart_order_bump_style ) ) {
					include \QuickerPro::template_dir() . 'side-cart/order-bump/bump-grid.php';
				}
				?>
			<?php else : ?>
				<div class="empty-cart">
					<p><?php esc_html_e( 'Empty Cart', 'quicker' ); ?></p>
					<a href="<?php echo esc_url( get_permalink( wc_get_page_id( 'shop' ) ) ); ?>">
						<button><?php echo esc_html__( 'Explore Products', 'quicker' ); ?></button>
					</a>
				</div>
				<?php
			endif;
			do_action( 'woocommerce_after_mini_cart' );
			?>
		</div>
	</div>
</div>

