<?php
/**
 * ROI Theme - Panel de Administración Principal
 *
 * @var AdminDashboardRenderer $this
 */

declare(strict_types=1);

// Prevenir acceso directo
if (!defined('ABSPATH')) {
    exit;
}

$components = $this->getComponents();

// Determinar tab activo: desde URL o primer componente
$activeComponentId = array_key_first($components);

// Leer parametro admin-tab de la URL con sanitizacion
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Solo lectura de parametro para UI
if (isset($_GET['admin-tab'])) {
    $requestedTab = sanitize_text_field(wp_unslash($_GET['admin-tab']));
    // Validar que el componente exista
    if (array_key_exists($requestedTab, $components)) {
        $activeComponentId = $requestedTab;
    }
}
?>

<div class="wrap roi-admin-panel">
    <!-- Navigation Tabs -->
    <ul class="nav nav-tabs nav-tabs-admin mb-0" role="tablist">
        <?php foreach ($components as $componentId => $component): ?>
            <li class="nav-item" role="presentation">
                <button class="nav-link <?php echo $componentId === $activeComponentId ? 'active' : ''; ?>"
                        data-bs-toggle="tab"
                        data-bs-target="#<?php echo esc_attr($componentId); ?>Tab"
                        type="button"
                        role="tab"
                        aria-controls="<?php echo esc_attr($componentId); ?>Tab"
                        aria-selected="<?php echo $componentId === $activeComponentId ? 'true' : 'false'; ?>">
                    <i class="bi <?php echo esc_attr($component['icon']); ?> me-1"></i>
                    <?php echo esc_html($component['label']); ?>
                </button>
            </li>
        <?php endforeach; ?>
    </ul>

    <!-- Tab Content -->
    <div class="tab-content mt-3">
        <?php foreach ($components as $componentId => $component):
            $isActive = ($componentId === $activeComponentId);
            $componentSettings = $this->getComponentSettings($componentId);
        ?>
            <!-- Tab: <?php echo esc_html($component['label']); ?> -->
            <div class="tab-pane fade <?php echo $isActive ? 'show active' : ''; ?>"
                 id="<?php echo esc_attr($componentId); ?>Tab"
                 role="tabpanel">

                <?php
                // Renderizar FormBuilder del componente
                $formBuilderClass = $this->getFormBuilderClass($componentId);
                if (class_exists($formBuilderClass)) {
                    $formBuilder = new $formBuilderClass($this);
                    echo $formBuilder->buildForm($componentId);
                } else {
                    echo '<p class="text-danger">FormBuilder no encontrado: ' . esc_html($formBuilderClass) . '</p>';
                }
                ?>

            </div>
        <?php endforeach; ?>
    </div>

    <!-- Botones Globales Save/Cancel -->
    <div class="d-flex justify-content-end gap-2 p-3 rounded border mt-4" style="background-color: #f8f9fa; border-color: #e9ecef !important;">
        <button type="button" class="btn btn-outline-secondary" id="cancelChanges">
            <i class="bi bi-x-circle me-1"></i>
            Cancelar
        </button>
        <button type="button" id="saveSettings" class="btn fw-semibold text-white" style="background-color: #FF8600; border-color: #FF8600;">
            <i class="bi bi-check-circle me-1"></i>
            Guardar Cambios
        </button>
    </div>

</div><!-- /wrap -->
