<?php
session_start();

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

require_once '../../config/database.php';

$database = new Database();
$db = $database->getConnection();

// إحصائيات
$stats = [];

// إجمالي الزيارات
$query = "SELECT COUNT(*) as total FROM site_visits";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['total'] = $stmt->fetch(PDO::FETCH_ASSOC)['total'];

// زيارات اليوم
$query = "SELECT COUNT(*) as today FROM site_visits WHERE DATE(created_at) = CURDATE()";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['today'] = $stmt->fetch(PDO::FETCH_ASSOC)['today'];

// زيارات هذا الأسبوع
$query = "SELECT COUNT(*) as week FROM site_visits WHERE YEARWEEK(created_at) = YEARWEEK(NOW())";
$stmt = $db->prepare($query);
$stmt->execute();
$stats['week'] = $stmt->fetch(PDO::FETCH_ASSOC)['week'];

// آخر 20 زيارة
$query = "SELECT v.*, u.name as user_name, u.phone 
          FROM site_visits v 
          LEFT JOIN users u ON v.user_id = u.id 
          ORDER BY v.created_at DESC 
          LIMIT 20";
$stmt = $db->prepare($query);
$stmt->execute();
$visits = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>إحصائيات الزيارات</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { font-family: 'Tajawal', sans-serif; }
    </style>
</head>
<body class="bg-gray-50">
    <div class="min-h-screen">
        <header class="bg-white shadow-sm border-b border-gray-200">
            <div class="flex items-center justify-between px-6 py-4">
                <div class="flex items-center space-x-4 space-x-reverse">
                    <a href="../dashboard.php" class="text-gray-600 hover:text-gray-900">
                        <i class="fas fa-arrow-right text-xl"></i>
                    </a>
                    <h1 class="text-2xl font-bold text-gray-900">
                        <i class="fas fa-chart-line text-blue-600 ml-2"></i>إحصائيات الزيارات
                    </h1>
                </div>
            </div>
        </header>

        <div class="p-6">
            <!-- Stats Cards -->
            <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600 mb-1">إجمالي الزيارات</p>
                            <p class="text-3xl font-bold text-gray-900"><?php echo number_format($stats['total']); ?></p>
                        </div>
                        <div class="bg-blue-100 p-4 rounded-full">
                            <i class="fas fa-users text-blue-600 text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600 mb-1">زيارات اليوم</p>
                            <p class="text-3xl font-bold text-green-600"><?php echo number_format($stats['today']); ?></p>
                        </div>
                        <div class="bg-green-100 p-4 rounded-full">
                            <i class="fas fa-calendar-day text-green-600 text-2xl"></i>
                        </div>
                    </div>
                </div>

                <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-6">
                    <div class="flex items-center justify-between">
                        <div>
                            <p class="text-sm text-gray-600 mb-1">زيارات هذا الأسبوع</p>
                            <p class="text-3xl font-bold text-purple-600"><?php echo number_format($stats['week']); ?></p>
                        </div>
                        <div class="bg-purple-100 p-4 rounded-full">
                            <i class="fas fa-calendar-week text-purple-600 text-2xl"></i>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Recent Visits -->
            <div class="bg-white rounded-lg shadow-sm border border-gray-200">
                <div class="px-6 py-4 border-b border-gray-200">
                    <h2 class="text-lg font-semibold text-gray-900">
                        <i class="fas fa-history text-gray-600 ml-2"></i>آخر الزيارات
                    </h2>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الزائر</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الجهاز</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">المتصفح</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">IP</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الصفحة</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">الوقت</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            <?php foreach ($visits as $visit): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4">
                                    <div class="flex items-center">
                                        <?php if ($visit['user_id']): ?>
                                            <i class="fas fa-user text-green-600 ml-2"></i>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($visit['user_name']); ?></div>
                                                <div class="text-xs text-gray-500"><?php echo htmlspecialchars($visit['email']); ?></div>
                                            </div>
                                        <?php else: ?>
                                            <i class="fas fa-user-secret text-gray-400 ml-2"></i>
                                            <span class="text-sm text-gray-500">زائر</span>
                                        <?php endif; ?>
                                    </div>
                                </td>
                                <td class="px-6 py-4">
                                    <?php
                                    $device_icon = $visit['device_type'] === 'Mobile' ? 'fa-mobile-alt' : 
                                                  ($visit['device_type'] === 'Tablet' ? 'fa-tablet-alt' : 'fa-desktop');
                                    $device_color = $visit['device_type'] === 'Mobile' ? 'text-blue-600' : 
                                                   ($visit['device_type'] === 'Tablet' ? 'text-purple-600' : 'text-gray-600');
                                    ?>
                                    <i class="fas <?php echo $device_icon; ?> <?php echo $device_color; ?> ml-2"></i>
                                    <span class="text-sm text-gray-900"><?php echo $visit['device_type']; ?></span>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-900"><?php echo htmlspecialchars($visit['browser']); ?></td>
                                <td class="px-6 py-4">
                                    <code class="text-xs bg-gray-100 px-2 py-1 rounded"><?php echo htmlspecialchars($visit['ip_address']); ?></code>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-900"><?php echo htmlspecialchars($visit['page']); ?></td>
                                <td class="px-6 py-4 text-sm text-gray-500"><?php echo date('Y-m-d H:i', strtotime($visit['created_at'])); ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
