<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * @var array $args
 */

use Simplyin\Simplyin_W_Plugin\Admin\Shipping_Mapping_Options;

if ( isset( $args['mapping_config'] ) ) {
	/**
	 * @var Shipping_Mapping_Options $mapping_config
	 */
	$mapping_config = $args['mapping_config'];
	/**
	 * @var array $grouped
	 */
	$grouped        = $mapping_config->get_grouped_shipping_mapping_fields();
} else {
	return;
}

/**
 * @var array $zones
 */
$zones = $grouped['zones'];

if ( empty( $zones ) ) {
	return;
}

/**
 * @var array $zone_ids
 */
$zone_ids      = array_keys( $zones );
/**
 * @var string $first_zone_id
 */
$first_zone_id = $zone_ids[0];

?>

<div class="shipping_mapping" id="simply_shipping_mapping">
	<h2>
		<?php esc_html_e( 'Shipping mapping', 'simplyin' ); ?>
	</h2>
	<p>
		<?php
		esc_html_e(
			'Map your shipping methods to your Simply.IN shipping methods.',
			'simplyin'
		)
		?>
	</p>

	<div class="simply-zone-tabs" role="tablist">
		<?php foreach ( $zones as $zone_data ) : ?>
			<?php /** @var array $zone_data */ ?>
			<button class="simply-zone-tab-btn <?php echo $zone_data['zone_id'] === $first_zone_id ? 'active' : ''; ?>"
				data-zone="<?php echo esc_attr( $zone_data['zone_id'] ); ?>"
				type="button"
				role="tab"
				aria-selected="<?php echo $zone_data['zone_id'] === $first_zone_id ? 'true' : 'false'; ?>">
			<?php echo esc_html( $zone_data['zone_name'] ); ?>
		</button>
		<?php endforeach; ?>
	</div>

	<?php foreach ( $zones as $zone_data ) : ?>
		<?php /** @var array $zone_data */ ?>
		<div class="simply-zone-tab-panel <?php echo $zone_data['zone_id'] === $first_zone_id ? 'active' : ''; ?>"
		data-zone="<?php echo esc_attr( $zone_data['zone_id'] ); ?>">

		<ul>
			<?php foreach ( $zone_data['selects'] as $select ) : ?>
				<?php /** @var \Simplyin\Simplyin_W_Plugin\Admin\Template_Components\Select $select */ ?>
				<li>
					<?php
					$select->print_label_html();
					$select->print_html();
					?>
				</li>
			<?php endforeach; ?>
		</ul>
	</div>
	<?php endforeach; ?>
</div>
