<?php

$wrapper_attributes = get_block_wrapper_attributes(['class' => 'tab-group']);
$inner_blocks = $block->parsed_block['innerBlocks'] ?? [];

// Extraire les titres et icônes des blocs tab pour les boutons
$tabs = array_map(function ($inner_block) {
	return [
		'title' => $inner_block['attrs']['title'] ?? __('Sans titre', 'tab-group'),
		'iconUrl' => $inner_block['attrs']['iconUrl'] ?? '',
	];
}, array_filter($inner_blocks, fn($b) => ($b['blockName'] ?? '') === 'carhop-blocks/tab'));



?>





<section <?php echo $wrapper_attributes; ?>>
	<div class="tab-group__toolbar">
		<div role="tablist" aria-label="<?php esc_attr_e('Onglets', 'tab-group'); ?>" class="tablist">
			<?php foreach ($tabs as $index => $tab) : ?>
				<button
					type="button"
					role="tab"
					aria-selected="<?php echo $index === 0 ? 'true' : 'false'; ?>"
					aria-controls="tabpanel-<?php echo esc_attr($index + 1); ?>"
					tabindex="<?php echo $index === 0 ? '0' : '-1'; ?>"
					data-tab="<?php echo esc_attr($index); ?>">
					<?php if (!empty($tab['iconUrl'])) : ?>
						<img src="<?php echo esc_url($tab['iconUrl']); ?>" alt="" class="tab__icon" aria-hidden />
					<?php endif; ?>
					<span><?php echo esc_html($tab['title']); ?></span>
				</button>
			<?php endforeach; ?>
		</div>
	</div>
	<div class="tab-group__innerblocks">
		<?php foreach ($inner_blocks as $index => $inner_block) : ?>
			<div class="wp-block-carhop-blocks-tab tab" id="tabpanel-<?php echo esc_attr($index + 1); ?>" role="tabpanel" data-active="<?php echo $index === 0 ? 'true' : 'false'; ?>">
				<?php echo render_block($inner_block); ?>
			</div>
		<?php endforeach; ?>
	</div>
</section>