<?php
/**
 * Configuration Page
 *
 * Main configuration page with left sidebar menu for Templates, Autoresponder, and Newsletter
 */

if (!defined('ABSPATH')) exit; // Exit if accessed directly

// Get current tab from URL parameter
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for tab navigation/display purposes only, value is sanitized
$instant_popup_builder_current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'templates';

// Define available tabs
$tabs = [
    'templates' => [
        'title' => 'Templates',
        'icon' => 'dashicons-admin-page',
        'description' => 'Manage email templates and layouts'
    ],
    'autoresponder' => [
        'title' => 'Auto-Responder',
        'icon' => 'dashicons-email-alt',
        'description' => 'Configure automated email responses'
    ],
    'newsletter' => [
        'title' => 'Newsletter',
        'icon' => 'dashicons-megaphone',
        'description' => 'Manage newsletter settings and campaigns'
    ],
    'analytics' => [
        'title' => 'Performance Analytics',
        'icon' => 'dashicons-chart-bar',
        'description' => 'View detailed analytics and performance metrics'
    ]
];
?>

<div class="wrap instant-popup-configuration">
    <h1 class="wp-heading-inline">Configuration</h1>
    <p class="description">Configure templates, autoresponder, and newsletter settings for your popup subscription system.</p>

    <div class="instant-config-container">
        <!-- Left Sidebar Menu -->
        <div class="instant-config-sidebar">
            <nav class="instant-config-nav">
                <ul class="config-menu">
                    <?php foreach ($tabs as $instant_popup_builder_tab_key => $instant_popup_builder_tab_data): ?>
                        <li class="config-menu-item <?php echo $instant_popup_builder_current_tab === $instant_popup_builder_tab_key ? 'active' : ''; ?>">
                            <a href="<?php echo esc_url(admin_url('admin.php?page=instant-popup-configuration&tab=' . $instant_popup_builder_tab_key)); ?>"
                               class="config-menu-link">
                                <span class="dashicons <?php echo esc_attr($instant_popup_builder_tab_data['icon']); ?>"></span>
                                <span class="menu-text"><?php echo esc_html($instant_popup_builder_tab_data['title']); ?></span>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </nav>
        </div>

        <!-- Main Content Area -->
        <div class="instant-config-content">
            <div class="config-content-body">
                <?php
                // Load the appropriate tab content
                switch ($instant_popup_builder_current_tab) {
                    case 'templates':
                        include_once plugin_dir_path(__FILE__) . '/config-tabs/templates-tab.php';
                        break;
                    case 'autoresponder':
                        include_once plugin_dir_path(__FILE__) . '/config-tabs/autoresponder-tab.php';
                        break;
                    case 'newsletter':
                        include_once plugin_dir_path(__FILE__) . '/config-tabs/newsletter-tab.php';
                        break;
                    case 'analytics':
                        include_once plugin_dir_path(__FILE__) . '/config-tabs/analytics-tab.php';
                        break;
                    default:
                        include_once plugin_dir_path(__FILE__) . '/config-tabs/templates-tab.php';
                        break;
                }
                ?>
            </div>
        </div>
    </div>
</div>

<style>
/* Configuration Page Styles */
.instant-popup-configuration {
    margin: 20px 20px 0 2px;
}

.instant-config-container {
    display: flex;
    background: #fff;
    border: 1px solid #ccd0d4;
    border-radius: 4px;
    margin-top: 20px;
    min-height: 600px;
}

/* Sidebar Styles */
.instant-config-sidebar {
    width: 250px;
    background: #f8f9fa;
    border-right: 1px solid #e1e1e1;
    flex-shrink: 0;
}

.instant-config-nav {
    padding: 0;
}

.config-menu {
    margin: 0;
    padding: 0;
    list-style: none;
}

.config-menu-item {
    border-bottom: 1px solid #e1e1e1;
}

.config-menu-item:last-child {
    border-bottom: none;
}

.config-menu-link {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    color: #555;
    text-decoration: none;
    transition: all 0.2s ease;
    border-left: 3px solid transparent;
}

.config-menu-link:hover {
    background: #e9ecef;
    color: #0073aa;
}

.config-menu-link:focus {
    box-shadow: none;
    outline: none;
}

.config-menu-item.active .config-menu-link {
    background: #fff;
    color: #0073aa;
    border-left-color: #0073aa;
    font-weight: 600;
}

.config-menu-link .dashicons {
    margin-right: 10px;
    font-size: 18px;
    width: 18px;
    height: 18px;
}

.menu-text {
    font-size: 14px;
}

/* Content Area Styles */
.instant-config-content {
    flex: 1;
    padding: 0;
}

.config-content-header {
    padding: 25px 30px 20px;
    border-bottom: 1px solid #e1e1e1;
    background: #fafafa;
}

.config-section-title {
    margin: 0 0 8px 0;
    font-size: 24px;
    font-weight: 600;
    color: #23282d;
    display: flex;
    align-items: center;
}

.config-section-title .dashicons {
    margin-right: 10px;
    color: #0073aa;
}

.config-section-description {
    margin: 0;
    color: #666;
    font-size: 14px;
}

.config-content-body {
    padding: 30px;
}

/* Responsive Design */
@media (max-width: 1024px) {
    .instant-config-container {
        flex-direction: column;
    }

    .instant-config-sidebar {
        width: 100%;
        border-right: none;
        border-bottom: 1px solid #e1e1e1;
    }

    .config-menu {
        display: flex;
        overflow-x: auto;
    }

    .config-menu-item {
        border-bottom: none;
        border-right: 1px solid #e1e1e1;
        flex-shrink: 0;
    }

    .config-menu-item:last-child {
        border-right: none;
    }

    .config-menu-link {
        white-space: nowrap;
        border-left: none;
        border-bottom: 3px solid transparent;
    }

    .config-menu-item.active .config-menu-link {
        border-left: none;
        border-bottom-color: #0073aa;
    }
}

@media (max-width: 768px) {
    .instant-popup-configuration {
        margin: 20px 10px 0 2px;
    }

    .config-content-header {
        padding: 20px;
    }

    .config-content-body {
        padding: 20px;
    }

    .config-section-title {
        font-size: 20px;
    }

    .config-menu-link {
        padding: 12px 15px;
    }

    .config-menu-link .dashicons {
        margin-right: 8px;
        font-size: 16px;
        width: 16px;
        height: 16px;
    }

    .menu-text {
        font-size: 13px;
    }
}
</style>