<?php
/**
 * VMPFence Login Security View
 * Two-Factor Authentication Management with Settings
 *
 * @package VMPFence
 * @since 2.1.0
 * @since 2.2.1 Updated text-domain to vmpfence-security
 * @since 2.2.2 Updated to reflect VMP Security branding, Improved UI/UX design
 * @since 2.2.3 Removed license check to allow 2FA for all users
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Capability check - allow access if user is in forced setup mode
$current_user_id = get_current_user_id();
$force_setup_flag = get_user_meta($current_user_id, 'vmpfence_force_2fa_setup', true);

if (!current_user_can('manage_options') && !$force_setup_flag) {
    // User doesn't have permission AND is not in forced setup mode
    wp_die(__('You do not have sufficient permissions to access this page.', 'vmpfence-security'));
}
// If user has force_setup flag, allow access regardless of manage_options capability

// Enqueue Login Security CSS
wp_enqueue_style('vmpfence-login-security-css', 
    plugins_url('assets/css/login-security.css', dirname(__FILE__)), 
    array(), VMPFENCE_VERSION);

// Enqueue toast notification system
wp_enqueue_style('vmpfence-toast-notifications', 
    plugins_url('assets/css/toast-notifications.css', dirname(__FILE__)), 
    array(), VMPFENCE_VERSION);
wp_enqueue_script('vmpfence-toast-notifications', 
    plugins_url('assets/js/toast-notifications.js', dirname(__FILE__)), 
    array('jquery'), VMPFENCE_VERSION, true);

// Enqueue Premium Upgrade Button Handler (required for "UPGRADE TO PREMIUM" buttons)
wp_enqueue_script('vmpfence-premium-upgrade', 
    plugins_url('assets/js/premium-upgrade.js', dirname(__FILE__)), 
    array('jquery'), VMPFENCE_VERSION, true);

wp_enqueue_script('vmpfence-login-security', 
    plugins_url('assets/js/login-security.js', dirname(__FILE__)), 
    array('jquery', 'vmpfence-toast-notifications'), 
    VMPFENCE_VERSION, true);

// Enqueue Login Security Settings JavaScript (Phase 1)
wp_enqueue_script('vmpfence-login-security-settings', 
    plugins_url('assets/js/login-security-settings.js', dirname(__FILE__)), 
    array('jquery', 'vmpfence-toast-notifications'), 
    VMPFENCE_VERSION, true);

// Get current user
$current_user = wp_get_current_user();
$user_id = $current_user->ID;

// Get API key
$api_key = VMPFence_Config::get('central_api_key');
// Use centralized license getter for consistency
$license_type = VMPFence_License::get_license_type();

// 2FA is now available for all users (free and premium)
$is_premium = true; // Set to true to allow all users access to 2FA UI

// Get 2FA status from backend
$two_fa_status = array('enabled' => false, 'method' => null);
if ($api_key) {
    $api_client = new VMPFence_API_Client();
    $status_response = $api_client->get_2fa_status($api_key, $user_id);
    if (!is_wp_error($status_response)) {
        $two_fa_status = $status_response;
    }
}

// API returns 'enabled' not 'is_enabled'
$is_2fa_enabled = isset($two_fa_status['enabled']) && $two_fa_status['enabled'];

// Check if forced setup is required
$force_setup = get_user_meta($user_id, 'vmpfence_force_2fa_setup', true);
$is_forced_setup = isset($_GET['force_setup']) && $_GET['force_setup'] == '1';

// Get active tab from URL parameter
$active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'two-factor-authentication';

// Force tab to 2FA if setup is required
if ($force_setup) {
    $active_tab = 'two-factor-authentication';
}
?>

<!-- WordPress Admin Notices Container (must be in .wrap for WordPress notices to display correctly) -->
<div class="wrap">
    <?php settings_errors(); ?>
</div>

<div class="vmpfence-login-security" style="max-width: 1300px;">
    <h1><?php esc_html_e('Login Security', 'vmpfence-security'); ?></h1>
    
    <?php if ($force_setup): ?>
    <div class="notice notice-error" style="border-left-color: #dc3232; padding: 15px; margin: 20px 0;">
        <h2 style="margin-top: 0;">🔒 <?php esc_html_e('Two-Factor Authentication Setup Required', 'vmpfence-security'); ?></h2>
        <p><strong><?php esc_html_e('Your account requires 2FA setup. You must complete the setup below before you can access other areas of WordPress admin.', 'vmpfence-security'); ?></strong></p>
        <p><?php esc_html_e('Follow the steps below to activate Two-Factor Authentication using an authenticator app on your phone.', 'vmpfence-security'); ?></p>
    </div>
    <?php endif; ?>
    
    <!-- Tabs Navigation -->
    <div class="vmpfence-tabs">
        <ul class="vmpfence-tabs-nav">
            <li><button class="<?php echo ($active_tab === 'two-factor-authentication') ? 'active' : ''; ?>" data-tab="two-factor-authentication" <?php echo $force_setup ? 'style="pointer-events: none; opacity: 1;"' : ''; ?>><?php echo esc_html__('Two-Factor Authentication', 'vmpfence-security'); ?></button></li>
            <li><button class="<?php echo ($active_tab === 'settings') ? 'active' : ''; ?>" data-tab="settings" <?php echo $force_setup ? 'disabled style="pointer-events: none; opacity: 0.5;"' : ''; ?>><?php echo esc_html__('Settings', 'vmpfence-security'); ?></button></li>
        </ul>

        <!-- Two-Factor Authentication Tab -->
        <div id="two-factor-authentication" class="vmpfence-tab-content <?php echo ($active_tab === 'two-factor-authentication') ? 'active' : ''; ?>">
    
    <div class="vmpfence-card">
        <div class="vmpfence-card-header" id="vmpfence-2fa-header">
            <h2><?php esc_html_e('Two-Factor Authentication (2FA)', 'vmpfence-security'); ?></h2>
            <!-- 2FA is now available for all users -->
        </div>

        <div class="vmpfence-card-body">
            <?php if (!$api_key): ?>
                <!-- No API Key Warning -->
                <div class="vmpfence-notice vmpfence-notice-warning">
                    <p><?php esc_html_e('API key not configured. Please complete plugin setup first.', 'vmpfence-security'); ?></p>
                </div>

            <?php else: ?>
                <!-- 2FA Management Section for Premium Users -->
                
                <?php if ($is_2fa_enabled): ?>
                    <!-- 2FA Enabled State -->
                    <div class="vmpfence-2fa-status vmpfence-2fa-enabled">
                        <div class="vmpfence-status-content">
                            <h3><?php esc_html_e('Two-Factor Authentication is Active', 'vmpfence-security'); ?></h3>
                            <p><?php esc_html_e('Your account is protected with 2FA. You\'ll need to enter a code from your authenticator app when logging in.', 'vmpfence-security'); ?></p>
                        </div>
                    </div>

                    <div class="vmpfence-2fa-actions">
                        <button type="button" class="button" id="vmpfence-regenerate-codes">
                            <?php esc_html_e('Regenerate Backup Codes', 'vmpfence-security'); ?>
                        </button>
                        <button type="button" class="button button-secondary" id="vmpfence-disable-2fa">
                            <?php esc_html_e('Disable 2FA', 'vmpfence-security'); ?>
                        </button>
                    </div>

                    <!-- Backup Codes Display -->
                    <div id="vmpfence-backup-codes-container" style="display: none;">
                        <div class="vmpfence-backup-codes">
                            <h3><?php esc_html_e('Your Backup Codes', 'vmpfence-security'); ?></h3>
                            <p class="description"><?php esc_html_e('Save these codes in a safe place. Each code can only be used once. Use them if you lose access to your authenticator app.', 'vmpfence-security'); ?></p>
                            <div class="vmpfence-codes-grid" id="vmpfence-codes-list">
                                <!-- Backup codes will be inserted here -->
                            </div>
                            <div class="vmpfence-codes-actions">
                                <button type="button" class="button" id="vmpfence-print-codes">
                                    <?php esc_html_e('Print Codes', 'vmpfence-security'); ?>
                                </button>
                                <button type="button" class="button" id="vmpfence-download-codes">
                                    <?php esc_html_e('Download Codes', 'vmpfence-security'); ?>
                                </button>
                            </div>
                        </div>
                    </div>

                <?php else: ?>
                    <!-- 2FA Disabled State -->
                    <div class="vmpfence-2fa-status vmpfence-2fa-disabled">
                        <div class="vmpfence-status-content">
                            <h3><?php esc_html_e('Two-Factor Authentication is Disabled', 'vmpfence-security'); ?></h3>
                            <p><?php esc_html_e('Add an extra layer of security to your account. When enabled, you\'ll need both your password and a code from your phone to log in.', 'vmpfence-security'); ?></p>
                        </div>
                    </div>

                    <!-- Editing User Display -->
                    <div class="vmpfence-editing-user">
                        <span class="vmpfence-editing-label">Editing User:</span>
                        <span class="vmpfence-editing-value">
                            <?php echo esc_html($current_user->user_login); ?> <span class="vmpfence-you-badge">(you)</span>
                        </span>
                    </div>

                    <!-- Single Page Setup - Auto-loads on page load -->
                    <div id="vmpfence-2fa-setup-container" class="vmpfence-2fa-setup-container" data-auto-load="true">
                        <div class="vmpfence-2fa-setup-grid">
                            <!-- Left Column: QR Code & Secret Key -->
                            <div class="vmpfence-qr-section">
                                <h3><?php esc_html_e('1. Scan Code or Enter Key', 'vmpfence-security'); ?></h3>
                                <p class="vmpfence-description"><?php esc_html_e('Scan this QR code with your authenticator app (Google Authenticator, Authy, 1Password, etc.)', 'vmpfence-security'); ?></p>
                                
                                <div class="vmpfence-qr-container">
                                    <div id="vmpfence-qr-code">
                                        <!-- QR code will be inserted here -->
                                    </div>
                                </div>

                                <div class="vmpfence-manual-entry">
                                    <p class="vmpfence-manual-label"><strong><?php esc_html_e('Can\'t scan? Enter this key manually:', 'vmpfence-security'); ?></strong></p>
                                    <div class="vmpfence-secret-key">
                                        <code id="vmpfence-secret-display"></code>
                                        <button type="button" class="button button-small vmpfence-copy-btn" id="vmpfence-copy-secret">
                                            <?php esc_html_e('Copy', 'vmpfence-security'); ?>
                                        </button>
                                    </div>
                                </div>
                            </div>

                            <!-- Right Column: Backup Codes & Verification -->
                            <div class="vmpfence-codes-section">
                                <h3><?php esc_html_e('2. Enter Code from Authenticator App', 'vmpfence-security'); ?></h3>
                                
                                <!-- Backup Codes Display -->
                                <div class="vmpfence-backup-codes-display">
                                    <h4><?php esc_html_e('Download Recovery Codes', 'vmpfence-security'); ?> <span class="vmpfence-optional-badge"><?php esc_html_e('Optional', 'vmpfence-security'); ?></span></h4>
                                    <p class="vmpfence-codes-description"><?php esc_html_e('Use one of these 5 codes to log in if you lose access to your authenticator device. Codes are 16 characters long plus optional spaces. Each one may be used only once.', 'vmpfence-security'); ?></p>
                                    
                                    <div class="vmpfence-codes-grid" id="vmpfence-backup-codes-list">
                                        <!-- Backup codes will be inserted here -->
                                    </div>
                                    
                                    <button type="button" class="button vmpfence-download-btn" id="vmpfence-setup-download-codes">
                                        <?php esc_html_e('DOWNLOAD', 'vmpfence-security'); ?>
                                    </button>
                                </div>

                                <!-- Verification Input -->
                                <div class="vmpfence-verification-section">
                                    <p class="vmpfence-verify-description"><?php esc_html_e('Enter the code from your authenticator app below to verify and activate two-factor authentication for this account.', 'vmpfence-security'); ?></p>
                                    
                                    <div class="vmpfence-code-input-group">
                                        <input type="text" 
                                               id="vmpfence-verify-code" 
                                               class="vmpfence-code-input" 
                                               maxlength="6" 
                                               pattern="[0-9]{6}" 
                                               placeholder="123456"
                                               autocomplete="off" />
                                    </div>

                                    <p class="vmpfence-help-link"><?php esc_html_e('For help on setting up an app,', 'vmpfence-security'); ?> <a href="#" target="_blank"><?php esc_html_e('visit our help article', 'vmpfence-security'); ?></a>.</p>

                                    <button type="button" class="button button-primary button-large vmpfence-activate-btn" id="vmpfence-activate-2fa">
                                        <?php esc_html_e('ACTIVATE', 'vmpfence-security'); ?>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>

                <?php endif; ?>

            <?php endif; ?>
        </div>
    </div>
        </div>
        <!-- End Two-Factor Authentication Tab -->

        <!-- Settings Tab -->
        <div id="settings" class="vmpfence-tab-content <?php echo ($active_tab === 'settings') ? 'active' : ''; ?>">

            <!-- Settings Header -->
            <div class="login-settings-header">
                <h2><?php esc_html_e('Login Security Settings', 'vmpfence-security'); ?></h2>
                <div class="login-settings-actions">
                    <button type="button" id="vmpfence-cancel-settings" class="button"><?php esc_html_e('CANCEL', 'vmpfence-security'); ?></button>
                    <button type="button" id="vmpfence-save-settings" class="button button-primary"><?php esc_html_e('SAVE', 'vmpfence-security'); ?></button>
                </div>
            </div>

            <!-- User Summary Section -->
            <div class="login-settings-section">
                <h3><?php esc_html_e('User Summary', 'vmpfence-security'); ?></h3>
                <div class="login-settings-user-summary">
                    <table class="widefat">
                        <thead>
                            <tr>
                                <th><?php esc_html_e('Role', 'vmpfence-security'); ?></th>
                                <th><?php esc_html_e('Total Users', 'vmpfence-security'); ?></th>
                                <th><?php esc_html_e('2FA Active', 'vmpfence-security'); ?></th>
                                <th><?php esc_html_e('2FA Inactive', 'vmpfence-security'); ?></th>
                            </tr>
                        </thead>
                        <tbody id="user-stats-tbody">
                            <tr>
                                <td><?php esc_html_e('Administrator', 'vmpfence-security'); ?></td>
                                <td>2</td>
                                <td>0</td>
                                <td>2</td>
                            </tr>
                            <tr>
                                <td><?php esc_html_e('Total', 'vmpfence-security'); ?></td>
                                <td>2</td>
                                <td>0</td>
                                <td>2</td>
                            </tr>
                        </tbody>
                    </table>
                    <p class="login-settings-manage-link"><a href="<?php echo esc_url(admin_url('users.php')); ?>"><?php esc_html_e('Manage Users', 'vmpfence-security'); ?></a></p>
                </div>
            </div>

            <!-- 2FA Configuration Section -->
            <div class="login-settings-section">
                <h3><?php esc_html_e('2FA', 'vmpfence-security'); ?></h3>
                
                <div class="login-settings-option-row">
                    <label class="login-settings-option-label"><?php esc_html_e('2FA Roles', 'vmpfence-security'); ?></label>
                    <div class="login-settings-option-control">
                        <div class="login-settings-role-group">
                            <div class="login-settings-role-item">
                                <label><?php esc_html_e('Administrator', 'vmpfence-security'); ?></label>
                                <select id="vmpfence-2fa-role-administrator">
                                    <option value="disabled"><?php esc_html_e('Disabled', 'vmpfence-security'); ?></option>
                                    <option value="optional" selected><?php esc_html_e('Optional', 'vmpfence-security'); ?></option>
                                    <option value="required"><?php esc_html_e('Required', 'vmpfence-security'); ?></option>
                                </select>
                            </div>
                            <div class="login-settings-role-item">
                                <label><?php esc_html_e('Editor', 'vmpfence-security'); ?></label>
                                <select id="vmpfence-2fa-role-editor">
                                    <option value="disabled" selected><?php esc_html_e('Disabled', 'vmpfence-security'); ?></option>
                                    <option value="optional"><?php esc_html_e('Optional', 'vmpfence-security'); ?></option>
                                    <option value="required"><?php esc_html_e('Required', 'vmpfence-security'); ?></option>
                                </select>
                            </div>
                            <div class="login-settings-role-item">
                                <label><?php esc_html_e('Author', 'vmpfence-security'); ?></label>
                                <select id="vmpfence-2fa-role-author">
                                    <option value="disabled" selected><?php esc_html_e('Disabled', 'vmpfence-security'); ?></option>
                                    <option value="optional"><?php esc_html_e('Optional', 'vmpfence-security'); ?></option>
                                    <option value="required"><?php esc_html_e('Required', 'vmpfence-security'); ?></option>
                                </select>
                            </div>
                            <div class="login-settings-role-item">
                                <label><?php esc_html_e('Contributor', 'vmpfence-security'); ?></label>
                                <select id="vmpfence-2fa-role-contributor">
                                    <option value="disabled" selected><?php esc_html_e('Disabled', 'vmpfence-security'); ?></option>
                                    <option value="optional"><?php esc_html_e('Optional', 'vmpfence-security'); ?></option>
                                    <option value="required"><?php esc_html_e('Required', 'vmpfence-security'); ?></option>
                                </select>
                            </div>
                            <div class="login-settings-role-item">
                                <label><?php esc_html_e('Subscriber', 'vmpfence-security'); ?></label>
                                <select id="vmpfence-2fa-role-subscriber">
                                    <option value="disabled" selected><?php esc_html_e('Disabled', 'vmpfence-security'); ?></option>
                                    <option value="optional"><?php esc_html_e('Optional', 'vmpfence-security'); ?></option>
                                    <option value="required"><?php esc_html_e('Required', 'vmpfence-security'); ?></option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <?php esc_html_e('Grace Period', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <input type="number" id="vmpfence-2fa-grace-period" value="10" min="0" max="365" required class="small-text" /> <span><?php esc_html_e('days', 'vmpfence-security'); ?></span>
                        <p class="description"><?php esc_html_e('For users that require 2FA, users will initially be able to delay set up 2FA during this grace period...', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-2fa-remember-device" />
                        <?php esc_html_e('Allow remembering device for 30 days', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('If enabled, users with 2FA enabled may choose to be prompted for a code only once every 30 days per device.', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <?php esc_html_e('Require 2FA for XML-RPC call authentication', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <div class="login-settings-button-group">
                            <button type="button" id="vmpfence-xmlrpc-2fa-btn-skipped" class="login-settings-toggle-btn active"><?php esc_html_e('SKIPPED', 'vmpfence-security'); ?></button>
                            <button type="button" id="vmpfence-xmlrpc-2fa-btn-required" class="login-settings-toggle-btn"><?php esc_html_e('REQUIRED', 'vmpfence-security'); ?></button>
                        </div>
                        <p class="description"><?php esc_html_e('If enabled, XML-RPC calls that require authentication will also require a valid 2FA code...', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-disable-xmlrpc-auth" />
                        <?php esc_html_e('Disable XML-RPC authentication', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('If disabled, XML-RPC requests that attempt authentication will be rejected...', 'vmpfence-security'); ?></p>
                    </div>
                </div>
            </div>

            <!-- WooCommerce Section -->
            <div class="login-settings-section">
                <h3><?php esc_html_e('WooCommerce & Custom Integrations', 'vmpfence-security'); ?></h3>
                
                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-woocommerce-integration" />
                        <?php esc_html_e('WooCommerce integration', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('When enabled, reCAPTCHA and 2FA prompt support will be added to WooCommerce login and registration forms...', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-2fa-shortcode" />
                        <?php esc_html_e('2FA management shortcode', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('When enabled, the "vmpfence_2fa" shortcode may be used...', 'vmpfence-security'); ?></p>
                    </div>
                </div>
            </div>

            <!-- reCAPTCHA Section -->
            <div class="login-settings-section" id="vmpfence-recaptcha-settings">
                <h3><?php esc_html_e('reCAPTCHA', 'vmpfence-security'); ?></h3>
                
                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-recaptcha-enabled" />
                        <?php esc_html_e('Enable reCAPTCHA on the login and user registration pages', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('reCAPTCHA is close but not quite as effective as VMP Security versions...', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label"><?php esc_html_e('reCAPTCHA v3 Site Key', 'vmpfence-security'); ?></label>
                    <div class="login-settings-option-control">
                        <input type="text" id="vmpfence-recaptcha-site-key" class="regular-text" placeholder="6Le..." />
                        <p class="description"><?php esc_html_e('Get your Site Key from the Google reCAPTCHA admin console', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label"><?php esc_html_e('reCAPTCHA v3 Secret', 'vmpfence-security'); ?></label>
                    <div class="login-settings-option-control">
                        <input type="text" id="vmpfence-recaptcha-secret-key" class="regular-text" placeholder="6Le..." />
                        <p class="description"><?php esc_html_e('Your Secret Key (kept secure in database)', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label"><?php esc_html_e('reCAPTCHA humanbot threshold score', 'vmpfence-security'); ?></label>
                    <div class="login-settings-option-control">
                        <select id="vmpfence-recaptcha-threshold">
                            <option value="0.0"><?php esc_html_e('0.0 (Most Strict - May block humans)', 'vmpfence-security'); ?></option>
                            <option value="0.1"><?php esc_html_e('0.1', 'vmpfence-security'); ?></option>
                            <option value="0.2"><?php esc_html_e('0.2', 'vmpfence-security'); ?></option>
                            <option value="0.3"><?php esc_html_e('0.3 (Strict)', 'vmpfence-security'); ?></option>
                            <option value="0.4"><?php esc_html_e('0.4', 'vmpfence-security'); ?></option>
                            <option value="0.5" selected><?php esc_html_e('0.5 (Balanced)', 'vmpfence-security'); ?></option>
                            <option value="0.6"><?php esc_html_e('0.6', 'vmpfence-security'); ?></option>
                            <option value="0.7"><?php esc_html_e('0.7 (Lenient)', 'vmpfence-security'); ?></option>
                            <option value="0.8"><?php esc_html_e('0.8', 'vmpfence-security'); ?></option>
                            <option value="0.9"><?php esc_html_e('0.9', 'vmpfence-security'); ?></option>
                            <option value="1.0"><?php esc_html_e('1.0 (Most Lenient - May allow bots)', 'vmpfence-security'); ?></option>
                        </select>
                        <p class="description"><?php esc_html_e('Lower scores = stricter verification (may block legitimate users)', 'vmpfence-security'); ?></p>
                    </div>
                </div>
            </div>

            <!-- General Section -->
            <div class="login-settings-section">
                <h3><?php esc_html_e('General', 'vmpfence-security'); ?></h3>
                
                <div class="login-settings-option-row">
                    <label class="login-settings-option-label"><?php esc_html_e('Allowlisted IP addresses that bypass 2FA and reCAPTCHA', 'vmpfence-security'); ?></label>
                    <div class="login-settings-option-control">
                        <textarea id="vmpfence-ip-allowlist" rows="8" class="large-text"></textarea>
                        <p class="description">
                            <?php esc_html_e('Allowlisted IPs must be placed on separate lines. You can specify ranges using the following formats: 127.0.0.1/24, 127.0.0.[1-100], or 127.0.0.1-127.0.1.100.', 'vmpfence-security'); ?>
                            <br>
                            <strong><?php esc_html_e('Your current IP:', 'vmpfence-security'); ?> <code><?php echo esc_html($_SERVER['REMOTE_ADDR'] ?? 'Unknown'); ?></code></strong>
                        </p>
                        <div style="margin-top: 10px;">
                            <button type="button" id="vmpfence-validate-ip-allowlist" class="button button-secondary">
                                <?php esc_html_e('Validate & Apply', 'vmpfence-security'); ?>
                            </button>
                            <span id="vmpfence-ip-allowlist-status" style="margin-left: 10px; font-weight: 600;"></span>
                        </div>
                    </div>
                </div>
            </div>

            <!-- NTP Section -->
            <div class="login-settings-section">
                <h3><?php esc_html_e('NTP', 'vmpfence-security'); ?></h3>
                
                <div class="login-settings-option-row">
                    <div class="login-settings-option-label">
                        <p><?php esc_html_e('NTP is a protocol that allows for remote time synchronization. VMP Security Login Security uses this protocol to ensure that it has the most accurate time which is necessary for TOTP-based two-factor authentication.', 'vmpfence-security'); ?></p>
                        <p id="ntp-status-text"><strong><?php esc_html_e('NTP is currently enabled.', 'vmpfence-security'); ?></strong></p>
                    </div>
                    <div class="login-settings-option-control">
                        <button type="button" id="vmpfence-ntp-toggle-btn" class="button"><?php esc_html_e('DISABLE', 'vmpfence-security'); ?></button>
                    </div>
                </div>
            </div>

            <!-- Additional Options -->
            <div class="login-settings-section">
                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-show-last-login-column" />
                        <?php esc_html_e('Show last login column on WP Users page', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('When enabled, the last login timestamp will be displayed for each user on the WP Users page...', 'vmpfence-security'); ?></p>
                    </div>
                </div>

                <div class="login-settings-option-row">
                    <label class="login-settings-option-label">
                        <input type="checkbox" id="vmpfence-delete-on-deactivate" />
                        <?php esc_html_e('Delete Login Security tables and data on deactivation', 'vmpfence-security'); ?>
                    </label>
                    <div class="login-settings-option-control">
                        <p class="description"><?php esc_html_e('If enabled, all settings and 2FA records will be deleted on deactivation...', 'vmpfence-security'); ?></p>
                    </div>
                </div>
            </div>

        </div>
        <!-- End Settings Tab -->

    </div>
    <!-- End Tabs -->

    <!-- Loading Overlay -->
    <div id="vmpfence-loading-overlay" class="vmpfence-overlay" style="display: none;">
        <div class="vmpfence-spinner"></div>
        <p id="vmpfence-loading-message"><?php esc_html_e('Processing...', 'vmpfence-security'); ?></p>
    </div>

    <!-- Hidden data for JavaScript -->
    <input type="hidden" id="vmpfence-ajax-url" value="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" />
    <input type="hidden" id="vmpfence-nonce" value="<?php echo esc_attr(wp_create_nonce('vmpfence_2fa_nonce')); ?>" />
    <input type="hidden" id="vmpfence-settings-nonce" value="<?php echo esc_attr(wp_create_nonce('vmpfence_login_security_settings_nonce')); ?>" />
    <input type="hidden" id="vmpfence-user-id" value="<?php echo esc_attr($user_id); ?>" />
    
    <!-- Toast Notification Container -->
    <?php include VMPFENCE_PLUGIN_DIR . 'admin/views/partials/toast-notification-container.php'; ?>
</div>

<style>
/* Editing User Display */
.vmpfence-editing-user {
    padding: 8px 12px;
    background: #f8f9fa;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-size: 14px;
    margin-bottom: 12px;
}

.vmpfence-editing-label {
    font-weight: 600;
    margin-right: 8px;
    color: #1d2327;
}

.vmpfence-editing-value {
    color: #2c3338;
}

.vmpfence-you-badge {
    color: #646970;
    font-style: italic;
    margin-left: 4px;
}
</style>
