File size: 4,700 Bytes
659d490
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
```php
<?php
session_start();
include 'db.php';

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

$user_id = (int)$_GET['user_id'];

// Get user info
$sql = "SELECT name, email FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $sql);
$user = mysqli_fetch_assoc($result);

// Get all sessions for this user
$sessions = [];
$sql = "SELECT DISTINCT session_id FROM chat_logs WHERE user_id = $user_id ORDER BY session_id DESC";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
    $sessions[] = $row['session_id'];
}

// Get messages for selected session
$selected_session = isset($_GET['session']) ? (int)$_GET['session'] : ($sessions[0] ?? 0);
$messages = [];

if ($selected_session > 0) {
    $sql = "SELECT * FROM chat_logs 
            WHERE user_id = $user_id AND session_id = $selected_session 
            ORDER BY timestamp ASC";
    $result = mysqli_query($conn, $sql);
    while ($row = mysqli_fetch_assoc($result)) {
        $messages[] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>User Chats - AdminBot BrainHub</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="components/navbar.js"></script>
    <script src="components/sidebar.js"></script>
    <style>
        .chat-container {
            max-height: 500px;
            overflow-y: auto;
        }
        .user-message {
            background-color: #e3f2fd;
            border-radius: 10px;
            padding: 10px;
            margin: 5px 0;
            max-width: 80%;
            margin-left: auto;
        }
        .bot-message {
            background-color: #f1f1f1;
            border-radius: 10px;
            padding: 10px;
            margin: 5px 0;
            max-width: 80%;
        }
    </style>
</head>
<body class="bg-gray-100">
    <custom-navbar></custom-navbar>
    <div class="flex">
        <custom-sidebar></custom-sidebar>
        
        <main class="flex-1 p-8">
            <div class="mb-6">
                <a href="admin.php" class="text-blue-500 hover:text-blue-700 flex items-center">
                    <i data-feather="arrow-left"></i>
                    <span class="ml-2">Back to Admin</span>
                </a>
                <h1 class="text-2xl font-bold mt-4">Chat History for <?= htmlspecialchars($user['name']) ?></h1>
                <p class="text-gray-600"><?= htmlspecialchars($user['email']) ?></p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-4 gap-6">
                <!-- Session List -->
                <div class="lg:col-span-1 bg-white rounded-lg shadow p-4">
                    <h2 class="text-lg font-semibold mb-4">Chat Sessions</h2>
                    <div class="space-y-2">
                        <?php foreach ($sessions as $session): ?>
                            <a href="?user_id=<?= $user_id ?>&session=<?= $session ?>" 
                               class="block p-3 rounded-lg <?= $session == $selected_session ? 'bg-blue-100 text-blue-700' : 'hover:bg-gray-100' ?>">
                                Session #<?= $session ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>

                <!-- Chat Messages -->
                <div class="lg:col-span-3 bg-white rounded-lg shadow p-6">
                    <div class="chat-container">
                        <?php if (empty($messages)): ?>
                            <p class="text-gray-500">No messages found in this session.</p>
                        <?php else: ?>
                            <?php foreach ($messages as $msg): ?>
                                <div class="<?= $msg['type'] === 'user' ? 'user-message' : 'bot-message' ?>">
                                    <p><?= htmlspecialchars($msg['message']) ?></p>
                                    <p class="text-xs text-gray-500 mt-1">
                                        <?= date('M j, Y g:i A', strtotime($msg['timestamp'])) ?>
                                    </p>
                                </div>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </main>
    </div>

    <script>
        feather.replace();
    </script>
    <script src="script.js"></script>
</body>
</html>
```