<?php
$current_page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : 'easy-invoice';

// Function to check if current page is a subpage of a main menu
function is_subpage_of($current_page, $main_page) {
    // Check if current page starts with the main page slug
    $current_page = $current_page ?? '';
    $main_page = $main_page ?? '';
    return strpos($current_page, $main_page) === 0;
}

// Function to get the active menu class
function get_menu_class($current_page, $menu_page, $is_active = false) {
    if ($is_active) {
        return 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600';
    }
    return 'text-gray-600 hover:bg-gray-50 hover:text-gray-900';
}

// Function to get the active icon class
function get_icon_class($current_page, $menu_page, $is_active = false) {
    if ($is_active) {
        return 'text-indigo-600';
    }
    return 'text-gray-400 group-hover:text-gray-500';
}

// Determine which menu should be active
$is_dashboard_active = $current_page === 'easy-invoice';
$is_invoices_active = in_array($current_page, [
    'easy-invoice-all',           // All invoices
    'easy-invoice-builder',       // Invoice builder
    'easy-invoice-preview'        // Invoice preview
]);
$is_quotes_active = in_array($current_page, [
    'easy-quote-all',             // All quotes
    'easy-invoice-quote-builder',         // Quote builder
    'easy-quote-preview'          // Quote preview
]);
$is_payments_active = in_array($current_page, [
    'easy-invoice-payments',      // All payments
    'easy-invoice-payment-new'    // Add new payment
]);
$is_clients_active = in_array($current_page, [
    'easy-invoice-clients',       // All clients
    'easy-invoice-client-edit',   // Edit client
    'easy-invoice-client-view'    // View client
]);
$is_item_library_active = in_array($current_page, [
    'easy-invoice-pro-item-library',  // Item Library (Pro)
    'easy-invoice-item-library'       // Item Library (alternative)
]);
$is_templates_active = in_array($current_page, [
    'easy-invoice-templates',         // Template Builder (Pro)
    'easy-invoice-template-builder'   // Template Builder page
]);
$is_reports_active = $current_page === 'easy-invoice-reports';
$is_settings_active = in_array($current_page, [
    'easy-invoice-settings',      // Main settings
    'easy-invoice-email-settings', // Email settings
    'easy-invoice-email-settings-general',
    'easy-invoice-email-settings-invoice',
    'easy-invoice-email-settings-quote',
    'easy-invoice-email-settings-payment',
    'easy-invoice-email-settings-payment-reminder',
    'easy-invoice-pro-settings',  // Pro settings
    'easy-invoice-pro-translations' // Pro translations
]);
?>

<!-- Sidebar -->
<div class="fixed inset-y-0 left-0 w-64 bg-white shadow-lg border-r border-gray-200">
    <!-- Logo and Back Button -->
    <div class="ei-app-sidebar-top flex flex-col items-center justify-center px-4 py-2 border-b border-gray-200 box-border">
        <div class="flex items-center space-x-2 mb-1">
            <div class="flex items-center justify-center w-8 h-8 bg-indigo-600 rounded">
                <!-- Invoice Logo SVG -->
                <svg class="h-5 w-5 text-white" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M16 13H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M16 17H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M10 9H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            </div>
            <span class="text-lg font-semibold text-gray-900"><?php esc_html_e('Easy Invoice', 'easy-invoice'); ?></span>
        </div>
        <a href="<?php echo admin_url(); ?>" target="_blank" class="inline-flex items-center text-xs text-gray-500 hover:text-gray-700 transition-colors duration-200">
            <svg class="w-3 h-3 mr-1" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M19 12H5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 19L5 12L12 5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Back to WordPress', 'easy-invoice' ); ?>
        </a>

        <!-- Version Information -->
        <div class="mt-1 flex flex-wrap items-center justify-center gap-1.5">
            <!-- Free Version Badge -->
            <span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-700 border border-gray-200">
                <svg class="w-2.5 h-2.5 mr-1 text-gray-500" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
                v<?php echo esc_html(EASY_INVOICE_VERSION); ?>
            </span>

            <?php if (defined('EASY_INVOICE_PRO_VERSION') && function_exists('is_plugin_active') && is_plugin_active('easy-invoice-pro/easy-invoice-pro.php')): ?>
                <!-- Pro Version Badge -->
                <span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-700 border border-green-200">
                    <svg class="w-2.5 h-2.5 mr-1 text-green-500" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                        <path d="M9 12L11 14L15 10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                        <path d="M21 12C21 16.9706 16.9706 21 12 21C7.02944 21 3 16.9706 3 12C3 7.02944 7.02944 3 12 3C16.9706 3 21 7.02944 21 12Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    </svg>
                    <?php echo esc_html( sprintf( __( 'Pro v%s', 'easy-invoice' ), EASY_INVOICE_PRO_VERSION ) ); ?>
                </span>
            <?php endif; ?>
        </div>
    </div>

    <!-- Navigation Links -->
    <nav class="mt-6 px-3 space-y-2">
        <!-- Dashboard -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice'); ?>"
           class="<?php echo $is_dashboard_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_dashboard_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M3 13H9V19H3V13Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 13H15V19H9V13Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M15 13H21V19H15V13Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M3 5H9V11H3V5Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 5H15V11H9V5Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M15 5H21V11H15V5Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Dashboard', 'easy-invoice' ); ?>
        </a>

        <!-- Invoices -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-all'); ?>"
           class="<?php echo $is_invoices_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_invoices_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M16 13H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M16 17H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M10 9H8" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Invoices', 'easy-invoice' ); ?>
        </a>

        <!-- Quotes -->
        <a href="<?php echo admin_url('admin.php?page=easy-quote-all'); ?>"
           class="<?php echo $is_quotes_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_quotes_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M14 2H6C4.9 2 4 2.9 4 4V20C4 21.1 4.9 22 6 22H18C19.1 22 20 21.1 20 20V8L14 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M14 2V8H20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 9H15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 13H15" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 17H13" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Quotes', 'easy-invoice' ); ?>
        </a>

        <!-- Payments -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-payments'); ?>"
           class="<?php echo $is_payments_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_payments_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <rect x="1" y="4" width="22" height="16" rx="2" ry="2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <line x1="1" y1="10" x2="23" y2="10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Payments', 'easy-invoice' ); ?>
        </a>

        <!-- Clients -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-clients'); ?>"
           class="<?php echo $is_clients_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_clients_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M17 21V19C17 17.9391 16.5786 16.9217 15.8284 16.1716C15.0783 15.4214 14.0609 15 13 15H5C3.93913 15 2.92172 15.4214 2.17157 16.1716C1.42143 16.9217 1 17.9391 1 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <circle cx="9" cy="7" r="4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M23 21V19C23 18.1137 22.6485 17.2628 22.0237 16.6425C21.3989 16.0221 20.5471 15.6722 19.66 15.6722C18.7729 15.6722 17.9211 16.0221 17.2963 16.6425C16.6715 17.2628 16.32 18.1137 16.32 19V21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M16.32 11.6722C18.36 11.6722 20 10.0322 20 7.99219C20 5.95219 18.36 4.31219 16.32 4.31219" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Clients', 'easy-invoice' ); ?>
        </a>

        <?php if (easy_invoice_has_pro()): ?>
        <!-- Item Library -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-pro-item-library'); ?>"
           class="<?php echo $is_item_library_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_item_library_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M20 7H4C2.9 7 2 7.9 2 9V19C2 20.1 2.9 21 4 21H20C21.1 21 22 20.1 22 19V9C22 7.9 21.1 7 20 7Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M16 3H8C7.4 3 7 3.4 7 4V7H17V4C17 3.4 16.6 3 16 3Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M8 11H16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M8 15H13" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Item Library', 'easy-invoice' ); ?>
        </a>
        
        <!-- Template Builder -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-templates'); ?>"
           class="<?php echo $is_templates_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_templates_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M4 4h7v2H9.5L7.5 7.5H4V4zm7 4h7v2h2.5L17.5 7.5H11V4zm7 4h7v2h2.5L27.5 7.5H18V4z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M4 11h7v2H9.5L7.5 14.5H4V11zm7 0h7v2h2.5L17.5 14.5H11V11zm7 0h7v2h2.5L27.5 14.5H18V11z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M4 18h7v2H9.5L7.5 21.5H4V18zm7 0h7v2h2.5L17.5 21.5H11V18zm7 0h7v2h2.5L27.5 21.5H18V18z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Template Builder', 'easy-invoice' ); ?>
        </a>
        <?php endif; ?>

        <!-- Reports -->
        <?php if (!easy_invoice_has_pro()): ?>
        <a href="#" id="reports-menu-link"
           class="text-gray-600 hover:bg-gray-50 hover:text-gray-900 group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="text-gray-400 group-hover:text-gray-500 mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M18 20V10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 20V4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M6 20V14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Reports', 'easy-invoice' ); ?>
            <span class="ml-auto inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-purple-600 text-white shadow-sm">
                <svg class="w-4 h-4 mr-1 text-white" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 8L15 13.2L18 10.5L17.3 14H6.7L6 10.5L9 13.2L12 8M12 4L8.5 10L3 5L5 16H19L21 5L15.5 10L12 4Z"/>
                </svg>
                <?php esc_html_e( 'Pro', 'easy-invoice' ); ?>
            </span>
        </a>
        <?php else: ?>
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-reports'); ?>"
           class="<?php echo $is_reports_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_reports_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M18 20V10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M12 20V4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M6 20V14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Reports', 'easy-invoice' ); ?>
        </a>
        <?php endif; ?>

        <!-- Settings -->
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-settings'); ?>"
           class="<?php echo $is_settings_active ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo $is_settings_active ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <circle cx="12" cy="12" r="3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M19.4 15A1.65 1.65 0 0 0 18 14A6 6 0 0 0 6 14A1.65 1.65 0 0 0 4.6 15A2 2 0 0 0 6 19H18A2 2 0 0 0 19.4 15Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <?php esc_html_e( 'Settings', 'easy-invoice' ); ?>
        </a>

        <!-- License -->
        <?php
        // Check if Pro plugin is active and get license status
        $show_license_badge = false;
        $license_status = 'inactive';
        $license_text = __( 'Deactivated', 'easy-invoice' );

        if (easy_invoice_has_pro()) {
            $license_key = \EasyInvoicePro\Updater\License::get_license_key();
            $is_valid = \EasyInvoicePro\Updater\License::has_valid_license();
            $is_expired = \EasyInvoicePro\Updater\License::has_license_expired();

            // Always show badge for Pro version
            $show_license_badge = true;

            if (!empty($license_key)) {
                if ($is_valid && !$is_expired) {
                    $license_status = 'activated';
                    $license_text = __( 'Activated', 'easy-invoice' );
                } elseif ($is_expired) {
                    $license_status = 'expired';
                    $license_text = __( 'Expired', 'easy-invoice' );
                } else {
                    $license_status = 'inactive';
                    $license_text = __( 'Deactivated', 'easy-invoice' );
                }
            } else {
                // No license key entered
                $license_status = 'inactive';
                $license_text = __( 'Inactive', 'easy-invoice' );
            }
        } else {
            // Free version - show Free badge
            $show_license_badge = true;
            $license_status = 'free';
            $license_text = __( 'Free', 'easy-invoice' );
        }
        ?>
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-license'); ?>"
           class="<?php echo ($current_page === 'easy-invoice-license') ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo ($current_page === 'easy-invoice-license') ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2L13.09 8.26L20 9L13.09 9.74L12 16L10.91 9.74L4 9L10.91 8.26L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <span class="flex-1"><?php esc_html_e( 'License', 'easy-invoice' ); ?></span>
            <?php if ($show_license_badge): ?>
            <span class="license-status-badge <?php echo $license_status; ?>">
                <span class="status-dot"></span>
                <?php echo esc_html( $license_text ); ?>
            </span>
            <?php endif; ?>
        </a>

        <!-- Free vs Pro - only show in free version -->
        <?php
        $show_free_vs_pro = !easy_invoice_has_pro();

        if ($show_free_vs_pro):
        ?>
        <a href="<?php echo admin_url('admin.php?page=easy-invoice-free-vs-pro'); ?>"
           class="<?php echo ($current_page === 'easy-invoice-free-vs-pro') ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo ($current_page === 'easy-invoice-free-vs-pro') ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M3 6L21 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M3 12L21 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M3 18L21 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M9 3L9 21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M15 3L15 21" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <span class="flex-1"><?php esc_html_e('Free vs Pro', 'easy-invoice'); ?></span>
            <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-purple-100 text-purple-800">
                <?php esc_html_e('Compare', 'easy-invoice'); ?>
            </span>
        </a>
        <?php endif; ?>

        <!-- Join Community -->
        <a href="<?php echo esc_url('https://www.facebook.com/groups/mantrabraincommunity'); ?>"
           target="_blank"
           rel="noopener noreferrer"
           class="<?php echo ($current_page === 'easy-invoice-join-community') ? 'bg-indigo-50 text-indigo-700 border-r-2 border-indigo-600' : 'text-gray-600 hover:bg-gray-50 hover:text-gray-900'; ?> group flex items-center px-4 py-3 text-sm font-medium rounded-lg transition-all duration-200">
            <svg class="<?php echo ($current_page === 'easy-invoice-join-community') ? 'text-indigo-600' : 'text-gray-400 group-hover:text-gray-500'; ?> mr-3 flex-shrink-0 h-5 w-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M18 2H15C13.6739 2 12.4021 2.52678 11.4645 3.46447C10.5268 4.40215 10 5.67392 10 7V10H7V14H10V22H14V14H17L18 10H14V7C14 6.73478 14.1054 6.48043 14.2929 6.29289C14.4804 6.10536 14.7348 6 15 6H18V2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            <span class="flex-1"><?php esc_html_e('Join Community', 'easy-invoice'); ?></span>
            <svg class="flex-shrink-0 h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 6H6a2 2 0 00-2 2v10a2 2 0 002 2h10a2 2 0 002-2v-4M14 4h6m0 0v6m0-6L10 14"/>
            </svg>
        </a>

    </nav>

    <!-- User Profile -->
    <div class="absolute bottom-0 w-full border-t border-gray-200 p-4">
        <div class="relative">
            <button type="button" id="user-dropdown-button" class="flex items-center w-full text-left hover:bg-gray-50 rounded-lg p-2 transition-colors duration-200">
                <div class="flex-shrink-0">
                    <div class="h-8 w-8 rounded-full bg-gray-200 flex items-center justify-center">
                        <span class="text-gray-600 font-medium text-sm"><?php echo substr(wp_get_current_user()->display_name, 0, 1); ?></span>
                    </div>
                </div>
                <div class="ml-3 flex-1">
                    <p class="text-sm font-medium text-gray-700"><?php echo esc_html(wp_get_current_user()->display_name); ?></p>
                    <p class="text-xs text-gray-500"><?php echo esc_html(wp_get_current_user()->user_email); ?></p>
                </div>
                <div class="flex-shrink-0">
                    <svg class="h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path>
                    </svg>
                </div>
            </button>

            <!-- Dropdown Menu -->
            <div id="user-dropdown-menu" class="hidden absolute bottom-full left-0 right-0 mb-2 bg-white border border-gray-200 rounded-lg shadow-lg z-50">
                <div class="py-1">
                    <a href="<?php echo admin_url(); ?>" target="_blank" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors duration-200">
                        <svg class="mr-3 h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18"></path>
                        </svg>
                        <?php _e('Back to WordPress', 'easy-invoice'); ?>
                    </a>
                    <a href="<?php echo wp_logout_url(); ?>" class="flex items-center px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors duration-200">
                        <svg class="mr-3 h-4 w-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"></path>
                        </svg>
                        <?php _e('Logout', 'easy-invoice'); ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Mobile menu button -->
<div class="fixed top-0 left-0 z-50 p-4 sm:hidden">
    <button type="button" class="inline-flex items-center justify-center p-2 rounded-md text-gray-400 hover:text-gray-500 hover:bg-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-indigo-500" aria-expanded="false" aria-label="<?php echo esc_attr__( 'Open menu', 'easy-invoice' ); ?>">
        <svg class="h-6 w-6" fill="none" stroke="currentColor" viewBox="0 0 24 24" aria-hidden="true">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path>
        </svg>
    </button>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const mobileMenuButton = document.querySelector('button[aria-expanded="false"]');
    const sidebar = document.querySelector('.fixed.inset-y-0.left-0');

    if (mobileMenuButton && sidebar) {
        mobileMenuButton.addEventListener('click', function() {
            const expanded = this.getAttribute('aria-expanded') === 'true';
            this.setAttribute('aria-expanded', !expanded);
            sidebar.classList.toggle('hidden');
        });
    }

    // Handle Reports menu click for premium feature
    const reportsMenuLink = document.getElementById('reports-menu-link');
    if (reportsMenuLink) {
        reportsMenuLink.addEventListener('click', function(e) {
            e.preventDefault();
            // Show premium popup using the proper confirmation modal
            if (typeof EasyInvoiceConfirmation !== 'undefined') {
                EasyInvoiceConfirmation.showFeatureUpgrade(
                    <?php echo wp_json_encode( __( 'Detailed Reports', 'easy-invoice' ) ); ?>,
                    <?php echo wp_json_encode( __( 'Get comprehensive insights into your business with detailed reports including revenue analysis, payment statistics, invoice status tracking, and monthly revenue charts. Upgrade to Easy Invoice Pro to unlock advanced reporting features.', 'easy-invoice' ) ); ?>
                );
            } else {
                // Fallback to alert if confirmation modal is not available
                alert('<?php echo esc_js(__('Detailed Reports is a premium feature. Upgrade to Easy Invoice Pro to unlock advanced reporting features including revenue analysis, payment statistics, and monthly revenue charts.', 'easy-invoice')); ?>');
            }
        });
    }

    // Handle user dropdown
    const userDropdownButton = document.getElementById('user-dropdown-button');
    const userDropdownMenu = document.getElementById('user-dropdown-menu');

    if (userDropdownButton && userDropdownMenu) {
        userDropdownButton.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();
            userDropdownMenu.classList.toggle('hidden');
        });

        // Close dropdown when clicking outside
        document.addEventListener('click', function(e) {
            if (!userDropdownButton.contains(e.target) && !userDropdownMenu.contains(e.target)) {
                userDropdownMenu.classList.add('hidden');
            }
        });

        // Close dropdown when pressing Escape key
        document.addEventListener('keydown', function(e) {
            if (e.key === 'Escape') {
                userDropdownMenu.classList.add('hidden');
            }
        });
    }
});
</script>

<style>
html.wp-toolbar {
    padding-top: 0 !important;
}
/* Matches hideAdminUi: hidden admin bar must not reserve body padding or main content shifts vs fixed sidebar */
body.easy-invoice-admin-body.admin-bar {
    padding-top: 0 !important;
}
body.easy-invoice-admin-body.admin-bar #wpbody,
body.easy-invoice-admin-body.admin-bar #wpbody-content {
    padding-top: 0 !important;
}

/* Custom color for active icons */
.group:hover svg {
    color: rgb(79 70 229) !important;
}

/* Active state icon color */
.bg-indigo-50 svg {
    color: rgb(79 70 229) !important;
}

/* Hover state for non-active items */
.group:hover:not(.bg-indigo-50) svg {
    color: rgb(79 70 229) !important;
}
</style>

<div class="min-h-screen bg-gray-50 easy-invoice-admin">
    <a href="#easy-invoice-main" class="easy-invoice-skip-link screen-reader-text"><?php esc_html_e( 'Skip to main content', 'easy-invoice' ); ?></a>
    <!-- Main Content -->
    <div id="easy-invoice-main" class="pl-64" tabindex="-1">
        <?php

        $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : 'easy-invoice';
        // Content will be inserted here
        do_action('easy_invoice_admin_before_main_content', $page);

        do_action('easy_invoice_admin_main_content', $page);

        do_action('easy_invoice_admin_after_main_content', $page);

    ?>
    </div>
    
    <!-- Add ajaxurl for custom notice JavaScript -->
    <script>
    if (typeof ajaxurl === 'undefined') {
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    }
    </script>
</div>
