<?php
// gg.php - 公告管理页面

// 数据库连接信息
$servername = "localhost";
$dbUsername = "a1sax1m9i";
$dbPassword = "a1sax1m9i";
$dbName = "a1sax1m9i";

// 创建连接
$conn = new mysqli($servername, $dbUsername, $dbPassword, $dbName);
if ($conn->connect_error) {
    die("数据库连接失败: " . $conn->connect_error);
}
$conn->set_charset("utf8mb4");

$message = "";
$messageType = "";

// 处理表单提交
/*
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['action']) && $_POST['action'] == 'add_announcement') {
    $nr = trim($_POST["nr"] ?? "");
    
    if (empty($nr)) {
        $message = "公告内容不能为空！";
        $messageType = "error";
    } else {
        $stmt = $conn->prepare("INSERT INTO announcements (nr) VALUES (?)");
        $stmt->bind_param("s", $nr);

        if ($stmt->execute()) {
            $message = "公告发布成功！";
            $messageType = "success";
            header("Location: " . $_SERVER['PHP_SELF']);
            exit;
        } else {
            $message = "发布失败: " . $conn->error;
            $messageType = "error";
        }
        $stmt->close();
    }
}*/

// 查询所有公告
$announcements = [];
$ggResult = $conn->query("SELECT id, nr, time FROM announcements ORDER BY id DESC");
while($row = $ggResult->fetch_assoc()) {
    $announcements[] = $row;
}

$conn->close();
?>
<!DOCTYPE html>
<html lang="zh_cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>历史公告</title>
    <link rel="icon" href="./static/icon/icon.png" type="image/png">
    <link rel="alternate icon" href="./static/icon/icon.ico" type="image/x-icon">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @font-face {
            font-family: "MyCustomFont";
            src: url("./static/font/bbc.ttf") format("truetype");
            font-weight: normal;
            font-style: normal;
        }
        
        :root {
            --main-font: "MyCustomFont", sans-serif; /* 自定义字体变量 */
        }
        
        body {font-family: var(--main-font) /* 应用自定义字体 */, sans-serif; background-color: #f5f5f7; color: #333; margin: 0; padding: 20px; }
        .container { max-width: 800px; margin: 0 auto; background-color: #fff; padding: 20px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
        h1, h2 { color: #2c3e50; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 8px; font-weight: bold; }
        textarea.form-control { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 4px; box-sizing: border-box; min-height: 120px; }
        .submit-btn { background-color: #2c3e50; color: white; border: none; padding: 10px 20px; border-radius: 4px; cursor: pointer; font-size: 16px; }
        .submit-btn:hover { background-color: #34495e; }
        .announcement-list { margin-top: 30px; }
        .announcement-item { padding: 15px; border-bottom: 1px solid #eee; }
        .announcement-item:last-child { border-bottom: none; }
        .announcement-meta { color: #777; font-size: 14px; margin-bottom: 8px; }
        .announcement-content { line-height: 1.6; }
        .back-link { display: inline-block; margin-top: 20px; color: #2c3e50; text-decoration: none; }
        .back-link:hover { text-decoration: underline; }
        .message { padding: 10px; margin-bottom: 15px; border-radius: 4px; }
        .success { background-color: #d4edda; color: #155724; }
        .error { background-color: #f8d7da; color: #721c24; }
    </style>
</head>
<body>
    <div class="container">
        <h1>历史公告</h1>

        <!-- 发布公告表单 -->
        <!--<div class="recommend-container">
            <h2>发布新公告</h2>
            <?php if (!empty($message)): ?>
                <div class="message <?php echo $messageType; ?>">
                    <?php echo $message; // 提示信息仍用纯文本 ?>
                </div>
            <?php endif; ?>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                <input type="hidden" name="action" value="add_announcement">
                <div class="form-group">
                    <label for="nr">公告内容 (支持HTML)</label>
                    <textarea id="nr" name="nr" class="form-control" placeholder="请输入公告内容，例如：<b>加粗</b>、<a href='https://shanwogou.cn'>链接</a>..." required></textarea>
                </div>
                <button type="submit" class="submit-btn">发布公告</button>
            </form>
        </div>-->

        <!-- 历史公告列表 -->
        <div class="announcement-list">
            <h2>历史公告</h2>
            <?php if (empty($announcements)): ?>
                <p>暂无历史公告。</p>
            <?php else: ?>
                <?php foreach($announcements as $ann): ?>
                    <div class="announcement-item">
                        <div class="announcement-meta">
                            <span>ID: <?php echo $ann['id']; ?></span> | 
                            <span>发布时间: <?php echo $ann['time']; ?></span>
                        </div>
                        <div class="announcement-content">
                            <?php
                                // 核心修改：移除 htmlspecialchars，让HTML标签生效
                                // 保留 nl2br 以正确处理换行符
                                echo nl2br($ann['nr']); 
                            ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
        
        <a href="index.php" class="back-link"><i class="fas fa-arrow-left"></i> 返回首页</a>
    </div>
</body>
</html>