<?php
/**
 * Addon metabox template.
 * @since 1.0.6
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

?>

<div id="hbfwc-addon-metabox-wrapper">
<?php
	foreach( $fields as $key => $field ) {
		print '<p class="hbfwc-field field-' . esc_attr( $key ) . ' ' . esc_attr( ( $field['wrapper_class'] ?? '' ) ) . '">';
		print '<label class="' . esc_attr( ( $field['parent_class'] ?? '' ) ) . '" for="addon-field-' . esc_attr( $key ) . '">' . esc_html( ( $field['label'] ?? '' ) ) . '</label>';
		print '<span class="addon-field-wrap ' . esc_attr( ( $field['parent_class'] ?? '' ) ) . '">';

		$disabled = ( isset( $field['disabled'] ) && $field['disabled'] ) ? ' disabled' : '';

		switch( $field['type'] ) {
			case 'toggle':

				print '<input type="checkbox" role="switch" id="addon-field-' . esc_attr( $key ) . '" name="' . esc_attr( $key ) . '" value="yes" ' . checked( $metabox::get_value( $post, $key, $field['default'] ), 'yes', false ) . $disabled . '/>';

				break;

			case 'textarea':

				print '<textarea name="' . esc_attr( $key ) . '" id="addon-field-' . esc_attr( $key ) . '" rows="3">' . wp_kses_post( $metabox::get_value( $post, $key, $field['default'] ) ) . '</textarea>';
				
				break;

			case 'woo-select':
				$product_types = wc_get_product_types();
				if ( isset($product_types['accommodation']) ) {
					unset($product_types['accommodation']);
				}
				$product_types = array_keys( $product_types );

				print '<select class="wc-product-search" multiple="multiple" style="width: 50%;" name="' . esc_attr( $key ) . '[]" data-exclude_type="' . esc_attr( implode(',', $product_types) ) . '" data-placeholder="' . esc_attr__( 'Search for a room&hellip;', 'hotel-booking-for-woocommerce' ) . '" data-action="woocommerce_json_search_products_and_variations">';
				$values = $metabox::get_value( $post, $key, [] );
				if ( ! empty($values) ) {
					foreach( $values as $product_id ) {
						$product = wc_get_product( $product_id );
						if ( is_object( $product ) ) {
							echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . esc_html( wp_strip_all_tags( $product->get_formatted_name() ) ) . '</option>';
						}
					}
				}
				print '</select>';

				break;

			case 'radio':
				foreach( $field['options'] as $value => $radio_ops ) {
					$radio_disabled = ( isset( $radio_ops['disabled'] ) && $radio_ops['disabled'] ) ? ' disabled' : '';
					print '<span class="addon-radio">';
					print '<label>';
					print '<input type="radio" name="' . esc_attr( $key ) . '" value="' . esc_attr( $value ) .'" ' . checked( $metabox::get_value( $post, $key, $field['default'] ), $value, false ) . ' ' . $radio_disabled . ' />';
					print ( $radio_ops['label'] ?? '' );
					print '</label>';
					$radio_use_tooltip = $radio_ops['use_tooltip'] ?? false;
					$radio_description = $radio_ops['description'] ?? '';
					if ( '' !== $radio_description ) {
						if ( $radio_use_tooltip ) {
							print \wc_help_tip( esc_attr( $radio_description ) );
						} else {
							print '<br/>';
							print '<span class="addon-desc">' . wp_kses_post( $radio_description ) . '</span>';
						}
					}
					print '</span>';
				}
				break;
			
			case 'text':
			case 'number':
			default:

				print '<input type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $key ) . '" id="addon-field-' . esc_attr( $key ) . '" class="addon-field ' . esc_attr( ( $field['class'] ?? '' ) ) . '" value="' . esc_attr( $metabox::get_value( $post, $key, $field['default'] ) ) . '" ' . $disabled . '/>';

				break;
		}

		$use_tooltip = $field['use_tooltip'] ?? false;
		$description = $field['description'] ?? '';

		if ( '' !== $description ) {
			if ( $use_tooltip ) {
				print \wc_help_tip( esc_attr( $description ) );
			} else {
				print '<br/>';
				print '<span class="addon-desc">' . wp_kses_post( $description ) . '</span>';
			}
		}

		print '</span>';
		print '</p>';
	}
?>
</div>
