<?php
session_start();
require_once '../config.php';
require_once 'functions.php'; // 引入后台公共函数

// 检查登录状态
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

$message = '';
$message_type = '';

// 检查配置文件权限
$permission_check = check_config_permission();
if ($permission_check !== true) {
    $message = $permission_check;
    $message_type = 'danger';
}

// 添加公告
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_announcement'])) {
    $new_announcement = [
        'title' => safe_input($_POST['title']),
        'content' => safe_input($_POST['content']),
        'time' => date('Y-m-d H:i:s')
    ];

    // 确保announcements数组存在
    if (!isset($config['announcements']) || !is_array($config['announcements'])) {
        $config['announcements'] = [];
    }
    
    array_unshift($config['announcements'], $new_announcement); // 新公告放在前面

    if (save_config($config)) {
        $message = '公告添加成功！';
        $message_type = 'success';
    } else {
        $message = '公告添加失败，请检查文件权限！';
        $message_type = 'danger';
    }
}

// 删除公告
if (isset($_GET['delete'])) {
    $index = intval($_GET['delete']);
    if (isset($config['announcements'][$index])) {
        array_splice($config['announcements'], $index, 1);
        if (save_config($config)) {
            $message = '公告删除成功！';
            $message_type = 'success';
        } else {
            $message = '公告删除失败，请检查文件权限！';
            $message_type = 'danger';
        }
    } else {
        $message = '公告不存在！';
        $message_type = 'danger';
    }
}

$page_title = '公告管理';
?>
<?php include 'header.php'; ?>

<div class="container-fluid">
    <div class="row">
        <?php include 'sidebar.php'; ?>

        <div class="col-md-10 p-4">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2 class="mb-0">公告管理</h2>
                <span class="badge bg-primary"><?php echo isset($config['announcements']) ? count($config['announcements']) : 0; ?> 条公告</span>
            </div>

            <?php if ($message): ?>
            <div class="alert alert-<?php echo $message_type; ?> alert-dismissible fade show" role="alert">
                <?php echo $message; ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
            <?php endif; ?>

            <style>
                .usage-guide {
                    background: #f8f9fa;
                    border: 1px solid #e9ecef;
                    border-radius: 8px;
                    padding: 1.5rem;
                    margin: 1.5rem 0;
                    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
                }
                .usage-guide h6 {
                    color: #2c3e50;
                    margin-bottom: 1rem;
                    font-weight: 600;
                    border-bottom: 2px solid #ffc107;
                    padding-bottom: 0.5rem;
                }
                .code-example {
                    background: #2c3e50;
                    color: #ecf0f1;
                    padding: 1rem;
                    border-radius: 6px;
                    font-family: 'Courier New', monospace;
                    font-size: 0.85rem;
                    margin: 0.75rem 0;
                    border-left: 4px solid #ffc107;
                }
                .announcement-card {
                    transition: transform 0.2s;
                    border: 1px solid #e9ecef;
                    border-radius: 10px;
                }
                .announcement-card:hover {
                    transform: translateY(-2px);
                    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
                }
                .announcement-item {
                    border-left: 4px solid #ffc107;
                    padding-left: 1rem;
                }
            </style>

            <div class="row">
                <div class="col-md-6">
                    <div class="card announcement-card">
                        <div class="card-header bg-primary text-white">
                            <i class="bi bi-plus-circle"></i> 添加公告
                        </div>
                        <div class="card-body">
                            <form method="POST">
                                <div class="mb-3">
                                    <label class="form-label">公告标题</label>
                                    <input type="text" class="form-control" name="title" placeholder="输入公告标题" required>
                                </div>
                                <div class="mb-3">
                                    <label class="form-label">公告内容</label>
                                    <textarea class="form-control" name="content" rows="5" placeholder="输入公告详细内容" required></textarea>
                                    <div class="form-text">支持HTML标签，可以设置公告的样式</div>
                                </div>
                                <button type="submit" name="add_announcement" class="btn btn-primary w-100">
                                    <i class="bi bi-check-lg"></i> 发布公告
                                </button>
                            </form>
                        </div>
                    </div>
                </div>

                <div class="col-md-6">
                    <div class="card announcement-card">
                        <div class="card-header bg-success text-white d-flex justify-content-between align-items-center">
                            <span><i class="bi bi-megaphone"></i> 公告列表</span>
                            <span class="badge bg-light text-dark"><?php echo isset($config['announcements']) ? count($config['announcements']) : 0; ?> 条</span>
                        </div>
                        <div class="card-body">
                            <?php if (empty($config['announcements'])): ?>
                            <div class="text-center py-4">
                                <i class="bi bi-megaphone display-1 text-muted"></i>
                                <p class="text-muted mt-3">暂无公告</p>
                                <small class="text-muted">点击左侧表单添加第一条公告</small>
                            </div>
                            <?php else: ?>
                            <div class="list-group list-group-flush">
                                <?php foreach ($config['announcements'] as $index => $announcement): ?>
                                <div class="list-group-item px-0 announcement-item">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div style="flex: 1;">
                                            <div class="d-flex align-items-center mb-2">
                                                <h6 class="mb-0 me-2"><?php echo $announcement['title']; ?></h6>
                                                <span class="badge bg-warning">最新</span>
                                            </div>
                                            <div class="mb-2">
                                                <p class="mb-1 text-muted"><?php echo nl2br(htmlspecialchars($announcement['content'])); ?></p>
                                            </div>
                                            <small class="text-muted">
                                                <i class="bi bi-clock"></i> <?php echo $announcement['time']; ?>
                                            </small>
                                        </div>
                                        <div class="ms-3">
                                            <a href="?delete=<?php echo $index; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除这个公告吗？')" title="删除公告">
                                                <i class="bi bi-trash"></i>
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <?php endforeach; ?>
                            </div>
                            <div class="mt-3 pt-3 border-top">
                                <small class="text-muted">
                                    <i class="bi bi-info-circle"></i> 
                                    最新公告会显示在网站侧边栏，最多显示<?php echo $config['display']['announcements_count'] ?? 5; ?>条公告
                                </small>
                            </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 使用说明放在功能后面 -->
            <div class="usage-guide">
                <h6>📝 公告管理使用说明</h6>
                <p><strong>模板调用方法：</strong></p>
                <div class="code-example">
                    {if $announcements}<br>
                    &nbsp;&nbsp;&lt;div class="announcements-section"&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&lt;h3&gt;网站公告&lt;/h3&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;{foreach $announcements as $announcement}<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;div class="announcement-item"&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h5&gt;{$announcement.title}&lt;/h5&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;{$announcement.content}&lt;/p&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;small class="text-muted"&gt;{$announcement.time}&lt;/small&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/div&gt;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;{/foreach}<br>
                    &nbsp;&nbsp;&lt;/div&gt;<br>
                    {/if}
                </div>
                
                <p><strong>公告显示设置：</strong></p>
                <div class="row">
                    <div class="col-md-6">
                        <ul class="list-unstyled">
                            <li><i class="bi bi-layout-sidebar text-primary"></i> <strong>显示位置</strong> - 默认在侧边栏显示</li>
                            <li><i class="bi bi-sort-numeric-down text-info"></i> <strong>显示顺序</strong> - 最新公告显示在最前面</li>
                        </ul>
                    </div>
                    <div class="col-md-6">
                        <ul class="list-unstyled">
                            <li><i class="bi bi-list-ol text-success"></i> <strong>显示数量</strong> - 最多显示5条公告</li>
                            <li><i class="bi bi-code-slash text-warning"></i> <strong>内容格式</strong> - 支持HTML标签</li>
                        </ul>
                    </div>
                </div>

                <p><strong>使用建议：</strong></p>
                <ul>
                    <li>公告标题要简洁明了，突出重点</li>
                    <li>公告内容可以包含重要通知、更新日志等</li>
                    <li>及时删除过期的公告，保持信息时效性</li>
                    <li>重要公告可以置顶显示</li>
                </ul>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdmirror.com/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>