<?php
/**
 * Amenities tab
 *
 * @package HBFWC
 */

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

$groups = hbfwc_get_amenities_group();
?>
<div id="hbfwc_amenities_product_data" class="panel woocommerce_options_panel">
	<div class="hbfwc-toolbar">
		<span class="hbfwc-lead"><?php print esc_html__( 'Amenities', 'hotel-booking-for-woocommerce' ); ?></span>
		<div class="hbfwc-rp-selections">
			<a href="#" id="hbfwc-new-amenity"><?php print esc_html__( '+ Add new amenity', 'hotel-booking-for-woocommerce' ); ?></a>
		</div>
	</div>

	<div id="new-amenity-form">
<?php
	$select_groups = array( '' => __( 'Select a group', 'hotel-booking-for-woocommerce' ) ) + $groups;
	woocommerce_wp_select(
		array(
			'label'       => __( 'Group', 'hotel-booking-for-woocommerce' ),
			'id'          => 'am-group',
			'name'        => 'am_group',
			'value'       => '',
			'options'     => $select_groups,
			'description' => __( 'Amenities group.', 'hotel-booking-for-woocommerce' ),
			'desc_tip'    => true,
		)
	);

	woocommerce_wp_text_input(
		array(
			'label' => __( 'Amenity name', 'hotel-booking-for-woocommerce' ),
			'id'    => 'am-name',
			'name'  => 'am_name',
			'value' => '',
		)
	);

	woocommerce_wp_select(
		array(
			'label'       => __( 'Icon', 'hotel-booking-for-woocommerce' ),
			'id'          => 'am-icon-type',
			'name'        => 'am_icon_type',
			'value'       => '',
			'options'     => array(
				''                     => __( 'Select the icon style', 'hotel-booking-for-woocommerce' ),
				'fontawesome-6-brands' => 'Font awesome 6 brands',
				'fontawesome-6'        => 'Font awesome 6',
				'ionicons'             => 'Ionicons',
				'themify'              => 'Themify icon',
				'svg'                  => __( 'Custom SVG', 'hotel-booking-for-woocommerce' ),
			),
			'description' => __( 'Icon style.', 'hotel-booking-for-woocommerce' ),
			'desc_tip'    => true,
		)
	);
	?>
		<div id="hbfwc-am-icon" style="display: none;">
			<span><?php print esc_html__( 'Select an icon', 'hotel-booking-for-woocommerce' ); ?></span><br/>
			<div id="hbfwc-icon-output"></div>
		</div>

<?php
	woocommerce_wp_textarea_input(
		array(
			'label'             => __( 'SVG code', 'hotel-booking-for-woocommerce' ),
			'id'                => 'am-icon-svg',
			'name'              => 'am_icon_svg',
			'style'             => 'height:100px;',
			'value'             => '',
			'custom_attributes' => array(
				'min'  => '0',
				'step' => '1',
			),
			'description'       => __( 'Enter the SVG icon code.', 'hotel-booking-for-woocommerce' ),
			'desc_tip'          => true,
		)
	);
	?>
		<p class="buttons">
			<a href="#" id="hbfwc-new-amenity-submit" class="button"><?php print esc_html__( '+ Add new amenity', 'hotel-booking-for-woocommerce' ); ?></a>
			<span class="hbfwc-alert"><?php print esc_html__( 'All fields are mandatory.', 'hotel-booking-for-woocommerce' ); ?></span>
		</p>
	</div>
<?php
foreach ( $groups as $group_key => $group_name ) {
	$amenity_terms = hbfwc_get_amenities_by_group( $group_key );
	?>
	<div class="amenities-group-select">
		<h4><?php print esc_html( $group_name ); ?></h4>
		<div class="amenities-group-select__terms" id="amenities-group_<?php print esc_attr( $group_key ); ?>">
		<?php
		if ( $amenity_terms ) {
			?>
			<ul class="amenities-term__list">
			<?php
			foreach ( $amenity_terms as $amenity_term ) {
				$icon_type   = get_term_meta( $amenity_term->term_id, 'icon_type', true );
				$checked_val = has_term( $amenity_term->term_id, 'amenities', $product->get_id() ) ? $amenity_term->term_id : '';
				print '<li>';
				print '<label>
						<input type="checkbox" name="amenity[]" value="' . esc_attr( $amenity_term->term_id ) . '" ' . checked( $checked_val, $amenity_term->term_id, false ) . '/>' . ( ( 'svg' === $icon_type )
						? wp_kses( get_term_meta( $amenity_term->term_id, 'icon_svg', true ), hbfwc_get_svg_args() )
						: '<i class="' . esc_attr( get_term_meta( $amenity_term->term_id, 'icon_class', true ) ) . '"></i>' ) . ' ' . esc_html( $amenity_term->name ) . '</label>';
				print '</li>';
			}
			?>
			</ul>
			<?php
		} else {
			print '<p class="description">' . esc_html__( 'No amenities yet.', 'hotel-booking-for-woocommerce' ) . '</p>';
		}
		?>
		</div>
	</div>
	<?php
}
?>

</div>
