<?php
/**
 * Cart Page
 *
 * IMPORTANT: The following classes/attributes are required by the theme's
 * JavaScript (assets/js/cart.js + assets/js/cart-qty-ajax.js) and WooCommerce
 * AJAX handlers. They MUST be preserved when editing this template:
 *   - .woocommerce-cart-form            (form, event delegation)
 *   - .cart-item.woocommerce-cart-form__cart-item[data-cart-item-key]
 *   - .item-price                       (AJAX line-total target)
 *   - .quantity-control .quantity input[type=number].qty
 *   - .qty-btn.qty-minus / .qty-btn.qty-plus (added by cart.js)
 *   - .remove-btn[href]                 (remove link)
 *   - .cart-summary                     (summary card / loading state)
 *   - .summary-products                 (AJAX product-list target)
 *   - .summary-row.summary-subtotal .summary-value
 *   - .summary-row.discount-line
 *   - #shippingVal                      (AJAX shipping target)
 *   - .summary-row.total .summary-value
 *   - .summary-tax                      (AJAX tax target)
 *   - .item-count-badge                 (AJAX count target)
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.
 *
 * @see     https://woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 10.8.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_cart' ); ?>

<?php if ( WC()->cart->is_empty() ) : ?>

	<div class="woocommerce-cart cart-empty-wrapper">
		<div class="empty-cart show">
			<div class="empty-icon">
				<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>
			</div>
			<h2><?php esc_html_e( 'Your Cart is Empty', 'pawwell' ); ?></h2>
			<p><?php esc_html_e( 'Looks like you haven\'t added anything to your cart yet. Explore our collections and find something you love.', 'pawwell' ); ?></p>
			<?php if ( wc_get_page_id( 'shop' ) > 0 ) : ?>
				<a class="btn-shop" href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>">
					<?php esc_html_e( 'Start Shopping', 'pawwell' ); ?>
					<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14"/><path d="m12 5 7 7-7 7"/></svg>
				</a>
			<?php endif; ?>
		</div>
	</div>

<?php else : ?>

	<?php
	$cart_count    = WC()->cart->get_cart_contents_count();
	$cart_count_lb = sprintf(
		/* translators: %d: number of items in cart */
		_n( '%d item', '%d items', $cart_count, 'pawwell' ),
		$cart_count
	);
	?>

	<!-- ============ PAGE HERO ============ -->
	<?php if ( true === get_theme_mod( 'pawwell_cart_hero_enable', true ) ) : ?>
	<section class="cart-hero">
		<div class="container">
			<div class="cart-hero-row">
				<div class="cart-hero-text">
					<?php
					$cart_eyebrow = get_theme_mod( 'pawwell_cart_hero_eyebrow', __( 'Almost Yours', 'pawwell' ) );
					$cart_title   = get_theme_mod( 'pawwell_cart_hero_title', __( 'Your', 'pawwell' ) );
					$cart_accent  = get_theme_mod( 'pawwell_cart_hero_title_accent', __( 'Shopping Cart', 'pawwell' ) );
					$cart_sub     = get_theme_mod( 'pawwell_cart_hero_subtitle', __( 'Review your selected essentials, adjust quantities and apply any promo codes before a quick, secure checkout.', 'pawwell' ) );
					?>
					<?php if ( ! empty( $cart_eyebrow ) ) : ?>
						<div class="cart-eyebrow"><?php echo esc_html( $cart_eyebrow ); ?></div>
					<?php endif; ?>
					<h1 class="cart-hero-title"><?php echo esc_html( $cart_title ); ?> <span class="cart-accent"><?php echo esc_html( $cart_accent ); ?></span></h1>
					<?php if ( ! empty( $cart_sub ) ) : ?>
						<p class="cart-hero-sub"><?php echo esc_html( $cart_sub ); ?></p>
					<?php endif; ?>
				</div>
				<?php if ( true === get_theme_mod( 'pawwell_cart_hero_stat_enable', true ) ) : ?>
					<div class="cart-hero-stat">
						<div class="chs-icon">
							<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M6 2 3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4Z"/><path d="M3 6h18"/><path d="M16 10a4 4 0 0 1-8 0"/></svg>
						</div>
						<div class="chs-text">
							<strong class="item-count-badge"><?php echo esc_html( $cart_count_lb ); ?></strong>
							<span><?php echo esc_html( get_theme_mod( 'pawwell_cart_hero_stat_text', __( 'ready to checkout', 'pawwell' ) ) ); ?></span>
						</div>
					</div>
				<?php endif; ?>
			</div>
		</div>
	</section>
	<?php endif; ?>

	<!-- ============ CART LAYOUT ============ -->
	<section class="cart-section">
		<div class="container">
			<div class="cart-layout">

				<!-- ===== CART MAIN ===== -->
				<div class="cart-main">

					<!-- Desktop column labels -->
					<div class="cart-head">
						<div><?php esc_html_e( 'Product', 'pawwell' ); ?></div>
						<div class="ch-qty"><?php esc_html_e( 'Quantity', 'pawwell' ); ?></div>
						<div class="ch-total"><?php esc_html_e( 'Total', 'pawwell' ); ?></div>
					</div>

					<form class="woocommerce-cart-form cart-items-card" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
						<?php do_action( 'woocommerce_before_cart_table' ); ?>
						<?php do_action( 'woocommerce_before_cart_contents' ); ?>

						<?php
						foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
							$_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
							$product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

							/**
							 * Filter the product name.
							 *
							 * @since 2.1.0
							 * @param string $product_name Name of the product in the cart.
							 * @param array  $cart_item    The product in the cart.
							 * @param string $cart_item_key Key for the product in the cart.
							 */
							$product_name = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key );

							if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
								$product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );

								// First product category label.
								$product_terms = get_the_terms( $product_id, 'product_cat' );
								$ci_category   = ( $product_terms && ! is_wp_error( $product_terms ) ) ? $product_terms[0]->name : '';

								// Unit + regular price for sale display.
								$ci_regular_price = $_product->get_regular_price();
								$ci_is_on_sale    = $_product->is_on_sale() && '' !== $ci_regular_price && (float) $_product->get_price() < (float) $ci_regular_price;
								?>
								<div class="cart-item woocommerce-cart-form__cart-item <?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>" data-cart-item-key="<?php echo esc_attr( $cart_item_key ); ?>">

									<div class="ci-product">
										<div class="ci-img item-image">
											<?php
											$thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

											if ( ! $product_permalink ) {
												echo wp_kses_post( $thumbnail );
											} else {
												printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), wp_kses_post( $thumbnail ) );
											}
											?>
										</div>

										<div class="ci-info">
											<?php if ( $ci_category ) : ?>
												<div class="ci-cat"><?php echo esc_html( $ci_category ); ?></div>
											<?php endif; ?>

											<div class="ci-name item-name">
												<?php
												if ( ! $product_permalink ) {
													echo wp_kses_post( $product_name . '&nbsp;' );
												} else {
													echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
												}
												?>
											</div>

											<?php do_action( 'woocommerce_after_cart_item_name', $cart_item, $cart_item_key ); ?>

											<?php
											$item_data = wc_get_formatted_cart_item_data( $cart_item );
											if ( $item_data ) :
											?>
											<div class="ci-meta item-variant"><?php echo wp_kses_post( $item_data ); ?></div>
											<?php endif; ?>

											<?php
											if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
												echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'pawwell' ) . '</p>', $product_id ) );
											}
											?>

											<?php
											echo wp_kses_post( apply_filters(
												'woocommerce_cart_item_remove_link',
												sprintf(
													'<a role="button" href="%s" class="ci-remove remove-btn" aria-label="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7h16M9 7V5a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2M6 7l1 13a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-13M10 11v6M14 11v6"/></svg> %s</a>',
													esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
													esc_attr( sprintf( __( 'Remove %s from cart', 'pawwell' ), wp_strip_all_tags( $product_name ) ) ),
													esc_attr( $product_id ),
													esc_attr( $_product->get_sku() ),
													esc_attr( $cart_item_key ),
													esc_html__( 'Remove', 'pawwell' )
												),
												$cart_item_key
											) );
											?>
										</div>
									</div>

									<div class="ci-qty-cell">
										<div class="quantity-control">
											<?php
											if ( $_product->is_sold_individually() ) {
												$min_quantity = 1;
												$max_quantity = 1;
											} else {
												$min_quantity = 0;
												$max_quantity = $_product->get_max_purchase_quantity();
											}

											$product_quantity = woocommerce_quantity_input(
												array(
													'input_name'   => "cart[{$cart_item_key}][qty]",
													'input_value'  => $cart_item['quantity'],
													'max_value'    => $max_quantity,
													'min_value'    => $min_quantity,
													'product_name' => $product_name,
												),
												$_product,
												false
											);

											echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
											?>
										</div>
										<span class="ci-qty-label"><?php esc_html_e( 'Qty', 'pawwell' ); ?></span>
									</div>

									<div class="ci-price-cell">
										<div class="ci-price item-price">
											<?php echo wp_kses_post( WC()->cart->get_product_price( $_product ) ); ?>
										</div>
										<?php if ( $ci_is_on_sale ) : ?>
											<span class="ci-price-old"><?php echo wp_kses_post( wc_price( $ci_regular_price ) ); ?></span>
										<?php endif; ?>
									</div>

									<input type="hidden" name="cart_item_key" value="<?php echo esc_attr( $cart_item_key ); ?>">
								</div>
								<?php
							}
						}
						?>

						<?php do_action( 'woocommerce_cart_contents' ); ?>

						<button type="submit" class="button update-cart-btn" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'pawwell' ); ?>" style="display:none;"><?php esc_html_e( 'Update cart', 'pawwell' ); ?></button>

						<?php do_action( 'woocommerce_cart_actions' ); ?>
						<?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
						<?php do_action( 'woocommerce_after_cart_contents' ); ?>
					</form>

					<?php do_action( 'woocommerce_after_cart_table' ); ?>

					<!-- Cart actions row: coupon + continue shopping -->
					<div class="cart-actions">
						<?php if ( wc_coupons_enabled() ) : ?>
						<form class="coupon-form promo-box" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
							<span class="coupon-icon" aria-hidden="true">
								<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9-9-9z"/><circle cx="7.5" cy="7.5" r="1.4"/></svg>
							</span>
							<label for="coupon_code" class="screen-reader-text"><?php esc_html_e( 'Promo code:', 'pawwell' ); ?></label>
							<input type="text" name="coupon_code" class="promo-input coupon-input" id="coupon_code" placeholder="<?php esc_attr_e( 'Promo code', 'pawwell' ); ?>">
							<button type="submit" class="coupon-btn promo-btn" name="apply_coupon" value="<?php esc_attr_e( 'Apply', 'pawwell' ); ?>"><?php esc_html_e( 'Apply', 'pawwell' ); ?></button>
						</form>
						<?php do_action( 'woocommerce_cart_coupon' ); ?>
						<?php endif; ?>

						<a href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>" class="continue-link">
							<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M5 12h14m-6-6 6 6-6 6"/></svg>
							<?php esc_html_e( 'Continue Shopping', 'pawwell' ); ?>
						</a>
					</div>
				</div>

				<!-- ===== ORDER SUMMARY ===== -->
				<aside class="cart-aside">
					<div class="cart-summary summary-card">
						<div class="summary-head">
							<div class="sh-icon" aria-hidden="true">
								<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="5" y="5" width="14" height="16" rx="2"/><path d="M9 3h6v4H9zM9 12h6M9 16h4"/></svg>
							</div>
							<div>
								<h2 class="summary-title"><?php esc_html_e( 'Order Summary', 'pawwell' ); ?></h2>
								<div class="sh-sub item-count-badge"><?php echo esc_html( $cart_count_lb ); ?></div>
							</div>
						</div>

						<?php do_action( 'woocommerce_before_cart_totals' ); ?>

						<!-- Products list (kept for AJAX updates; visually compact) -->
						<div class="summary-products">
							<?php
							foreach ( WC()->cart->get_cart() as $item_key => $item ) {
								$item_product = apply_filters( 'woocommerce_cart_item_product', $item['data'], $item, $item_key );
								$item_product_id = apply_filters( 'woocommerce_cart_item_product_id', $item['product_id'], $item, $item_key );

								if ( $item_product && $item_product->exists() && $item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $item, $item_key ) ) {
									$item_permalink = apply_filters( 'woocommerce_cart_item_permalink', $item_product->is_visible() ? $item_product->get_permalink( $item ) : '', $item, $item_key );
									$item_thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $item_product->get_image( 'woocommerce_thumbnail' ), $item, $item_key );
									$item_name      = apply_filters( 'woocommerce_cart_item_name', $item_product->get_name(), $item, $item_key );
									?>
									<div class="summary-product-item" data-cart-item-key="<?php echo esc_attr( $item_key ); ?>">
										<div class="summary-product-thumb">
											<?php
											if ( ! $item_permalink ) {
												echo wp_kses_post( $item_thumbnail );
											} else {
												printf( '<a href="%s">%s</a>', esc_url( $item_permalink ), wp_kses_post( $item_thumbnail ) );
											}
											?>
										</div>
										<div class="summary-product-info">
											<span class="summary-product-name">
												<?php
												if ( ! $item_permalink ) {
													echo wp_kses_post( $item_name );
												} else {
													printf( '<a href="%s">%s</a>', esc_url( $item_permalink ), wp_kses_post( $item_name ) );
												}
												?>
											</span>
											<span class="summary-product-qty"><?php echo esc_html( $item['quantity'] ); ?> &times; <?php echo wp_kses_post( WC()->cart->get_product_price( $item_product ) ); ?></span>
										</div>
										<div class="summary-product-total">
											<?php
											echo wp_kses_post( apply_filters(
												'woocommerce_cart_item_subtotal',
												WC()->cart->get_product_subtotal( $item_product, $item['quantity'] ),
												$item,
												$item_key
											) );
											?>
										</div>
									</div>
									<?php
								}
							}
							?>
						</div>

						<div class="summary-row summary-subtotal">
							<span><?php esc_html_e( 'Subtotal', 'pawwell' ); ?></span>
							<span class="summary-value"><?php wc_cart_totals_subtotal_html(); ?></span>
						</div>

						<?php foreach ( WC()->cart->get_coupons() as $code => $coupon ) : ?>
						<div class="summary-row discount-line">
							<span><?php wc_cart_totals_coupon_label( $coupon ); ?></span>
							<span class="summary-value discount"><?php wc_cart_totals_coupon_html( $coupon ); ?></span>
						</div>
						<?php endforeach; ?>

						<?php foreach ( WC()->cart->get_fees() as $fee ) : ?>
						<div class="summary-row">
							<span><?php echo esc_html( $fee->name ); ?></span>
							<span class="summary-value"><?php wc_cart_totals_fee_html( $fee ); ?></span>
						</div>
						<?php endforeach; ?>

						<?php if ( WC()->cart->needs_shipping() && WC()->cart->show_shipping() ) : ?>
						<div class="summary-row">
							<span><?php esc_html_e( 'Shipping', 'pawwell' ); ?></span>
							<span class="summary-value" id="shippingVal"><?php echo WC()->cart->get_cart_shipping_total(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
						</div>
						<?php elseif ( WC()->cart->needs_shipping() ) : ?>
						<div class="summary-row">
							<span><?php esc_html_e( 'Shipping', 'pawwell' ); ?></span>
							<span class="summary-value"><?php esc_html_e( 'Calculated at checkout', 'pawwell' ); ?></span>
						</div>
						<?php endif; ?>

						<?php if ( wc_tax_enabled() && ! WC()->cart->display_prices_including_tax() ) : ?>
						<div class="summary-row">
							<span><?php echo esc_html( WC()->countries->tax_or_vat() ); ?></span>
							<span class="summary-value"><?php wc_cart_totals_taxes_total_html(); ?></span>
						</div>
						<?php endif; ?>

						<div class="summary-divider"></div>

						<div class="summary-row total">
							<span class="st-label"><?php esc_html_e( 'Total', 'pawwell' ); ?>
								<?php if ( wc_tax_enabled() ) : ?>
									<small><?php esc_html_e( 'Inclusive of all taxes', 'pawwell' ); ?></small>
								<?php endif; ?>
							</span>
							<span class="summary-value st-value"><?php wc_cart_totals_order_total_html(); ?></span>
						</div>

						<?php if ( wc_tax_enabled() ) : ?>
						<p class="summary-tax"><?php echo wp_kses_post( WC()->cart->get_taxes_total() ); ?></p>
						<?php endif; ?>

						<div class="wc-proceed-to-checkout">
							<a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="btn-checkout">
								<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="4" y="10" width="16" height="11" rx="2"/><path d="M8 10V7a4 4 0 0 1 8 0v3"/><path d="M12 15v2"/></svg>
								<?php esc_html_e( 'Secure Checkout', 'pawwell' ); ?>
							</a>
						</div>

						<div class="summary-trust">
							<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3 5 6v6c0 4 3 7 7 9 4-2 7-5 7-9V6l-7-3z"/><path d="m9 12 2 2 4-4"/></svg> <?php esc_html_e( 'Secure Payment', 'pawwell' ); ?></div>
							<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M4 9a8 8 0 0 1 14-3l2 2"/><path d="M20 4v4h-4"/><path d="M20 15a8 8 0 0 1-14 3l-2-2"/><path d="M4 20v-4h4"/></svg> <?php esc_html_e( 'Easy Returns', 'pawwell' ); ?></div>
							<div><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 7h11v9H3zM14 10h4l3 3v3h-7"/><circle cx="7" cy="18" r="1.8"/><circle cx="17.5" cy="18" r="1.8"/></svg> <?php esc_html_e( 'Fast Delivery', 'pawwell' ); ?></div>
						</div>

						<div class="pay-row" aria-hidden="true">
							<span class="pay-chip">VISA</span>
							<span class="pay-chip">MC</span>
							<span class="pay-chip">PayPal</span>
						</div>

						<?php do_action( 'woocommerce_after_cart_totals' ); ?>
					</div>
				</aside>
			</div>
		</div>
	</section>

	<?php do_action( 'woocommerce_before_cart_collaterals' ); ?>

	<div class="cart-collaterals">
		<?php do_action( 'woocommerce_cart_collaterals' ); ?>
	</div>

	<?php do_action( 'woocommerce_after_cart' ); ?>

<?php endif;
