<?php
/**
 * Cart Page
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs, the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @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', 'shopbar' ); ?></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.', 'shopbar' ); ?></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', 'shopbar' ); ?>
					<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 = (int) WC()->cart->get_cart_contents_count(); ?>

	<div class="cart-page-head">
		<h1 class="cart-page-title">
			<?php esc_html_e( 'Shopping Cart', 'shopbar' ); ?>
			<span class="cart-count">(<?php echo esc_html( $cart_count ); ?>)</span>
		</h1>
		<p class="cart-subtitle"><?php esc_html_e( 'Review your items and proceed to checkout.', 'shopbar' ); ?></p>
	</div>

	<div class="cart-layout">
		<form class="woocommerce-cart-form cart-items" 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' ); ?>

			<table class="cart-table">
				<thead>
					<tr>
						<th class="col-product"><?php esc_html_e( 'Product', 'shopbar' ); ?></th>
						<th class="col-price center"><?php esc_html_e( 'Price', 'shopbar' ); ?></th>
						<th class="col-qty center"><?php esc_html_e( 'Quantity', 'shopbar' ); ?></th>
						<th class="col-subtotal center"><?php esc_html_e( 'Subtotal', 'shopbar' ); ?></th>
						<th class="col-action center"><?php esc_html_e( 'Action', 'shopbar' ); ?></th>
					</tr>
				</thead>
				<tbody>
				<?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 );
						?>
						<tr 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 ); ?>">

							<td class="product-cell" data-label="<?php esc_attr_e( 'Product', 'shopbar' ); ?>">
								<div class="product-cell-inner">
									<label class="product-checkbox">
										<input type="checkbox" class="cart-item-check" checked aria-label="<?php esc_attr_e( 'Select this item', 'shopbar' ); ?>">
									</label>
									<div class="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="item-details">
										<div class="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="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'] ) ) : ?>
											<div class="backorder_notification"><?php echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', esc_html__( 'Available on backorder', 'shopbar' ), $product_id ) ); ?></div>
										<?php elseif ( $_product->is_in_stock() ) : ?>
											<div class="in-stock">
												<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg>
												<?php esc_html_e( 'In Stock', 'shopbar' ); ?>
											</div>
										<?php else : ?>
											<div class="out-of-stock"><?php esc_html_e( 'Out of Stock', 'shopbar' ); ?></div>
										<?php endif; ?>
									</div>
								</div>
							</td>

							<td class="price-cell item-price" data-label="<?php esc_attr_e( 'Price', 'shopbar' ); ?>">
								<?php echo wp_kses_post( WC()->cart->get_product_price( $_product ) ); ?>
							</td>

							<td class="qty-cell" data-label="<?php esc_attr_e( 'Quantity', 'shopbar' ); ?>">
								<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 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
									?>
								</div>
							</td>

							<td class="subtotal-cell" data-label="<?php esc_attr_e( 'Subtotal', 'shopbar' ); ?>">
								<?php
								echo wp_kses_post( apply_filters(
									'woocommerce_cart_item_subtotal',
									WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ),
									$cart_item,
									$cart_item_key
								) );
								?>
							</td>

							<td class="action-cell" data-label="<?php esc_attr_e( 'Action', 'shopbar' ); ?>">
								<?php
								echo wp_kses_post( apply_filters(
									'woocommerce_cart_item_remove_link',
									sprintf(
										'<a role="button" href="%s" class="remove-btn action-btn" aria-label="%s" data-product_id="%s" data-product_sku="%s" data-cart_item_key="%s">%s</a>',
										esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
										esc_attr( sprintf( __( 'Remove %s from cart', 'shopbar' ), wp_strip_all_tags( $product_name ) ) ),
										esc_attr( $product_id ),
										esc_attr( $_product->get_sku() ),
										esc_attr( $cart_item_key ),
										'<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/></svg>'
									),
									$cart_item_key
								) );
								?>
							</td>
						</tr>
						<?php
					}
				}
				?>
				</tbody>
			</table>

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

			<div class="cart-actions">
				<div class="cart-actions-left">
					<label class="select-all">
						<input type="checkbox" id="cartSelectAll" checked>
						<?php esc_html_e( 'Select All', 'shopbar' ); ?>
					</label>
					<button type="button" class="link-btn cart-remove-selected"><?php esc_html_e( 'Remove Selected', 'shopbar' ); ?></button>
				</div>
				<a href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>" class="continue-shopping-btn">
					<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5"/><path d="m12 19-7-7 7-7"/></svg>
					<?php esc_html_e( 'Continue Shopping', 'shopbar' ); ?>
				</a>
			</div>

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

		<aside class="cart-summary">
			<h2 class="summary-title"><?php esc_html_e( 'Order Summary', 'shopbar' ); ?></h2>

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

			<div class="summary-row summary-subtotal">
				<span><?php echo esc_html( sprintf( _n( 'Subtotal (%s item)', 'Subtotal (%s items)', $cart_count, 'shopbar' ), $cart_count ) ); ?></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', 'shopbar' ); ?></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', 'shopbar' ); ?></span>
				<span class="summary-value"><?php esc_html_e( 'Calculated at checkout', 'shopbar' ); ?></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 summary-tax"><?php wc_cart_totals_taxes_total_html(); ?></span>
			</div>
			<?php endif; ?>

			<div class="summary-row total">
				<span><?php esc_html_e( 'Total', 'shopbar' ); ?></span>
				<span class="summary-value"><?php wc_cart_totals_order_total_html(); ?></span>
			</div>

			<?php if ( WC()->cart->get_cart_contents_count() > 0 ) : ?>
			<div class="free-shipping-note">
				<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M5 18H3c-.6 0-1-.4-1-1V7c0-.6.4-1 1-1h10c.6 0 1 .4 1 1v11"/><path d="M14 9h4l3 3v5c0 .6-.4 1-1 1h-2"/><circle cx="7.5" cy="18.5" r="2.5"/><circle cx="16.5" cy="18.5" r="2.5"/></svg>
				<span><?php esc_html_e( 'Free shipping on your order', 'shopbar' ); ?></span>
			</div>
			<?php endif; ?>

			<div class="wc-proceed-to-checkout">
				<a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="btn-checkout">
					<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
					<?php esc_html_e( 'Proceed to Checkout', 'shopbar' ); ?>
				</a>
			</div>

			<?php if ( wc_coupons_enabled() ) : ?>
			<div class="promo-input-group">
				<span class="promo-label">
					<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M20.59 13.41 13.42 20.58a2 2 0 0 1-2.83 0L2 12V2h10l8.59 8.59a2 2 0 0 1 0 2.82Z"/><line x1="7" y1="7" x2="7.01" y2="7"/></svg>
					<?php esc_html_e( 'Have a coupon code?', 'shopbar' ); ?>
				</span>
				<form class="promo-box" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
					<label for="coupon_code" class="screen-reader-text"><?php esc_html_e( 'Coupon:', 'shopbar' ); ?></label>
					<input type="text" name="coupon_code" class="promo-input" id="coupon_code" placeholder="<?php esc_attr_e( 'Enter coupon code', 'shopbar' ); ?>">
					<button type="submit" class="promo-btn" name="apply_coupon" value="<?php esc_attr_e( 'Apply', 'shopbar' ); ?>"><?php esc_html_e( 'Apply', 'shopbar' ); ?></button>
				</form>
				<?php do_action( 'woocommerce_cart_coupon' ); ?>
			</div>
			<?php endif; ?>

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

	<?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; ?>
