<?php

if (!defined('ABSPATH')) {
    exit;
}

$dmm          = AIEO_DMM::instance();
$subtabs      = $dmm->get_subtabs();
$tab_tree     = $dmm->get_subtab_tree($active_id);
$current_user = wp_get_current_user();
?>
<div id="aieo-wpbody-content">

    <div class="aieo-top-bar">
        <a href="https://rooster.systems/get-aieo/?utm_source=woo&utm_medium=plugin&utm_campaign=header"
           class="aieo-top-bar-logo" target="_blank">
            <img src="<?php echo esc_url(AIEO_URI); ?>assets/images/OMLogo-300.webp" />
        </a>

        <?php require AIEO_DIR . 'assets/pages/navigation-tabs.php'; ?>

        <div class="aieo-top-bar-infos">
            <div class="aieo-top-bar-info-item">
                <div class="aieo-version">
                    <a href="https://eshop-optimizer.com/changelog/?utm_source=woo&utm_medium=plugin&utm_campaign=header"
                       class="aieo-website" target="_blank"><strong>v<?php echo esc_html(AIEO_VERSION); ?></strong></a>
                </div>
            </div>
            <div class="aieo-top-bar-info-item">
                <strong>
                    <a href="https://rooster.systems/get-aieo/?utm_source=woo&utm_medium=plugin&utm_campaign=header"
                       class="aieo-website" target="_blank">
                        Rooster AI Eshop Optimizer<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" clip-rule="evenodd"
                                d="M13.6 2.40002H7.20002L8.00002 4.00002H11.264L6.39202 8.88002L7.52002 10.008L12 5.53602V8.00002L13.6 8.80002V2.40002ZM9.60002 9.60002V12H4.00002V6.40002H7.20002L8.80002 4.80002H2.40002V13.6H11.2V8.00002L9.60002 9.60002Z"
                                fill="#3858E9"></path>
                        </svg>
                    </a>
                </strong>
            </div>
        </div>
    </div>

    <div class="wrap aieo-merchant-wrap">

        <!-- Hero header (matches Analytics page) -->
        <div class="aieo-modules-header1">
            <div class="aieo-modules-header-left">
                <div class="aieo-modules-header-heading">
                    <?php echo esc_html(class_exists('AIEO_DMM') ? AIEO_DMM::get_menu_title() : __('Discounts & Marketplaces', 'ai-eshop-optimizer')); ?> 🎁
                    <br><?php esc_html_e('Pricing, discounts, gifts, loyalty and marketplace feeds — unified.', 'ai-eshop-optimizer'); ?>
                </div>
                <div class="aieo-modules-header-subheading">
                    <?php
                    printf(
                        
                        esc_html__('Welcome back %s :)', 'ai-eshop-optimizer'),
                        '<strong>' . esc_html($current_user->display_name) . '</strong>'
                    );
                    ?>
                    <div class="aieo-card-dark">
                        <?php echo wp_kses_post(__('Role-based pricing, brand discounts, free gifts, loyalty classes, free-shipping thresholds, product needs, branch stock scanner, CSV imports/exports and marketplace feeds — all in one place.', 'ai-eshop-optimizer')); ?>
                        <br><br>
                        <?php echo wp_kses_post(__('Your runtime pricing decisions use a single <strong>global recipe</strong> for how role price, brand discount, loyalty class and gifts combine — cached for speed across 35k products and 100k customers.', 'ai-eshop-optimizer')); ?>
                        <br><br>
                        <?php echo wp_kses_post(__('Admin changes here write to the same tables your frontend plugins already read, so <strong>om-role-pricing</strong> and <strong>om-free-shipping</strong> keep working unchanged.', 'ai-eshop-optimizer')); ?>
                    </div>
                </div>
            </div>
            <div class="aieo-modules-header-right">
                <ul class="aieo-modules-header-shortlinks">
                    <li>
                        <a href="#" data-aieo-nav-trigger="1" role="button">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <circle cx="11" cy="11" r="6" stroke="#787C82" stroke-width="2" fill="none"/>
                                <path d="M16 16l4 4" stroke="#787C82" stroke-width="2" stroke-linecap="round"/>
                            </svg>
                            <span><?php echo wp_kses_post(__('<strong>Quick navigator</strong> &nbsp;<kbd style="font-size:10px;background:#e5e7eb;padding:1px 5px;border-radius:3px;">⌘K</kbd>', 'ai-eshop-optimizer')); ?></span>
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M10.8626 8.04102L14.2809 12.0291L10.8626 16.0172L9.72363 15.041L12.3053 12.0291L9.72363 9.01721L10.8626 8.04102Z" fill="#757575"/>
                            </svg>
                        </a>
                    </li>
                    <li>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=ai-eshop-optimizer')); ?>">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M3 3h8v8H3V3zm10 0h8v5h-8V3zM3 13h5v8H3v-8zm7 0h11v8H10v-8zm3-10h8v2h-8V3z" fill="#787C82"/>
                            </svg>
                            <span><?php echo wp_kses_post(__('Back to <strong>Analytics</strong>', 'ai-eshop-optimizer')); ?></span>
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M10.8626 8.04102L14.2809 12.0291L10.8626 16.0172L9.72363 15.041L12.3053 12.0291L9.72363 9.01721L10.8626 8.04102Z" fill="#757575"/>
                            </svg>
                        </a>
                    </li>
                    <li>
                        <a href="<?php echo esc_url(admin_url('admin.php?page=ai-eshop-chat')); ?>">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M20 2H4C2.9 2 2 2.9 2 4V22L6 18H20C21.1 18 22 17.1 22 16V4C22 2.9 21.1 2 20 2ZM20 16H6L4 18V4H20V16Z" fill="#787C82"/>
                                <path d="M7 9H17V11H7V9ZM7 12H14V14H7V12Z" fill="#787C82"/>
                            </svg>
                            <span><?php echo wp_kses_post(__('Go to <strong>AI Chat</strong>', 'ai-eshop-optimizer')); ?></span>
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M10.8626 8.04102L14.2809 12.0291L10.8626 16.0172L9.72363 15.041L12.3053 12.0291L9.72363 9.01721L10.8626 8.04102Z" fill="#757575"/>
                            </svg>
                        </a>
                    </li>
                    <li>
                        <a href="https://eshop-optimizer.com/help-and-support/?utm_source=woo&utm_medium=plugin&utm_campaign=dmm" target="_blank">
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 17h-2v-2h2v2zm2.07-7.75l-.9.92C13.45 12.9 13 13.5 13 15h-2v-.5c0-1.1.45-2.1 1.17-2.83l1.24-1.26c.37-.36.59-.86.59-1.41 0-1.1-.9-2-2-2s-2 .9-2 2H8c0-2.21 1.79-4 4-4s4 1.79 4 4c0 .88-.36 1.68-.93 2.25z" fill="#787C82"/>
                            </svg>
                            <span><?php echo wp_kses_post(__('Get <strong>help and support</strong>', 'ai-eshop-optimizer')); ?></span>
                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                <path d="M10.8626 8.04102L14.2809 12.0291L10.8626 16.0172L9.72363 15.041L12.3053 12.0291L9.72363 9.01721L10.8626 8.04102Z" fill="#757575"/>
                            </svg>
                        </a>
                    </li>
                </ul>
            </div>
        </div>

        <!-- Secondary subtab bar -->
        <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'] === $active_id);
                    ?>
                    <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']);
                    ?>
                    <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'] === $active_id);
                                            ?>
                                            <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'] === $active_id);
                                    ?>
                                    <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>

        <!-- Module content panel -->
        <div class="aieo-modules-panel aieo-dmm-modules-panel">
            <?php if ($module instanceof AIEO_DMM_Module_Base) : ?>
                <?php $module->render(); ?>
            <?php else : ?>
                <div class="aieo-modules-box"><div class="aieo-modules-box-heading">
                    <?php esc_html_e('No DMM module selected.', 'ai-eshop-optimizer'); ?>
                </div></div>
            <?php endif; ?>
        </div>

    </div><!-- /.aieo-merchant-wrap -->

    <div id="aieo-dmm-flash" class="aieo-dmm-flash" aria-live="polite"></div>
</div><!-- /#aieo-wpbody-content -->
