From 75e72784370b75c343e9b2bed2048a53e1f9151f Mon Sep 17 00:00:00 2001
From: CXM <chenzixin202209@163.com>
Date: Sun, 14 Apr 2024 14:09:51 +0100
Subject: [PATCH] create chat history management function

---
 Admin/admin_page.php          |  1 +
 Admin/chat_history_manage.php | 76 +++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 Admin/chat_history_manage.php

diff --git a/Admin/admin_page.php b/Admin/admin_page.php
index f947c15..80f9cf4 100644
--- a/Admin/admin_page.php
+++ b/Admin/admin_page.php
@@ -29,6 +29,7 @@ if (!isset($_SESSION['user_type'])) {
             <li><a href="#" data-src="QA_manage.php" class="block px-5 py-3 hover:bg-gray-700">Question and answer management</a></li>
             <li><a href="#" data-src="fb_manage.php" class="block px-5 py-3 hover:bg-gray-700">Feedback management</a></li>
             <li><a href="#" data-src="bus_manage.php" class="block px-5 py-3 hover:bg-gray-700">Bus line management</a></li>
+            <li><a href="#" data-src="chat_history_manage.php" class="block px-5 py-3 hover:bg-gray-700">Chat History management</a></li>
         </ul>
     </div>
     <!-- 主内容区 -->
diff --git a/Admin/chat_history_manage.php b/Admin/chat_history_manage.php
new file mode 100644
index 0000000..b6fdc71
--- /dev/null
+++ b/Admin/chat_history_manage.php
@@ -0,0 +1,76 @@
+<?php
+include_once '../Logic/config.php';
+
+// 准备SQL查询
+$stmt = DB->prepare("SELECT * FROM chat_message");
+$stmt->execute();
+$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
+
+function deleteMessage($id)
+{
+    $sql = "DELETE FROM chat_message WHERE message_id = ?";
+    $stmt = DB->prepare($sql);
+    return $stmt->execute([$id]);
+}
+
+if ($_SERVER['REQUEST_METHOD'] === 'POST') {
+
+    // 处理表单提交的数据
+    if ($_POST["formType"] == "delMessageForm") {
+        $id = $_POST['id'];
+
+        deleteMessage($id);
+        echo "<script>window.location.href ='../Admin/chat_history_manage.php';</script>";
+    }
+
+}
+?>
+<!DOCTYPE html>
+<html lang="zh-CN">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Chat History Management</title>
+    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
+</head>
+<body class="bg-gray-100">
+<div class="container mx-auto p-4">
+    <table class="min-w-full max-w-4xl mx-auto border-collapse">
+        <thead class="bg-gray-800 text-white">
+        <tr class="border-b">
+            <th class="px-4 py-2 text-left rounded-tl-lg">Student ID</th>
+            <th class="px-4 py-2 text-left">Message</th>
+            <th class="px-4 py-2 text-left rounded-tr-lg">Operations</th>
+        </tr>
+        </thead>
+        <tbody>
+        <?php foreach ($messages as $message) { ?>
+            <tr class="border-b">
+                <td class="px-4 py-2"><?php echo htmlspecialchars($message['sender_id']) ?></td>
+                <td class="px-4 py-2"><?php echo htmlspecialchars($message['message']) ?></td>
+                <td class="px-4 py-2 text-center">
+                    <!-- 删除按钮 -->
+                    <form action="../Admin/chat_history_manage.php" method="post">
+                        <input type="hidden" name="id"
+                               value="<?php echo htmlspecialchars($message['message_id']) ?>">
+                        <input type="hidden" name="formType" value="delMessageForm">
+                        <button type="submit" class="text-red-500 hover:text-red-700 mx-2">
+                            <i class="fas fa-trash-alt"></i>
+                        </button>
+                    </form>
+                </td>
+            </tr>
+        <?php } ?>
+        </tbody>
+    </table>
+</div>
+
+<div class="fixed bottom-0 right-0 p-4">
+    <button onclick="window.location.reload()"
+            class="px-4 py-2 bg-gray-800 text-white rounded-md shadow-md hover:bg-gray-900 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-opacity-50">
+        <i class="fas fa-sync-alt mr-2"></i>
+    </button>
+</div>
+</body>
+</html>
-- 
GitLab