<?php

use DropshippingExportFreeVendor\WPDesk\Library\DropshippingExport\Form\Fields\ExportMapperFormFields;

/**
 * @var \WPDesk\Forms\Field $field
 * @var \WPDesk\View\Renderer\Renderer $renderer
 * @var string $name_prefix
 * @var string $value
 *
 * @var string $template_name Real field template.
 */

$single_value_columns = [
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_TYPE,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_LOGIC,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_MATH,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_MATH_VALUE,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_TYPE,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_LOGIC,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_MATH,
	ExportMapperFormFields::PRODUCT_ADVANCED_MOD_MATH_VALUE,
];

$allowed_attributes = [
	'id'           => [],
	'type'         => [],
	'class'        => [],
	'data-value'   => [],
	'data-allowed' => [],
	'data-type'    => [],
	'placeholder'  => [],
	'name'         => [],
	'value'        => [],
	'selected'     => [],
	'disabled'     => [],
	'required'     => [],
	'multiple'     => [],
];

$items_nr = is_array( $value ) && is_array( reset( $value ) ) ? count( reset( $value ) ) : 1;

$is_pl_lang = \get_locale() === 'pl_PL';
$buy_url    = $is_pl_lang ? 'https://wpdesk.link/dropshipping-product-export-pro-pl' : 'https://wpdesk.link/dropshipping-product-export-pro';
?>

<div class="buy-pro-version"><a target="_blank" href="<?php echo esc_url( $buy_url ); ?>"><?php echo esc_html( __( 'Upgrade to PRO &rarr;', 'dropshipping-export-free' ) ); ?> </a> <?php echo esc_html( __( 'to fully unlock this feature', 'dropshipping-export-free' ) ); ?></div>
<div id="<?php echo esc_attr( $field->get_id() ); ?>" class="flex-container odd-blue">

	<?php
	for ( $i = 0; $i < $items_nr; $i++ ) :
		?>
		<div class="wrap-condition single-condition flex-row">
			<div class="flex-col width-100">
				<div class="flex-container">
					<div class="flex-row stretch flex-fields fields-4">
						<?php
						$items = $field->get_items();
						if ( is_array( $items ) ) {
							$str = $options = ''; // phpcs:ignore

							foreach ( $items as $item ) {
								$item->set_attribute( 'disabled', 'disabled' );
								$val = ( isset( $value[ $item->get_name() ][ $i ] ) ) ? $value[ $item->get_name() ][ $i ] : '';
								if ( $item->is_multiple() ) {
									$val = explode( '|', $val );
								}
								$item->set_attribute( 'id', $name_prefix . '_' . $field->get_id() . '_' . $item->get_id() . '_' . $i );
								$template_name = ( $item->get_template_name() === 'input-text' ) ? 'input' : $item->get_template_name();

								if ( in_array( $item->get_name(), $single_value_columns, true ) ) {
									if ( ! empty( $options ) ) {
										$str    .= '<div class="flex-col">' . $options . '</div>';
										$options = '';
									}
									if ( $item->has_label() ) {
										$str .= '<div class="flex-col"><b class="nowrap">' . \esc_html( $item->get_label() ) . '</b></div>';
									}
									$rendered = $renderer->render(
										$template_name,
										[
											'field'       => $item,
											'renderer'    => $renderer,
											'name_prefix' => $name_prefix . '[' . $field->get_name() . ']',
											'value'       => $val,
											'multiple'    => true,
										]
									);
									$str     .= '<div class="flex-col">' . $rendered . '</div>';
								} else {
									$attributes = $item->get_attributes();
									$data_value = isset( $attributes['data-value'] ) ? 'data-value="' . $attributes['data-value'] . '"' : '';
									$rendered   = $renderer->render(
										$template_name,
										[
											'field'       => $item,
											'renderer'    => $renderer,
											'name_prefix' => $name_prefix . '[' . $field->get_name() . ']',
											'value'       => $val,
											'multiple'    => true,
										]
									);
									$options   .= '<div class="field-wrapper hidden" ' . $data_value . '>' . $rendered . '</div>';
								}
							}
							if ( ! empty( $options ) ) {
								$str .= '<div class="flex-col">' . $options . '</div>';
							}

							echo wp_kses(
								$str,
								[
									'div'    => $allowed_attributes,
									'input'  => $allowed_attributes,
									'select' => $allowed_attributes,
									'option' => $allowed_attributes,
									'b'      => $allowed_attributes,
									'span'   => $allowed_attributes,
								]
							);
						}
						?>
						</span>
					</div>
				</div>
			</div>
			<div class="flex-col" style="width:20px;"><a href="#" class="remove-condition <?php echo esc_html( ( $i === 0 ) ? 'hidden' : '' ); ?>"><span class="dashicons dashicons-remove"></span></a></div>
			<div class="flex-col" style="width:20px;"><a href="#" class="add-condition"><span class="dashicons dashicons-plus-alt"></span></a></div>
		</div>
		<?php
	endfor;
	?>
</div>
