<?php
/**
 * Add to cart form.
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/hbfwc/rates/add-to-cart.php.
 *
 * @package HBFWC\Templates
 * @version 1.0.0
 */

defined( 'ABSPATH' ) || exit;

global $product;

$maybe_has_cart_key = hbfwc_item_in_cart( $product, $rate_id, $query_args );
$display            = empty( $maybe_has_cart_key ) ? '' : 'display:none;';
$is_disabled        = $query_args['disabled'] ?? false;
?>
<form class="hbfwc-ajax-rest__form" action="<?php print esc_url( $product->get_permalink() ); ?>" method="post" enctype='multipart/form-data' style="<?php print esc_attr( $display ); ?>">
	<input type="hidden" name="check-in" value="<?php print esc_attr( $query_args['checkin'] ); ?>" />
	<input type="hidden" name="check-out" value="<?php print esc_attr( $query_args['checkout'] ); ?>" />
	<input type="hidden" name="adults" value="<?php print esc_attr( $query_args['adults'] ); ?>" />
	<input type="hidden" name="children" value="<?php print esc_attr( $query_args['children'] ); ?>" />
	<input type="hidden" name="qty" value="<?php print esc_attr( isset( $query_args['rooms'] ) ? max( 1, absint( $query_args['rooms'] ) ) : 1 ); ?>" />
	<input type="hidden" name="rate" value="<?php print esc_attr( $rate_id ); ?>" />
<?php
if ( ! empty( $query_args['child_ages'] ) ) {
	$count = 1;
	foreach ( $query_args['child_ages'] as $child_age ) {
		print '<input type="hidden" name="children_age[]" value="' . esc_attr( $child_age ) . '" />';
		++$count;
	}
}

	$button_text = apply_filters( 'hbfwc_ajax_add_to_cart_button_label', __( 'Select', 'hotel-booking-for-woocommerce' ) );
?>
	<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="button hbfwc-ajax-rest-btn alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"<?php echo ( $is_disabled ? ' disabled' : '' ); ?>><?php print esc_html( $button_text ); ?></button>
</form>

<?php
if ( ! empty( $maybe_has_cart_key ) && ! $is_disabled ) :

	print '<div class="hbfgwc-quantity-number">
		<button type="button" class="hbfwc-minus-button">&ndash;</button>
		<input type="number" value="' . esc_attr( $maybe_has_cart_key['qty'] ) . '" name="qty" min="0" step="1" class="rest-qty__action" data-key="' . esc_attr( $maybe_has_cart_key['key'] ) . '" />
		<button type="button" class="hbfwc-plus-button">&plus;</button>
		</div>';

endif; ?>
