<?php
/**
 * Custom mini-cart
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/hbfwc/cart/mini-cart.php.
 *
 * @package HBFWC\Templates
 * @version 1.1.17
 */

defined( 'ABSPATH' ) || exit;

$cart_sub_total = 0;
do_action( 'hbfwc_template_before_mini_cart' ); ?>

<?php if ( hbfwc_cart_has_accommodation() ) : ?>
	
	<h3><?php print esc_html( apply_filters( 'hbfwc_mini_cart_heading_text', __( 'Rooms', 'hotel-booking-for-woocommerce' ) ) ); ?></h3>

	<ul class="hbfwc-mini-cart__items">
	<?php
	do_action( 'hbfwc_template_before_mini_cart_contents' );

	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 = $cart_item['product_id'];

		// Only show accommodation products in cart.
		if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && $_product->is_type( 'accommodation' ) ) {
			$product_name = $_product->get_name();
			$rate_name    = '';
			if ( ! empty( $cart_item['rate_id'] ) ) {
				$rate      = get_term_by( 'term_id', absint( $cart_item['rate_id'] ), 'rate_plan' );
				$rate_name = $rate->name;
			}

			$qty_string = sprintf(
				/* translators: Number of room available */
				_n( '%d room', '%d rooms', absint( $cart_item['quantity'] ), 'hotel-booking-for-woocommerce' ),
				absint( $cart_item['quantity'] )
			);
			$checkin_date  = '';
			$stay_string   = '';

			$booking_item_subtotal_price = $cart_item['line_subtotal'];
			$product_price               = apply_filters( 'hbfwc_minicart_item_price', wc_price( $booking_item_subtotal_price ), $cart_item, $cart_item_key );
			
			$cart_sub_total = bcadd( $cart_sub_total, $booking_item_subtotal_price, HBFWC\Utils\Constants::MATH_SCALE );

			if ( ! empty( $cart_item['check_in'] ) && ! empty( $cart_item['check_out'] ) ) {
				$ci_date     = new \DateTime(wp_date( 'Y-m-d', $cart_item['check_in'] ) );
				$co_date     = new \DateTime(wp_date( 'Y-m-d', $cart_item['check_out'] ) );
				$stay        = absint( $co_date->diff( $ci_date )->format( '%a' ) );
				$stay_string = sprintf(
					/* translators: Number of nights */
					_n( '%d night', '%d nights', $stay, 'hotel-booking-for-woocommerce' ),
					$stay
				);

				$checkin_date =wp_date( hbfwc_get_date_format(), $cart_item['check_in'] ) . ' &ndash; ' . wp_date( hbfwc_get_date_format(), $cart_item['check_out'] );
			}
			?>
			<li class="hbfwc-mini-cart__item">
				<h4 class="hbfwc-mini-cart__item-name"><?php print esc_html( $product_name ); ?></h4>
				<span class="hbfwc-mini-cart__item-meta">
					<?php 
					if ( $rate_name ) {
						echo '<span class="cart-room__rate">' . sprintf(
							/* translators: Rate name */
							__( 'Rate: %s', 'hotel-booking-for-woocommerce' ),
							esc_html( $rate_name )
						) . '</span>';
					}
					?>
					<span class="cart-checkin__date"><i class="fas fa-calendar-check"></i> <?php print esc_html( $checkin_date ); ?></span>
					<span class="cart-checkin__guests"><i class="fas fa-user-group"></i> 
					<?php
					printf(
						/* translators: Number of adults */
						esc_html__( 'Adults: %s', 'hotel-booking-for-woocommerce' ),
						esc_html( $cart_item['adults'] )
					);

					if ( isset( $cart_item['children'] ) ) {
						printf(
							/* translators: Number of children */
							esc_html__( ', Children: %s', 'hotel-booking-for-woocommerce' ),
							esc_html( $cart_item['children'] )
						);
					}
					?>
					</span>
					<?php
						do_action( 'hbfwc_template_after_mini_cart_item_meta', $product_id, $cart_item );

						if ( isset( $cart_item['addons'] ) && ! empty( $cart_item['addons'] ) ) {
							print '<span class="cart-checkin-addons-wrap">';
							foreach( $cart_item['addons'] as $addon ) {
								$addon_id  = (int) $addon['id'];
								$raw_price = get_post_meta( $addon_id, 'price', true );
								$unit      = get_post_meta( $addon_id, 'unit', true );
								
								$addon['qty_and_unit'] = ! empty( $unit ) 
									? sprintf( '%d %s', intval( $addon['qty'] ), $unit ) 
									: $addon['qty'];
								
								$addon_qty = intval( $addon['qty'] ) > 1 ? wc_price( wc_format_decimal($raw_price, wc_get_price_decimals()) ) . ' &times; ' . $addon['qty_and_unit'] : wc_price( wc_format_decimal($raw_price, wc_get_price_decimals()) );

								$addon_html = '<span class="cart-checkin-addon"><span class="cart-checkin-addon__name">' . get_the_title( $addon_id ) . '</span><span class="cart-checkin-addon__qty">' . $addon_qty . '</span><span class="cart-checkin-addon__price">' . wc_price( (float) hbfwc_price_to_float( $raw_price ) * intval( $addon['qty'] ) ) . '</span></span>';
								$addon_html = apply_filters( 'hbfwc_addon_html_item_mini_cart', $addon_html, $cart_item, $addon );

								echo hbfwc_in_legacy_pro() 
											? '<span class="cca-legacy">' . wp_kses_post($addon_html) . '</span>'
											: wp_kses_post($addon_html);
							}
							print '</span>';
						}
					?>
				</span>
				<span class="hbfwc-mini-cart__item-price">
					<span class="cart-price__qty"><?php printf( '%1$s &times; %2$s', esc_html( $qty_string ), esc_html( $stay_string ) ); ?></span>
					<span class="cart-price__subtotal"><?php print $product_price; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				</span>
				<a href="#" class="hbfwc_remove_item" data-key="<?php print esc_attr( $cart_item_key ); ?>" title="<?php esc_attr_e( 'Remove item', 'hotel-booking-for-woocommerce' ); ?>">
					<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path fill="currentColor" fill-rule="evenodd" clip-rule="evenodd" d="M12 5.5A2.25 2.25 0 0 0 9.878 7h4.244A2.251 2.251 0 0 0 12 5.5ZM12 4a3.751 3.751 0 0 0-3.675 3H5v1.5h1.27l.818 8.997a2.75 2.75 0 0 0 2.739 2.501h4.347a2.75 2.75 0 0 0 2.738-2.5L17.73 8.5H19V7h-3.325A3.751 3.751 0 0 0 12 4Zm4.224 4.5H7.776l.806 8.861a1.25 1.25 0 0 0 1.245 1.137h4.347a1.25 1.25 0 0 0 1.245-1.137l.805-8.861Z"></path></svg>
				</a>
			</li>

			<?php
		}
	}

	do_action( 'hbfwc_template_after_mini_cart_contents' );
	?>
	</ul>

	<p class="hbfwc-mini-cart__total total">
		<?php
		echo '<strong>' . esc_html__( 'Total:', 'hotel-booking-for-woocommerce' ) . '</strong> ' . wc_price( $cart_sub_total ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		?>
	</p>

	<p class="hbfwc-mini-cart__buttons buttons">
		<?php do_action( 'hbfwc_template_mini_cart_buttons' ); ?>
	</p>

<?php else : ?>

	<p class="bbfwc-mini-cart__empty-message"><?php esc_html_e( 'Your cart is empty', 'hotel-booking-for-woocommerce' ); ?></p>

<?php endif; ?>

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