<?php
/**
 * VMPFence Login Security View
 *
 * @package VMPFence
 * @since 1.0.0
 * @since 2.0.0 Updated to reflect VMP WordPress Security branding.
 * @since 2.2.2 Updated to reflect VMP Security branding.
 */

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

// Get filter for successful/failed
$login_filter = isset($_GET['login_filter']) ? sanitize_text_field($_GET['login_filter']) : 'all';

// Fetch recent login attempts from database
global $wpdb;
$logins_table = $wpdb->prefix . 'vmpfence_logins';

// Check MySQL version for INET6_NTOA support (MySQL 5.6.3+)
$mysql_version = $wpdb->get_var('SELECT VERSION()');
$has_inet6 = version_compare($mysql_version, '5.6.3', '>=');
$ip_convert = $has_inet6 ? 'INET6_NTOA(IP)' : 'INET_NTOA(IP)';

// Build query based on filter
$where_clause = '';
if ($login_filter === 'successful') {
    $where_clause = 'WHERE fail = 0';
} elseif ($login_filter === 'failed') {
    $where_clause = 'WHERE fail = 1';
}

$recent_logins = $wpdb->get_results(
    "SELECT 
        id,
        ctime,
        username,
        {$ip_convert} as ip_address,
        fail,
        action,
        reason
    FROM {$logins_table}
    {$where_clause}
    ORDER BY ctime DESC
    LIMIT 50",
    ARRAY_A
);

// Get statistics
$total_logins = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$logins_table}");
$successful_logins = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$logins_table} WHERE fail = 0");
$failed_logins = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$logins_table} WHERE fail = 1");

?>

<div class="wrap">
    <h1>VMP Security Login Security</h1>
    
    <div class="vmpfence-login-security">
        <div class="vmpfence-card">
            <h2>Brute Force Protection</h2>
            <p>Login protection is currently <strong>enabled</strong>.</p>
            <p>Maximum failed attempts: <strong>5</strong></p>
            <p>Lockout duration: <strong>30 minutes</strong></p>
        </div>
        
        <div class="vmpfence-card">
            <h2>Login Attempts</h2>
            
            <!-- Filter Buttons -->
            <div style="margin-bottom: 15px;">
                <button class="button <?php echo ($login_filter === 'successful') ? 'button-primary' : ''; ?>" onclick="window.location.href='<?php echo esc_url(add_query_arg('login_filter', 'successful')); ?>'">Successful</button>
                <button class="button <?php echo ($login_filter === 'failed') ? 'button-primary' : ''; ?>" onclick="window.location.href='<?php echo esc_url(add_query_arg('login_filter', 'failed')); ?>'">Failed</button>
                <?php if ($login_filter !== 'all'): ?>
                    <button class="button" onclick="window.location.href='<?php echo esc_url(remove_query_arg('login_filter')); ?>'">Show All</button>
                <?php endif; ?>
            </div>
            
            <!-- Statistics -->
            <div style="background: #f8f9fa; padding: 15px; border-radius: 6px; margin-bottom: 20px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; text-align: center;">
                <div>
                    <div style="font-size: 24px; font-weight: 700; color: #191970; margin-bottom: 5px;"><?php echo number_format($total_logins); ?></div>
                    <div style="font-size: 13px; color: #666;">Total Attempts</div>
                </div>
                <div>
                    <div style="font-size: 24px; font-weight: 700; color: #28a745; margin-bottom: 5px;"><?php echo number_format($successful_logins); ?></div>
                    <div style="font-size: 13px; color: #666;">Successful</div>
                </div>
                <div>
                    <div style="font-size: 24px; font-weight: 700; color: #dc3545; margin-bottom: 5px;"><?php echo number_format($failed_logins); ?></div>
                    <div style="font-size: 13px; color: #666;">Failed</div>
                </div>
            </div>
            
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th>Username</th>
                        <th>IP</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($recent_logins)): ?>
                        <tr>
                            <td colspan="3" style="text-align: center; padding: 40px 20px;">
                                <div style="color: #95a5a6; font-style: italic; font-size: 15px;">No login attempts recorded</div>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($recent_logins as $login): ?>
                            <tr style="<?php echo ($login['fail'] == 1) ? 'background-color: #fff3f3;' : ''; ?>">
                                <td>
                                    <?php echo esc_html($login['username']); ?>
                                    <?php if ($login['fail'] == 1): ?>
                                        <span style="color: #dc3545; margin-left: 5px;">✗</span>
                                    <?php else: ?>
                                        <span style="color: #28a745; margin-left: 5px;">✓</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo esc_html($login['ip_address'] ?: 'Unknown'); ?></td>
                                <td><?php echo esc_html(gmdate('Y-m-d H:i:s', (int)$login['ctime'])); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <div class="vmpfence-card">
            <h2>Locked IP Addresses</h2>
            <table class="wp-list-table widefat fixed striped">
                <thead>
                    <tr>
                        <th>IP Address</th>
                        <th>Locked Until</th>
                        <th>Failed Attempts</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td colspan="4">No IP addresses are currently locked.</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php
// Enqueue admin styles (includes login status styles)
wp_enqueue_style(
    'vmpfence-admin',
    plugin_dir_url(dirname(__FILE__)) . 'assets/css/admin.css',
    array(),
    VMPFENCE_VERSION
);
?>
