<?php

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


use Prodigy\Includes\Helpers\Prodigy_Formatting;
use Prodigy\Includes\Helpers\Prodigy_Platform_Settings;

// need for WCAG compatibility
$as_panel                = $args['as_panel'] ?? true;
$panel_location_sufix_id = $as_panel ? '-locationselect' : '-location';
$panel_logo_sufix_id     = $as_panel ? '-logoselect' : '-logo';

static $logo_i = 0;
$logo_i++;

// Slot index for custom_per_location (0 = first Location row). Set in product-logo.php via mlc_block_index.
$args_inner        = $args ?? array();
$mlc_location_slot = isset( $args_inner['mlc_block_index'] ) && null !== $args_inner['mlc_block_index'] && '' !== $args_inner['mlc_block_index']
	? (int) $args_inner['mlc_block_index']
	: ( $logo_i - 1 );

// Multiple Logo Charge settings come from the /products API response (included array).
$mlc_settings = ! empty( $args['product'] ) ? $args['product']->get_multiple_logo_charge_settings() : array();

$mlc_enabled           = ! empty( $mlc_settings['enabled'] );
$crl_enabled           = $mlc_settings['charge-required-location'] ?? false;
$mlc_mode_raw          = strtolower( trim( (string) ( $mlc_settings['mode'] ?? '' ) ) );
$mlc_mode_aliases      = array(
	'additional_per_location'            => 'additional_per_location',
	'additional_per_logo_location'       => 'additional_per_location',
	'additional_price_per_logo_location' => 'additional_per_location',
	'custom_per_location'                => 'custom_per_location',
	'custom_per_logo_location'           => 'custom_per_location',
	'custom_price_per_logo_location'     => 'custom_per_location',
	'custom_price_for_each_logo_location'=> 'custom_per_location',
);
$mlc_mode              = $mlc_mode_aliases[ $mlc_mode_raw ] ?? $mlc_mode_raw;
$mlc_additional_raw    = $mlc_settings['additional-amount'] ?? null;
$mlc_additional_amount = ( null !== $mlc_additional_raw && '' !== $mlc_additional_raw && is_numeric( $mlc_additional_raw ) )
	? (float) $mlc_additional_raw
	: 0.0;
$mlc_custom_amounts    = $mlc_settings['custom-amounts'] ?? array();
$extra_location_charge = 0.0;
$show_extra_charge     = false;

if ( $mlc_enabled ) {
	$show_extra_charge = true;

	if ( 'additional_per_location' === $mlc_mode ) {
		$extra_location_charge = $mlc_location_slot > 0 ? $mlc_additional_amount : 0.0;
	} elseif ( 'custom_per_location' === $mlc_mode ) {
		if ( ! empty( $mlc_custom_amounts ) ) {
			$idx = $mlc_location_slot;
			if ( isset( $mlc_custom_amounts[ $idx ] ) ) {
				$entry                 = $mlc_custom_amounts[ $idx ];
				$extra_location_charge = is_array( $entry ) && isset( $entry['amount'] )
					? (float) $entry['amount']
					: (float) $entry;
			} else {
				$last                  = $mlc_custom_amounts[ count( $mlc_custom_amounts ) - 1 ];
				$extra_location_charge = is_array( $last ) && isset( $last['amount'] )
					? (float) $last['amount']
					: (float) $last;
			}
		}
	}
}
?>

<div
	class="prodigy-logo-tool__form-container form-container prodigy-custom-template"
	<?php if ( $as_panel ) : ?>
		id="<?php echo esc_attr( $uid ); ?>-panel" aria-hidden="true"
	<?php else : ?>
		aria-hidden="false"
	<?php endif; ?>
>
	<div
		class="prodigy-product__attr prodigy-product__logo prodigy-product__logo-location prodigy-product-location-block-js">
		<div class="prodigy-product__attr-item prodigy-product__logo-item">
			<div
				class="prodigy-product__attr-text prodigy-product__logo-text d-flex justify-content-between align-items-center">
				<div
					class="prodigy-product__attr-text-label prodigy-product__logo-text-label">
					<span
						class="prodigy-product__attr-text-label--inner prodigy-product__logo-text-label--inner">
						<?php esc_html_e( 'Location', 'prodigy-commerce' ); ?>
						<?php if ( $show_extra_charge ) : ?>
							<span class="prodigy-location-extra-charge-js">
							<?php echo esc_html( '(+$' . Prodigy_Formatting::prodigy_price_format( $extra_location_charge ) . ')' ); ?>
							</span>
						<?php endif; ?>
					</span>
				</div>
			</div>
			<label for="<?php echo esc_attr( $panel_location_sufix_id ); ?>"
				class="prodigy-visually-hidden"><?php esc_html_e( 'Select logo location', 'prodigy-commerce' ); ?></label>
			<select
				name="logo_location_values"
				id="<?php echo esc_attr( $panel_location_sufix_id ); ?>"
				class="prodigy-main-select logo_location_values form-control prodigy-logo-location-js"
				data-extra-charge="<?php echo esc_attr( (string) $extra_location_charge ); ?>"
				data-mlc-mode="<?php echo esc_attr( $mlc_mode ); ?>"
				data-mlc-slot-index="<?php echo esc_attr( (string) $mlc_location_slot ); ?>"
				data-mlc-additional-charge="<?php echo esc_attr( (string) $mlc_additional_amount ); ?>"
				data-mlc-custom-charges="<?php echo esc_attr( wp_json_encode( $mlc_custom_amounts ) ); ?>"
        data-required-mode="<?php echo (int) $crl_enabled; ?>"

			>
			<?php foreach ( $args['product']->get_logo_locations() as $location ) : ?>
				<?php $location_price = isset( $location['attributes']['charge-amount'] ) && ( $location['attributes']['charge-amount'] !== '0.0' ) ? '(+$' . Prodigy_Formatting::prodigy_price_format( $location['attributes']['charge-amount'] ) . ')' : ''; ?>
				<?php $location_text = $location['attributes']['required'] ? ' (Required)' . $location_price : ' ' . $location_price; ?>
						<option value="<?php echo esc_attr( $location['id'] ); ?>"
										data-attributes="<?php echo wp_json_encode( $location['attributes'] ); ?>"
										data-required="<?php echo (int) $location['attributes']['required']; ?>"

				<?php if ( isset( $location['attributes']['charge-amount'] ) ) : ?>
									data-price="<?php echo (float) $location['attributes']['charge-amount']; ?>"
				<?php endif; ?>
						>
				<?php echo esc_html( $location['attributes']['name'] . $location_text ); ?>
						</option>
			<?php endforeach ?>
			</select>
			<div class="prodigy-selectbox__arrow" role="presentation" aria-hidden="true"></div>
		</div>
	</div>

	<?php
	if ( ! $args['is_enable_logo_swatches'] ) :
		?>
			<div
				class="prodigy-product__attr prodigy-product__logo prodigy-product-logo-block-js">
				<div class="prodigy-product__attr-item prodigy-product__logo-item">
					<div
						class="prodigy-product__attr-text prodigy-product__logo-text d-flex justify-content-between align-items-center">
						<div
							class="prodigy-product__attr-text-label prodigy-product__logo-text-label">
						<span
							class="prodigy-product__attr-text-label--inner prodigy-product__logo-text-label--inner">
							<?php esc_html_e( 'Logo', 'prodigy-commerce' ); ?>
						</span>
						</div>
					</div>

					<label
							for="<?php echo esc_attr( $panel_logo_sufix_id ); ?>"
							class="prodigy-visually-hidden"><?php esc_html_e( 'Select logo', 'prodigy-commerce' ); ?>
					</label>
					
					<select name="logo_values"
							id="<?php echo esc_attr( $panel_logo_sufix_id ); ?>"
							class="prodigy-main-select prodigy-main-logoselect logo_values form-control prodigy-logo-values-js">
				<?php foreach ( array_reverse( $args['product']->get_logos() ) as $logo ) : ?>
					<?php $logo_charge_amount = (float) ( $logo['attributes']['charge-amount'] ?? 0 ); ?>
					<?php $logo_name = $logo['attributes']['name']; ?>
					<?php if ( Prodigy_Platform_Settings::is_redemption_store_status() ) : ?>
						<?php $logo_name .= '(+' . get_option( 'pg_currency_type' ) . Prodigy_Formatting::prodigy_price_format( $logo_charge_amount ) . ')'; ?>
					<?php endif; ?>
								<option
									data-price="<?php echo esc_attr( $logo_charge_amount ); ?>"
									value="<?php echo esc_attr( $logo['id'] ); ?>"
									data-attributes='<?php echo wp_json_encode( $logo['attributes'] ); ?>'
									data-image="<?php echo esc_url( $logo['attributes']['original-url'] ); ?>"
								>
					<?php echo esc_html( $logo_name ); ?>
								</option>
				<?php endforeach ?>
					</select>
					<div class="prodigy-selectbox__arrow" role="presentation" aria-hidden="true"></div>
				</div>
			</div>
	<?php else : ?>
			<div
				class="prodigy-product__attr-item--no-select prodigy-product__attr-item--logo-select">
				<div
					class="prodigy-product__attr-text prodigy-product__logo-text d-flex justify-content-between align-items-center">
					<div class="prodigy-product__attr-text-label">
				<span class="prodigy-product__attr-text-label--inner">
					<?php esc_html_e( 'Logo', 'prodigy-commerce' ); ?>
				</span>
					</div>
				</div>
				<div
					class="prodigy-product__attr-item--no-select-value prodigy-product__logo-item d-flex flex-column">
			<span
				class="prodigy-product__attr-text prodigy-product__logo-text prodigy-no-select">
				<?php esc_html_e( 'Selected', 'prodigy-commerce' ); ?>:
				<span
					class="prodigy-product__attr-text--name swatch-logo-name-js"></span>
			</span>
					<form
						class="w-100">
						<fieldset class="d-flex flex-wrap align-items-center prodigy-radio__logo-btn-wrap position-relative">
							<legend class="prodigy-visually-hidden">
								<?php esc_html_e( 'Select a logo', 'prodigy-commerce' ); ?>
							</legend>

				<?php foreach ( $args['product']->get_logos() as $key => $logo ) : ?>
					<?php $logo_charge_amount = (float) ( $logo['attributes']['charge-amount'] ?? 0 ); ?>
					<?php $logo_name = empty( $logo_charge_amount ) ? $logo['attributes']['name'] : $logo['attributes']['name']; ?>
					<?php if ( Prodigy_Platform_Settings::is_redemption_store_status() ) : ?>
						<?php $logo_name .= '(+' . get_option( 'pg_currency_type' ) . Prodigy_Formatting::prodigy_price_format( $logo_charge_amount ) . ')';
							$input_logo_id = preg_replace('/\s+/', '_', trim($logo_name) . '_' . $logo['id'] . '_' . $logo_i . '_' . $key);
						?>
					<?php endif; ?>

								<div
									class="flex-wrap align-items-center prodigy-radio__btn-wrap position-md-relative prodigy-tooltip prodigy-tooltip-js"
								>
						<span
							class="prodigy-tooltip__message prodigy-tooltip__not-allowed-logo-message">
							<?php esc_html_e( 'Not available with the selected product color', 'prodigy-commerce' ); ?>
						</span>
									<span class="prodigy-after__backdrop" aria-hidden="true"></span>
									<div
										class="prodigy-product__attr-item--no-select-value d-flex flex-wrap align-items-center">
										<div
											class="prodigy-radio__swatch-btn prodigy-radio__swatch-logo-btn">
											<input
												type="radio"
												name="radio.swatch.logo"
												class="prodigy-product__btn-no-select prodigy-product__swatch  prodigy-product__logo-swatch prodigy-product__logo-swatch-js"
												value="<?php echo esc_attr( $logo['id'] ); ?>"
												data-attributes='<?php echo wp_json_encode( $logo['attributes'] ); ?>'
												data-price="<?php echo esc_attr( $logo_charge_amount ); ?>"
												data-name="<?php echo esc_attr( $logo_name ); ?>"
												<?php echo empty( $key ) ? 'checked' : ''; ?>
												id="<?php echo esc_attr( $input_logo_id ); ?>"/>
											<label for="<?php echo esc_attr( $input_logo_id ); ?>"></label>
											<input type="image"
														alt="<?php echo esc_attr( $logo_name ); ?>"
														class="prodigy-product__btn-no-select prodigy-product__swatch prodigy-product__swatch-init-js prodigy-product__logo-swatch position-relative"
														src="<?php echo esc_url( $logo['attributes']['original-url'] ); ?>"/>
										</div>
									</div>
								</div>
				<?php endforeach; ?>
						</fieldset>
					</form>
				</div>
			</div>
	<?php endif; ?>
</div>


