<?php
require_once 'config/config.php';
requireLogin();

$currentUser = getCurrentUser();

// Kullanıcı istatistikleri
try {
    $db = new Database();
    
    // Toplam kullanıcı sayısı
    $totalUsers = $db->query("SELECT COUNT(*) as count FROM users")->fetch()['count'];
    
    // Aktif kullanıcı sayısı
    $activeUsers = $db->query("SELECT COUNT(*) as count FROM users WHERE status = 'active'")->fetch()['count'];
    
    // Admin sayısı
    $adminUsers = $db->query("SELECT COUNT(*) as count FROM users WHERE role = 'admin'")->fetch()['count'];
    
    // Son giriş yapan kullanıcılar
    $recentLogins = $db->query(
        "SELECT u.username, u.full_name, us.created_at 
         FROM user_sessions us 
         JOIN users u ON us.user_id = u.id 
         ORDER BY us.created_at DESC 
         LIMIT 5"
    )->fetchAll();
    
} catch (Exception $e) {
    error_log("Dashboard stats error: " . $e->getMessage());
    $totalUsers = $activeUsers = $adminUsers = 0;
    $recentLogins = [];
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ana Panel - <?php echo SITE_NAME; ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .logo h1 {
            font-size: 24px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-info span {
            font-size: 14px;
        }
        
        .nav {
            background: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .nav-content {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .nav ul {
            list-style: none;
            display: flex;
            gap: 30px;
        }
        
        .nav a {
            text-decoration: none;
            color: #333;
            font-weight: 500;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .nav a:hover, .nav a.active {
            background: #667eea;
            color: white;
        }
        
        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        
        .welcome {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
        }
        
        .welcome h2 {
            color: #333;
            margin-bottom: 1rem;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 2rem;
            margin-bottom: 2rem;
        }
        
        .stat-card {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        
        .stat-card h3 {
            font-size: 2.5rem;
            color: #667eea;
            margin-bottom: 0.5rem;
        }
        
        .stat-card p {
            color: #666;
            font-weight: 500;
        }
        
        .recent-activity {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .recent-activity h3 {
            color: #333;
            margin-bottom: 1rem;
        }
        
        .activity-list {
            list-style: none;
        }
        
        .activity-item {
            padding: 1rem 0;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .activity-item:last-child {
            border-bottom: none;
        }
        
        .activity-user {
            font-weight: 500;
            color: #333;
        }
        
        .activity-time {
            color: #666;
            font-size: 0.9rem;
        }
        
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #667eea;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-weight: 500;
            transition: background-color 0.3s;
        }
        
        .btn:hover {
            background: #5a6fd8;
        }
        
        .btn-danger {
            background: #e74c3c;
        }
        
        .btn-danger:hover {
            background: #c0392b;
        }
        
        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                gap: 1rem;
            }
            
            .nav ul {
                flex-direction: column;
                gap: 10px;
            }
            
            .container {
                padding: 0 1rem;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <div class="header-content">
            <div class="logo">
                <h1>Trendyol Sipariş Yönetimi</h1>
            </div>
            <div class="user-info">
                <span>Hoş geldiniz, <strong><?php echo htmlspecialchars($currentUser['full_name']); ?></strong></span>
                <span>(<?php echo htmlspecialchars($currentUser['role']); ?>)</span>
                <a href="logout.php" class="btn btn-danger">Çıkış</a>
            </div>
        </div>
    </header>
    
    <nav class="nav">
        <div class="nav-content">
            <ul>
                <li><a href="dashboard.php" class="active">Ana Panel</a></li>
                <?php if ($currentUser['role'] === ROLE_ADMIN): ?>
                    <li><a href="users.php">Kullanıcı Yönetimi</a></li>
                    <li><a href="add-user.php">Kullanıcı Ekle</a></li>
                <?php endif; ?>
                <li><a href="profile.php">Profil</a></li>
            </ul>
        </div>
    </nav>
    
    <div class="container">
        <div class="welcome">
            <h2>Ana Panel</h2>
            <p>Trendyol sipariş yönetim sistemine hoş geldiniz. Bu panel üzerinden kullanıcı yönetimi işlemlerini gerçekleştirebilirsiniz.</p>
            <p><strong>Not:</strong> Mobil uygulama kullanıcıları bu panelde tanımlanan kullanıcı bilgileri ile giriş yapabilir.</p>
        </div>
        
        <?php if ($currentUser['role'] === ROLE_ADMIN): ?>
        <div class="stats-grid">
            <div class="stat-card">
                <h3><?php echo $totalUsers; ?></h3>
                <p>Toplam Kullanıcı</p>
            </div>
            <div class="stat-card">
                <h3><?php echo $activeUsers; ?></h3>
                <p>Aktif Kullanıcı</p>
            </div>
            <div class="stat-card">
                <h3><?php echo $adminUsers; ?></h3>
                <p>Admin Kullanıcı</p>
            </div>
        </div>
        
        <div class="recent-activity">
            <h3>Son Giriş Yapan Kullanıcılar</h3>
            <?php if (!empty($recentLogins)): ?>
                <ul class="activity-list">
                    <?php foreach ($recentLogins as $login): ?>
                        <li class="activity-item">
                            <div>
                                <span class="activity-user"><?php echo htmlspecialchars($login['full_name']); ?></span>
                                <span>(<?php echo htmlspecialchars($login['username']); ?>)</span>
                            </div>
                            <span class="activity-time"><?php echo date('d.m.Y H:i', strtotime($login['created_at'])); ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php else: ?>
                <p>Henüz giriş kaydı bulunmuyor.</p>
            <?php endif; ?>
        </div>
        <?php else: ?>
        <div class="welcome">
            <h3>Kullanıcı Paneli</h3>
            <p>Merhaba <?php echo htmlspecialchars($currentUser['full_name']); ?>, sisteme başarıyla giriş yaptınız.</p>
            <p>Mobil uygulamayı kullanarak Trendyol siparişlerinizi yönetebilirsiniz.</p>
        </div>
        <?php endif; ?>
    </div>
</body>
</html>
