<?php
/**
 * FoodOrd Product Modal Toppings Section
 *
 * Allows customers to select topping sets within the product modal.
 *
 * @package FoodOrd
 * @var array $foodord_topping_sets Prepared topping sets for display.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( empty( $foodord_topping_sets ) || ! is_array( $foodord_topping_sets ) ) {
	return;
}
?>

<div class="foodord-product-toppings">
	<?php
	foreach ( $foodord_topping_sets as $foodord_index => $foodord_set ) :
		$foodord_items = isset( $foodord_set['items'] ) && is_array( $foodord_set['items'] ) ? $foodord_set['items'] : array();
		if ( empty( $foodord_items ) ) {
			continue;
		}

		$foodord_total_items   = count( $foodord_items );
		$foodord_should_expand = $foodord_total_items > 3;
		$foodord_set_id        = isset( $foodord_set['id'] ) ? $foodord_set['id'] : 'set_' . $foodord_index;
		$foodord_set_name_raw  = isset( $foodord_set['name'] ) ? $foodord_set['name'] : '';
		$foodord_set_name      = is_string( $foodord_set_name_raw ) ? trim( $foodord_set_name_raw ) : '';
		$foodord_normalized    = '' !== $foodord_set_name ? strtolower( $foodord_set_name ) : '';
		if ( 'dropdown group name' === $foodord_normalized ) {
			$foodord_set_name = '';
		}
		$foodord_set_desc              = isset( $foodord_set['label'] ) ? $foodord_set['label'] : '';
		$foodord_group_type            = isset( $foodord_set['group_type'] ) ? $foodord_set['group_type'] : 'dropdown';
		$foodord_is_required           = ! empty( $foodord_set['is_required'] );
		$foodord_hide_price            = ! empty( $foodord_set['hide_price'] );
		$foodord_checkbox_min          = isset( $foodord_set['checkbox_min'] ) ? (int) $foodord_set['checkbox_min'] : null;
		$foodord_checkbox_max          = isset( $foodord_set['checkbox_max'] ) ? (int) $foodord_set['checkbox_max'] : null;
		$foodord_has_hard_min          = ( 'checkbox' === $foodord_group_type && null !== $foodord_checkbox_min && $foodord_checkbox_min > 0 );
		$foodord_is_effective_required = $foodord_is_required || $foodord_has_hard_min;
		$foodord_required_count        = 0;
		if ( $foodord_is_effective_required ) {
			$foodord_required_count = 1;
			if ( 'checkbox' === $foodord_group_type && null !== $foodord_checkbox_min && $foodord_checkbox_min > 0 ) {
				$foodord_required_count = absint( $foodord_checkbox_min );
			}
		}
		$foodord_badge_is_dynamic       = $foodord_has_hard_min;
		$foodord_required_template_attr = '';
		if ( $foodord_badge_is_dynamic ) {
			/* translators: %d: minimum required selections. */
			$foodord_required_template_attr = __( '%d Required', 'foodord' );
		}
		$foodord_options_id = 'foodord-toppings-options-' . sanitize_html_class( $foodord_set_id . '-' . $foodord_index );
		$foodord_remaining  = $foodord_should_expand ? $foodord_total_items - 3 : 0;
		// translators: %d is the number of additional items to show.
		$foodord_more_text = $foodord_should_expand ? sprintf( esc_html__( 'Show %d more', 'foodord' ), $foodord_remaining ) : '';
		// translators: %d is the number of items to hide.
		$foodord_less_text = $foodord_should_expand ? sprintf( esc_html__( 'Show %d less', 'foodord' ), $foodord_remaining ) : '';
		?>

	<div class="foodord-product-toppings__set-header">
		<?php if ( $foodord_set_desc ) : ?>
		<span class="foodord-product-toppings__set-title"><?php echo esc_html( $foodord_set_desc ); ?></span>
		<?php endif; ?>
		<?php if ( $foodord_is_effective_required ) : ?>
			<span
				class="foodord-product-toppings__badge foodord-product-toppings__badge--required"
				<?php if ( $foodord_badge_is_dynamic ) : ?>
				data-required-min="<?php echo esc_attr( (string) $foodord_required_count ); ?>"
				data-required-template-number="<?php echo esc_attr( $foodord_required_template_attr ); ?>"
				<?php endif; ?>
				data-required-text="<?php echo esc_attr__( 'Required', 'foodord' ); ?>"
			>
				<?php
				if ( $foodord_badge_is_dynamic ) {
					/* translators: %d: minimum required selections. */
					printf( esc_html__( '%d Required', 'foodord' ), absint( $foodord_required_count ) );
				} else {
					esc_html_e( 'Required', 'foodord' );
				}
				?>
			</span>
		<?php else : ?>
			<span class="foodord-product-toppings__badge foodord-product-toppings__badge--optional"><?php esc_html_e( 'Optional', 'foodord' ); ?></span>
		<?php endif; ?>
	</div>

	<fieldset
		class="foodord-product-toppings__set"
		data-set-id="<?php echo esc_attr( $foodord_set_id ); ?>"
		data-group-type="<?php echo esc_attr( $foodord_group_type ); ?>"
		data-is-required="<?php echo esc_attr( $foodord_is_effective_required ? '1' : '0' ); ?>"
		data-checkbox-min="<?php echo esc_attr( null !== $foodord_checkbox_min ? (string) $foodord_checkbox_min : '' ); ?>"
		data-checkbox-max="<?php echo esc_attr( null !== $foodord_checkbox_max ? (string) $foodord_checkbox_max : '' ); ?>"
		data-set-label="<?php echo esc_attr( $foodord_set_desc ? $foodord_set_desc : $foodord_set_name ); ?>"
		data-set-name="<?php echo esc_attr( $foodord_set_name ); ?>"
	>

		<?php if ( 'dropdown' === $foodord_group_type ) : ?>
			<div class="foodord-product-toppings__control">
				<select
					name="foodord_toppings[<?php echo esc_attr( $foodord_set_id ); ?>]"
					class="foodord-product-toppings__select"
				>
					<?php
					foreach ( $foodord_items as $foodord_item ) :
						$foodord_option_id    = isset( $foodord_item['id'] ) ? $foodord_item['id'] : '';
						$foodord_option_label = isset( $foodord_item['label'] ) ? $foodord_item['label'] : '';
						$foodord_option_price = isset( $foodord_item['price'] ) ? (float) $foodord_item['price'] : 0.0;
						$foodord_is_default   = ! empty( $foodord_item['is_default'] );
						if ( '' === $foodord_option_label ) {
							continue;
						}
						?>
					<option value="<?php echo esc_attr( $foodord_option_id ); ?>" data-price="<?php echo esc_attr( (string) $foodord_option_price ); ?>" data-option-label="<?php echo esc_attr( $foodord_option_label ); ?>" <?php selected( $foodord_is_default ); ?>>
						<?php echo esc_html( $foodord_option_label ); ?>
						<?php if ( ! $foodord_hide_price && $foodord_option_price > 0 && function_exists( 'wc_price' ) ) : ?>
							(<?php echo wp_kses_post( wc_price( $foodord_option_price ) ); ?>)
						<?php endif; ?>
					</option>
					<?php endforeach; ?>
				</select>
			</div>
	<?php elseif ( 'radio' === $foodord_group_type ) : ?>
		<div
			class="foodord-product-toppings__options<?php echo esc_attr( $foodord_should_expand ? ' foodord-product-toppings__options--collapsible' : '' ); ?>"
			role="radiogroup"
			<?php if ( $foodord_should_expand ) : ?>
			id="<?php echo esc_attr( $foodord_options_id ); ?>"
			data-total-items="<?php echo esc_attr( (string) $foodord_total_items ); ?>"
			<?php endif; ?>
		>
				<?php
				foreach ( $foodord_items as $foodord_item_index => $foodord_item ) :
					$foodord_option_id    = isset( $foodord_item['id'] ) ? $foodord_item['id'] : '';
					$foodord_option_label = isset( $foodord_item['label'] ) ? $foodord_item['label'] : '';
					$foodord_option_price = isset( $foodord_item['price'] ) ? (float) $foodord_item['price'] : 0.0;
					$foodord_is_default   = ! empty( $foodord_item['is_default'] );
					if ( '' === $foodord_option_label ) {
						continue;
					}
					$foodord_input_id = 'foodord-' . sanitize_title( $foodord_set_id ) . '-' . $foodord_item_index;
					?>
					<label class="foodord-product-toppings__option" for="<?php echo esc_attr( $foodord_input_id ); ?>">
						<div class="foodord-product-toppings__option-content">
						<input
							type="radio"
							class="foodord-product-toppings__input"
							name="foodord_toppings[<?php echo esc_attr( $foodord_set_id ); ?>]"
							value="<?php echo esc_attr( $foodord_option_id ); ?>"
							id="<?php echo esc_attr( $foodord_input_id ); ?>"
							data-price="<?php echo esc_attr( (string) $foodord_option_price ); ?>"
							data-option-label="<?php echo esc_attr( $foodord_option_label ); ?>"
							<?php checked( $foodord_is_default ); ?>
						>
						<span class="foodord-product-toppings__option-label"><?php echo esc_html( $foodord_option_label ); ?></span>
						</div>
					<?php if ( ! $foodord_hide_price && $foodord_option_price > 0 && function_exists( 'wc_price' ) ) : ?>
						<span class="foodord-product-toppings__option-price"><?php echo wp_kses_post( wc_price( $foodord_option_price ) ); ?></span>
					<?php endif; ?>
					</label>
				<?php endforeach; ?>
			</div>
		<?php if ( $foodord_should_expand ) : ?>
			<button
				type="button"
				class="foodord-product-toppings__toggle"
				data-collapsed-text="<?php echo esc_attr( $foodord_more_text ); ?>"
				data-expanded-text="<?php echo esc_attr( $foodord_less_text ); ?>"
				aria-expanded="false"
				aria-controls="<?php echo esc_attr( $foodord_options_id ); ?>"
			>
				<span class="foodord-product-toppings__toggle-text"><?php echo esc_html( $foodord_more_text ); ?></span>
				<span class="foodord-product-toppings__toggle-icon" aria-hidden="true"></span>
			</button>
		<?php endif; ?>
		<?php else : ?>
		<div
			class="foodord-product-toppings__options<?php echo esc_attr( $foodord_should_expand ? ' foodord-product-toppings__options--collapsible' : '' ); ?>"
			role="group"
			<?php if ( $foodord_should_expand ) : ?>
			id="<?php echo esc_attr( $foodord_options_id ); ?>"
			data-total-items="<?php echo esc_attr( (string) $foodord_total_items ); ?>"
			<?php endif; ?>
		>
				<?php
				foreach ( $foodord_items as $foodord_item_index => $foodord_item ) :
					$foodord_option_id    = isset( $foodord_item['id'] ) ? $foodord_item['id'] : '';
					$foodord_option_label = isset( $foodord_item['label'] ) ? $foodord_item['label'] : '';
					$foodord_option_price = isset( $foodord_item['price'] ) ? (float) $foodord_item['price'] : 0.0;
					$foodord_is_default   = ! empty( $foodord_item['is_default'] );
					if ( '' === $foodord_option_label ) {
						continue;
					}
					$foodord_input_id = 'foodord-' . sanitize_title( $foodord_set_id ) . '-' . $foodord_item_index;
					?>
					<label class="foodord-product-toppings__option" for="<?php echo esc_attr( $foodord_input_id ); ?>">
						<div class="foodord-product-toppings__option-content">
						<input
							type="checkbox"
							class="foodord-product-toppings__input"
							name="foodord_toppings[<?php echo esc_attr( $foodord_set_id ); ?>][]"
							value="<?php echo esc_attr( $foodord_option_id ); ?>"
							id="<?php echo esc_attr( $foodord_input_id ); ?>"
							data-price="<?php echo esc_attr( (string) $foodord_option_price ); ?>"
							data-option-label="<?php echo esc_attr( $foodord_option_label ); ?>"
							<?php checked( $foodord_is_default ); ?>
						>
						<span class="foodord-product-toppings__option-label"><?php echo esc_html( $foodord_option_label ); ?></span>
						</div>
					<?php if ( ! $foodord_hide_price && $foodord_option_price > 0 && function_exists( 'wc_price' ) ) : ?>
						<span class="foodord-product-toppings__option-price"><?php echo wp_kses_post( wc_price( $foodord_option_price ) ); ?></span>
					<?php endif; ?>
				</label>
				<?php endforeach; ?>
		</div>
			<?php if ( $foodord_should_expand ) : ?>
			<button
				type="button"
				class="foodord-product-toppings__toggle"
				data-collapsed-text="<?php echo esc_attr( $foodord_more_text ); ?>"
				data-expanded-text="<?php echo esc_attr( $foodord_less_text ); ?>"
				aria-expanded="false"
				aria-controls="<?php echo esc_attr( $foodord_options_id ); ?>"
			>
				<span class="foodord-product-toppings__toggle-text"><?php echo esc_html( $foodord_more_text ); ?></span>
				<span class="foodord-product-toppings__toggle-icon" aria-hidden="true"></span>
			</button>
		<?php endif; ?>
		<?php endif; ?>
	</fieldset>
	<?php endforeach; ?>
</div>
