<?php

if (!defined('ABSPATH')) { exit; }
if (!class_exists('AIEO_DMM')) { return; }

$aieo_menu_active = isset($aieo_menu_active) ? (string) $aieo_menu_active : '';

$aieo_menu_active_group = isset($aieo_menu_active_group) ? (string) $aieo_menu_active_group : '';
$dmm      = AIEO_DMM::instance();
$tab_tree = $dmm->get_subtab_tree($aieo_menu_active);
?>
<div class="aieo-dmm-subtab-bar">
    <?php foreach ($tab_tree as $entry) :
        if ($entry['kind'] === 'tab') :
            $url       = $dmm->get_subtab_url($entry['id']);
            $is_active = ($entry['id'] === $aieo_menu_active);
            ?>
            <a class="aieo-dmm-subtab<?php echo $is_active ? ' is-active' : ''; ?>" href="<?php echo esc_url($url); ?>">
                <?php echo esc_html($entry['label']); ?>
            </a>
        <?php else :
            $is_active = !empty($entry['has_active'])
                || ($aieo_menu_active_group !== '' && $entry['id'] === $aieo_menu_active_group);
            ?>
            <div class="aieo-dmm-subtab-group<?php echo $is_active ? ' has-active' : ''; ?>">
                <button type="button" class="aieo-dmm-subtab aieo-dmm-subtab--group<?php echo $is_active ? ' is-active' : ''; ?>" aria-haspopup="true" aria-expanded="false">
                    <?php echo wp_kses($entry['label'], array()); ?>
                    <svg class="aieo-dmm-caret" width="10" height="10" viewBox="0 0 10 10" aria-hidden="true">
                        <path d="M2 4l3 3 3-3" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                </button>
                <div class="aieo-dmm-subtab-menu<?php echo !empty($entry['sections']) ? ' has-sections' : ''; ?>" role="menu">
                    <?php if (!empty($entry['sections'])) :
                        foreach ($entry['sections'] as $section) : ?>
                            <div class="aieo-dmm-subtab-menu-column" data-section-id="<?php echo esc_attr($section['id']); ?>" role="presentation">
                                <?php if ($section['label'] !== '') : ?>
                                    <div class="aieo-dmm-subtab-menu-section" data-section-id="<?php echo esc_attr($section['id']); ?>" role="presentation"><?php echo esc_html($section['label']); ?></div>
                                <?php endif;
                                foreach ($section['children'] as $child) :
                                    $is_link = isset($child['kind']) && 'link' === $child['kind'];
                                    $curl    = $is_link ? $child['url'] : $dmm->get_subtab_url($child['id']);
                                    $cactive = !$is_link && ($child['id'] === $aieo_menu_active);
                                    ?>
                                    <a class="aieo-dmm-subtab-menu-item<?php echo $cactive ? ' is-active' : ''; ?>" href="<?php echo esc_url($curl); ?>" role="menuitem"><?php echo esc_html($child['label']); ?></a>
                                <?php endforeach; ?>
                            </div>
                        <?php endforeach;
                    else :
                        foreach ($entry['children'] as $child) :
                            $is_link = isset($child['kind']) && 'link' === $child['kind'];
                            $curl    = $is_link ? $child['url'] : $dmm->get_subtab_url($child['id']);
                            $cactive = !$is_link && ($child['id'] === $aieo_menu_active);
                            ?>
                            <a class="aieo-dmm-subtab-menu-item<?php echo $cactive ? ' is-active' : ''; ?>" href="<?php echo esc_url($curl); ?>" role="menuitem"><?php echo esc_html($child['label']); ?></a>
                        <?php endforeach;
                    endif; ?>
                </div>
            </div>
        <?php endif;
    endforeach; ?>
</div>
