<?php
// 安全检查
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: login.php');
    exit;
}

// 获取当前文件名
$current_file = basename($_SERVER['PHP_SELF']);

// 获取统计数据
$total_announcements = isset($config['announcements']) ? count($config['announcements']) : 0;
$total_links = isset($config['friend_links']) ? count($config['friend_links']) : 0;
$total_navs = isset($config['custom_nav']) ? count($config['custom_nav']) : 0;
$total_custom_data = isset($config['custom_data']) ? count($config['custom_data']) : 0;
?>
<!-- 侧边栏 -->
<div class="col-md-2 sidebar p-0 position-fixed">
    <div class="d-flex flex-column h-100">
        <!-- 用户信息 -->
        <div class="text-center mb-4 pb-3 border-bottom px-3 pt-3">
            <div class="bg-primary rounded-circle d-inline-flex align-items-center justify-content-center mb-2" style="width: 60px; height: 60px;">
                <i class="bi bi-person-fill text-white fs-4"></i>
            </div>
            <h6 class="text-white mb-1"><?php echo $_SESSION['admin_username']; ?></h6>
            <small class="text-muted">管理员</small>
        </div>

        <!-- 导航菜单 -->
        <div class="flex-grow-1 px-3">
            <ul class="nav nav-pills flex-column mb-auto">
                <li class="nav-item mb-2">
                    <a href="index.php" class="nav-link <?php echo $current_file == 'index.php' ? 'active' : ''; ?>">
                        <i class="bi bi-speedometer2"></i> 后台首页
                    </a>
                </li>
                <li class="nav-item mb-2">
                    <a href="settings.php" class="nav-link <?php echo $current_file == 'settings.php' ? 'active' : ''; ?>">
                        <i class="bi bi-gear"></i> 系统设置
                    </a>
                </li>
                <li class="nav-item mb-2">
                    <a href="navigation.php" class="nav-link <?php echo $current_file == 'navigation.php' ? 'active' : ''; ?>">
                        <i class="bi bi-list-ul"></i> 导航管理
                        <span class="badge bg-primary float-end"><?php echo $total_navs; ?></span>
                    </a>
                </li>
                <li class="nav-item mb-2">
                    <a href="custom_data.php" class="nav-link <?php echo $current_file == 'custom_data.php' ? 'active' : ''; ?>">
                        <i class="bi bi-database"></i> 自定义数据
                        <span class="badge bg-info float-end"><?php echo $total_custom_data; ?></span>
                    </a>
                </li>
                <li class="nav-item mb-2">
                    <a href="links.php" class="nav-link <?php echo $current_file == 'links.php' ? 'active' : ''; ?>">
                        <i class="bi bi-link-45deg"></i> 友情链接
                        <span class="badge bg-success float-end"><?php echo $total_links; ?></span>
                    </a>
                </li>
                <li class="nav-item mb-2">
                    <a href="announcements.php" class="nav-link <?php echo $current_file == 'announcements.php' ? 'active' : ''; ?>">
                        <i class="bi bi-megaphone"></i> 公告管理
                        <span class="badge bg-warning float-end"><?php echo $total_announcements; ?></span>
                    </a>
                </li>
            </ul>
        </div>

        <!-- 快速操作 -->
        <div class="mt-3 mb-4 px-3">
            <h6 class="text-white small mb-2">快速操作</h6>
            <div class="d-grid gap-2">
                <a href="../" target="_blank" class="btn btn-outline-light btn-sm">
                    <i class="bi bi-eye"></i> 查看网站
                </a>
                <a href="settings.php?tab=ads" class="btn btn-outline-light btn-sm">
                    <i class="bi bi-arrow-repeat"></i> 广告管理
                </a>
            </div>
        </div>

        <!-- 底部信息 -->
        <div class="px-3 pb-3 border-top">
            <div class="text-center mb-3">
                <div class="btn-group w-100">
                    <a href="https://www.wenshay.cn/" target="_blank" class="btn btn-outline-light btn-sm">
                        <i class="bi bi-hdd"></i> 云主机
                    </a>
                    <a href="https://www.wsbjw.cn/" target="_blank" class="btn btn-outline-light btn-sm">
                        <i class="bi bi-journal-text"></i> 站长笔记
                    </a>
                </div>
            </div>
                        <small class="text-muted">
                <div class="mb-2">
                    <span class="text-white"><strong>系统版本:</strong> 文煞CMS(api版) v 2.0</span>
                </div>
                <div class="mb-2">
                    <span class="text-white"><strong>最后更新:</strong> 2025-10-20</span>
                </div>
                <div class="mb-2">
                    <span class="text-warning"> <strong>版本授权：</strong> 免费开源版</span>
                </div>
                <div class="text-center">
                    <small class="text-white">Powered By </small><a href="https://www.wenshay.cn/" target="_blank" class="text-decoration-none">
                        <small class="text-white">文煞CMS  © 2025</small><br/><br/><br/><br/><br/><br/><br/><br/>
                    </a>
                </div>
            </small>
        </div>
    </div>
</div>

<style>
.sidebar {
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
    color: white;
    height: 100vh;
    left: 0;
    z-index: 1000;
    overflow-y: auto;
    margin-left: 0 !important;
}

/* 移除Bootstrap可能带来的左侧间距 */
.sidebar * {
    margin-left: 0;
}

.nav-link {
    color: rgba(255,255,255,0.8) !important;
    border-radius: 0.5rem;
    padding: 0.75rem 1rem;
    transition: all 0.3s ease;
    border: none;
}

.nav-link:hover {
    background: rgba(255,255,255,0.1) !important;
    color: white !important;
    transform: translateX(5px);
}

.nav-link.active {
    background: rgba(52, 152, 219, 0.8) !important;
    color: white !important;
    box-shadow: 0 4px 15px rgba(52, 152, 219, 0.3);
}

.badge {
    font-size: 0.7rem;
    padding: 0.25em 0.5em;
}

.border-top {
    border-color: rgba(255,255,255,0.1) !important;
}

.btn-outline-light {
    border-color: rgba(255,255,255,0.3);
    color: rgba(255,255,255,0.8);
}

.btn-outline-light:hover {
    background: rgba(255,255,255,0.1);
    color: white;
    border-color: rgba(255,255,255,0.5);
}

/* 自定义滚动条 */
.sidebar::-webkit-scrollbar {
    width: 4px;
}

.sidebar::-webkit-scrollbar-track {
    background: rgba(255,255,255,0.1);
}

.sidebar::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.3);
    border-radius: 2px;
}

.sidebar::-webkit-scrollbar-thumb:hover {
    background: rgba(255,255,255,0.5);
}

/* 确保主内容区域有正确的左边距 */
.col-md-10 {
    margin-left: 16.666667%;
}

/* 响应式调整 */
@media (max-width: 768px) {
    .sidebar {
        position: relative;
        height: auto;
        margin-bottom: 1rem;
    }
    
    .col-md-10 {
        margin-left: 0;
    }
}
</style>
