<?php
/**
 * Admin sync settings page data preview.
 *
 * @package northmill-online-shared
 */

declare(strict_types=1);

/**
 * View parameters.
 *
 * @var array $params
 */

use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Color_Model;
use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Featured_Group_Model;
use Northmill_Online_Shared\Modules\Admin\Pages\Page\Admin_Sync_Settings\Models\Properties\Settings_Image_Model;

defined( 'ABSPATH' ) || exit;

if ( ! is_null( $params['data_preview'] ) ) {
	foreach ( $params['data_preview'] as $preview_table ) {
		?>
		<table class="widefat sync-settings-table">
			<thead>
			<tr>
				<th colspan="3">
					<h2 class="sync-settings-table__heading"><?php echo esc_html( $preview_table['label'] ); ?></h2>
				</th>
			</tr>
			</thead>
			<tbody>
			<?php
			foreach ( $preview_table['values'] as $v ) {
				$val  = $v['value'];
				$mark = $v['mark'] ?? '';
				?>
				<tr>
					<td>
						<?php
						echo esc_html( $v['label'] );
						if ( isset( $v['usage'] ) ) {
							echo '<br /><span class="usage">' . wp_kses_post( $v['usage'] ) . '</span>';
						}
						?>
					</td>
					<td class="help"></td>
					<td>
						<?php
						if ( $mark ) {
							echo '<mark class="' . esc_attr( $mark ) . '">'; }
						if ( 'yes' === $mark ) {
							echo '<span class="dashicons dashicons-yes"></span>'; }
						if ( 'no' === $mark ) {
							echo '<span class="dashicons dashicons-no"></span>'; }
						if ( is_a( $val, Settings_Color_Model::class ) ) {
							echo ( $val->get_color() )
								? '<input type="color" value="' . esc_attr( $val->get_color() ) . '" disabled /> ' . esc_html( $val->get_color() )
								: esc_html__( 'None', 'northmill-online-shared' );
						} elseif ( is_array( $val ) ) {
							if ( empty( $val ) ) {
								esc_html_e( 'None', 'northmill-online-shared' );
							} else {
								$items = array();
								foreach ( $val as $item ) {
									if ( is_a( $item, Settings_Image_Model::class ) ) {
										$items[] = '<img src="' . esc_attr( $item->get_url() ) . '" /><br />' . esc_html( $item->get_name() );
									} elseif ( is_a( $item, Settings_Featured_Group_Model::class ) ) {
										$o = '';
										if ( ! empty( $item->get_group_images() ) ) {
											foreach ( $item->get_group_images() as $image ) {
												$o .= '<br /><img src="' . esc_attr( $image->get_url() ) . '" /><br />';
											}
										}
										$items[] = $o . $item->get_group_name() . ' (' . $item->get_group_id() . ')';
									} else {
										$items[] = $item;
									}
								}
								echo wp_kses_post( implode( ', ', $items ) );
							}
						} elseif ( 'ShippingCost' === $v['name'] ) {
							echo wp_kses_post( wc_price( $val ) );
						} else {
							echo nl2br( esc_html( $val ) );
						}
						if ( $mark ) {
							echo '</mark>'; }
						?>
					</td>
				</tr>
				<?php
			}
			?>
			</tbody>
		</table>
		<?php
	}
}
