<?php
/**
 * Accommodation booking form to add-to-cart.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/accommodation.php.
 *
 * @package HBFWC\Templates
 * @version 1.0.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

if ( ! $product->is_purchasable() ) {
	return;
}

do_action( 'woocommerce_before_add_to_cart_form' );

$minimum_stay = apply_filters( 'hbfwc_single_room_minimum_stay', $product->get_minimum_stay(), $product );
?>
<form id="hbfwc-cart" class="hbfwc-cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data'>
	<?php do_action( 'woocommerce_before_add_to_cart_accommodation_button' ); ?>

	<div tabindex="0" class="hbfwc-date-form-compact" id="hbfwc-date-form-compact" data-mindate="<?php print esc_attr(wp_date( 'm/d/Y', hbfwc_default_checkin_date() ) ); ?>" data-output-format="<?php print esc_attr( hbfwc_get_date_format() ); ?>" data-min-stays="<?php print esc_attr( $minimum_stay ); ?>">
		<input type="hidden" name="check-in" value="<?php print esc_attr( $checkin ); ?>" />
		<input type="hidden" name="check-out" value="<?php print esc_attr( $checkout ); ?>" />
		<div class="check-in">
			<span class="hbfwc-label"><?php print esc_html__( 'Check In', 'hotel-booking-for-woocommerce' ); ?></span>
			<span class="hbfwc-outputdate check-in"><?php print esc_html( $checkin_date->format( hbfwc_get_date_format() ) ); ?></span>
		</div>
		<div class="night-totals" data-singular-unit="<?php print esc_attr__( 'night', 'hotel-booking-for-woocommerce' ); ?>" data-plural-unit="<?php print esc_attr__( 'nights', 'hotel-booking-for-woocommerce' ); ?>">
			<?php
				printf(
					/* translators: Night of stayc*/
					_n( '<span class="num">%d</span> night', '<span class="num">%d</span> nights', $stay, 'hotel-booking-for-woocommerce' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					absint( $stay )
				);
				?>
		</div>
		<div class="check-out">
			<span class="hbfwc-label"><?php print esc_html__( 'Check Out', 'hotel-booking-for-woocommerce' ); ?></span>
			<span class="hbfwc-outputdate check-out"><?php print esc_html( $checkout_date->format( hbfwc_get_date_format() ) ); ?></span>
		</div>
	</div>

	<div class="hbfwc-fieldset">
		<div class="hbfwc-legend">
			<?php
			if ( 2 > $product->get_allotment() ) {
				print esc_html__( 'Guests', 'hotel-booking-for-woocommerce' ); 
			} else {
				print esc_html__( 'Rooms &amp; Guests', 'hotel-booking-for-woocommerce' ); 
			}
			?>
		</div>
		<div class="hbfwc-inner-fields">
		<?php if ( 2 > $product->get_allotment() ) { ?>
			<input type="hidden" name="qty" value="<?php echo esc_attr( $product->get_allotment() ); ?>" />
		<?php } else { ?>
			<p>
				<label for=qty><?php print esc_html__( 'Rooms', 'hotel-booking-for-woocommerce' ); ?></label>
				<input type="number" class="hbfgwc-with-buttons" name="qty" id="qty" value="<?php print esc_attr( $qty ); ?>" min="1" step="1" />
			</p>
		<?php } ?>
			<p>
				<label for="adults"><?php print esc_html__( 'Adults', 'hotel-booking-for-woocommerce' ); ?></label>
				<input type="number" class="hbfgwc-with-buttons" name="adults" id="adults" value="1" min="1" step="1" max="<?php print esc_attr( hbfwc_get_max_adults() ); ?>" data-max-per-room="<?php print esc_attr( hbfwc_get_max_adults() ); ?>" />
			</p>
			<?php
				if ( 0 < (int) hbfwc_get_max_child() ) :
			?>
			<p>
				<label for="children"><?php print esc_html__( 'Children', 'hotel-booking-for-woocommerce' ); ?></label>
				<input type="number" class="hbfgwc-with-buttons" name="children" id="children" value="0" min="0" step="1" max="<?php print esc_attr( hbfwc_get_max_child() ); ?>" data-max-per-room="<?php print esc_attr( hbfwc_get_max_child() ); ?>" />
			</p>
			<?php
				endif;
			?>
		</div>
	</div>

	<?php
		print $product->get_accommodation_rates_html( strtotime( $checkin ), strtotime( $checkout ), $qty ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	?>

	<?php
		$product->get_available_addons();
	?>

	<button type="submit" name="add-to-cart" value="<?php print esc_attr( $product->get_id() ); ?>" class="accommodation_add_to_cart_button button btn alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" <?php if ( ! $product->has_active_rates_by_date( $checkin, $checkout ) ) print 'disabled'; ?>>
		<?php print esc_html( $product->accommodation_add_to_cart_text() ); ?>
	</button>

	<?php do_action( 'woocommerce_after_add_to_cart_accommodation_button' ); ?>
</form>

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