<?php
/**
 * Product rates.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/hbfwc/rates/active-rates.php.
 *
 * @package HBFWC\Templates
 * @version 1.0.32
 */

defined( 'ABSPATH' ) || exit;

global $product;

$rates = $product->get_active_rates();

if ( $rates ) {
	foreach ( $rates as $rate ) {
		$adds_errors = [];
		$query_args['disabled'] = false;
		$rooms = isset( $query_args['rooms'] ) ? max( 1, absint( $query_args['rooms'] ) ) : 1;
		// If rate not active. Skip.
		if ( ! in_array( $rate->term_id, $product->has_active_rates_by_date( $query_args['checkin'], $query_args['checkout'] ), true ) ) {
			if ( hbfwc_hide_unavailable_items_in_search() ) {
				continue;
			} else {
				$query_args['disabled'] = true;
				$adds_errors[] = __( 'This rate is not available for the selected dates.', 'hotel-booking-for-woocommerce' );
				$classes[] = 'hbfwc-error_rate_not_available';
			}
		}

		$need_more_stay = intval( $product->get_minimum_stay_for_rate( $rate->term_id ) ) > intval( $query_args['stay'] );
		$classes = [ 'hbfwc-room__rate' ];
		if ( $need_more_stay ) {
			$query_args['disabled'] = true;
			$classes[] = 'hbfwc-error_stay_length';
		}

		if ( ! $query_args['disabled'] ) {
			$available_by_dates_and_room = $product->check_availability( $query_args['checkin'], $query_args['checkout'], $rooms );
			if ( is_wp_error( $available_by_dates_and_room ) ) {
				$classes[] = 'hbfwc-error_available_by_dates_and_room';
				$query_args['disabled'] = true;

				$error_arrs = explode( '<br/>', $available_by_dates_and_room->get_error_message() );
				foreach( $error_arrs as $earr ) {
					$adds_errors[] = $earr;
				}
			}

			// Check availability by allowed adults (combined capacity across all requested rooms).
			if ( $product->get_max_adults() && ( absint( $product->get_max_adults() ) * $rooms ) < absint( $query_args['adults'] ) ) {
				$classes[] = 'hbfwc-error_adult_guest_count';
				$query_args['disabled'] = true;
				if ( $rooms > 1 ) {
					$adds_errors[] = sprintf(
						/* translators: 1: max adult per room, 2: total combined capacity, 3: number of rooms */
						__( 'This room allows maximum %1$d adult per room (%2$d total for %3$d rooms).', 'hotel-booking-for-woocommerce' ),
						absint( $product->get_max_adults() ),
						absint( $product->get_max_adults() ) * $rooms,
						$rooms
					);
				} else {
					$adds_errors[] = sprintf(
						/* translators: %d: Number of adult allowed */
						__( 'This room allows maximum %d adult.', 'hotel-booking-for-woocommerce' ),
						absint( $product->get_max_adults() )
					);
				}
			}

			// Check availability by allowed children (combined capacity across all requested rooms).
			if ( ! $product->get_max_children() && intval( $query_args['children'] ) > 0 ) {
				$classes[] = 'hbfwc-error_childres_guest_count';
				$query_args['disabled'] = true;
				$adds_errors[] = __( 'This room does not allow children.', 'hotel-booking-for-woocommerce' );
			}

			if ( absint( $product->get_max_children() ) > 0 && ( absint( $product->get_max_children() ) * $rooms ) < absint( $query_args['children'] ) ) {
				$classes[] = 'hbfwc-error_childres_guest_count';
				$query_args['disabled'] = true;
				if ( $rooms > 1 ) {
					$adds_errors[] = sprintf(
						/* translators: 1: max children per room, 2: total combined capacity, 3: number of rooms */
						__( 'This room allows maximum %1$d children per room (%2$d total for %3$d rooms).', 'hotel-booking-for-woocommerce' ),
						absint( $product->get_max_children() ),
						absint( $product->get_max_children() ) * $rooms,
						$rooms
					);
				} else {
					$adds_errors[] = sprintf(
						/* translators: %d: Number of children allowed */
						__( 'This room allows maximum %d children.', 'hotel-booking-for-woocommerce' ),
						absint( $product->get_max_children() )
					);
				}
			}
		}
		?>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" id="hbfwc-room__rate-<?php print esc_attr( $product->get_id() ); ?>-<?php print esc_attr( $rate->term_id ); ?>">
	<div class="hbfwc-room__rate-info">
		<h5><?php print esc_html( $rate->name ); ?></h5>
		<?php
			if ( hbfwc_show_min_stay_info() ) {
				printf(
					'<span class="rate-minimum-stay"><i class="ion-md-clock"></i> %s</span>',
					esc_html(
						sprintf(
							/* translators: Number of stay */
							__( 'Minimum %d-night stay', 'hotel-booking-for-woocommerce' ),
							intval( $product->get_minimum_stay_for_rate( $rate->term_id ) )
						)
					)
				);
			}

			if ( ! empty( $adds_errors ) ) {
				echo '<div class="hbfwc-error-availability">' . "\n";
				echo '<i class="ion-ios-alert"></i>' . "\n";
				echo implode( '<br>', array_map( 'esc_html', $adds_errors ) );
				echo '</div>' . "\n";
			}

			hbfwc_template_show_rate_features( $rate->term_id );
			hbfwc_template_rate_link_to_popup( $rate->term_id );
		?>
	</div>
	<div class="hbfwc-room__rate-action">
		<?php
			echo wp_kses_post( $product->get_rate_price_breakdowns( $rate->term_id, $query_args['checkin'], $query_args['checkout'] ) );
			do_action( 'hbfwc_template_rate_add_to_cart', $rate->term_id, $query_args );
		?>
	</div>
</div>
		<?php
	}
}
