<?php
/**
 * Slide-over bag.
 *
 * @package Foliora
 */

$ready   = function_exists( 'WC' ) && WC()->cart;
$items   = $ready ? WC()->cart->get_cart() : array();
$remain  = foliora_free_ship_remaining();
$percent = foliora_free_ship_percent();
?>
<div class="foliora-cart-drawer" data-cart-drawer hidden>
	<div class="cart-drawer__backdrop" data-cart-close></div>
	<aside class="cart-drawer__panel" role="dialog" aria-label="Your bag" aria-modal="true">
		<header class="cart-drawer__head">
			<h2>Your bag</h2>
			<button type="button" class="cart-drawer__x" data-cart-close>Close</button>
		</header>

		<div class="cart-drawer__ship">
			<?php if ( $remain <= 0 && $ready && WC()->cart->get_cart_contents_count() ) : ?>
				<p>Free shipping is on us — your next chapter is traveling well.</p>
			<?php else : ?>
				<p>You're <?php echo esc_html( foliora_money( $remain ) ); ?> away from free shipping</p>
			<?php endif; ?>
			<div class="ship-bar" aria-hidden="true"><span style="width: <?php echo esc_attr( (string) $percent ); ?>%"></span></div>
			<p class="fine"><?php echo esc_html( function_exists( 'foliora_ritual_message' ) ? foliora_ritual_message() : 'Add 2 reading essentials → 10% off · 3 → 15% · 4+ → 20%' ); ?></p>
		</div>

		<div class="cart-drawer__items">
			<?php if ( ! $items ) : ?>
				<p class="cart-drawer__empty">Your bag is waiting for a next chapter.</p>
			<?php else : ?>
				<ul>
					<?php
					foreach ( $items as $key => $item ) :
						$product = $item['data'];
						if ( ! $product ) {
							continue;
						}
						?>
						<li class="drawer-line">
							<?php echo $product->get_image( 'thumbnail' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							<div>
								<p class="drawer-line__name"><?php echo esc_html( $product->get_name() ); ?></p>
								<?php if ( ! empty( $item['foliora_personalize'] ) ) : ?>
									<p class="fine">Personalization: <?php echo esc_html( $item['foliora_personalize'] ); ?></p>
								<?php endif; ?>
								<p class="drawer-line__meta">Qty <?php echo esc_html( (string) $item['quantity'] ); ?> · <?php echo wp_kses_post( WC()->cart->get_product_subtotal( $product, $item['quantity'] ) ); ?></p>
							</div>
						</li>
					<?php endforeach; ?>
				</ul>
			<?php endif; ?>
		</div>

		<div class="cart-drawer__addons">
			<p class="cart-drawer__kicker">A little something for your next chapter?</p>
			<?php
			if ( foliora_wc_ready() ) {
				foreach ( foliora_drawer_addon_skus() as $sku ) {
					$id = wc_get_product_id_by_sku( $sku );
					if ( ! $id ) {
						continue;
					}
					$addon = wc_get_product( $id );
					if ( ! $addon ) {
						continue;
					}
					?>
					<div class="drawer-addon">
						<span><?php echo esc_html( $addon->get_name() ); ?> · <?php echo wp_kses_post( $addon->get_price_html() ); ?></span>
						<a
							class="button button--small ajax_add_to_cart add_to_cart_button"
							href="<?php echo esc_url( $addon->add_to_cart_url() ); ?>"
							data-product_id="<?php echo esc_attr( (string) $id ); ?>"
							data-quantity="1"
						>Add</a>
					</div>
					<?php
				}
			}
			?>
		</div>

		<footer class="cart-drawer__foot">
			<?php if ( $ready ) : ?>
				<p class="cart-drawer__sub">Subtotal <?php echo wp_kses_post( WC()->cart->get_cart_subtotal() ); ?></p>
			<?php endif; ?>
			<a class="button button--ghost" href="<?php echo esc_url( function_exists( 'wc_get_cart_url' ) ? wc_get_cart_url() : home_url( '/cart/' ) ); ?>">View Bag</a>
			<a class="button" href="<?php echo esc_url( function_exists( 'wc_get_checkout_url' ) ? wc_get_checkout_url() : home_url( '/checkout/' ) ); ?>">Checkout</a>
		</footer>
	</aside>
</div>
