<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// 检查是否已登录，但排除登录页面本身
$current_script = basename($_SERVER['SCRIPT_NAME']);
if ($current_script != 'login.php' && (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in'])) {
    header('Location: ' . SITE_URL . '/login.php');
    exit;
}

// 获取所有分类
if ($current_script != 'login.php') {
    $conn = db_connect();
    $categories = $conn->query("SELECT * FROM categories ORDER BY sort_order ASC, id ASC");
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $page_title ?? '导航管理系统'; ?></title>
    <link href="../assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="../assets/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            min-height: 100vh;
            background-color: #2c3338;
            padding-top: 1rem;
        }
        .nav-link {
            color: #b4b4b4;
            padding: 0.5rem 1rem;
            transition: all 0.2s;
        }
        .nav-link:hover {
            color: #fff;
            background-color: rgba(255,255,255,0.1);
        }
        .nav-link.active {
            color: #fff;
            background-color: #0d6efd;
        }
        .submenu {
            margin-left: 1.5rem;
            display: none;
        }
        .submenu.show {
            display: block;
        }
        .nav-item.has-submenu > .nav-link::after {
            content: '\f107';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            margin-left: 0.5rem;
            transition: transform 0.2s;
        }
        .nav-item.has-submenu.open > .nav-link::after {
            transform: rotate(180deg);
        }
        .submenu .nav-link {
            padding: 0.25rem 1rem;
            font-size: 0.9rem;
        }
        .submenu .nav-link.active {
            background-color: rgba(13,110,253,0.5);
        }
    </style>
</head>
<body>
<?php if ($current_script != 'login.php'): ?>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 px-0 sidebar">
                <div class="text-white p-3">
                    <h5>导航管理系统</h5>
                </div>
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link <?php echo (!isset($active_page) || $active_page == 'dashboard') ? 'active' : ''; ?>" href="<?php echo SITE_URL; ?>/index.php">
                            控制面板
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link <?php echo $active_page == 'category' ? 'active' : ''; ?>" href="<?php echo SITE_URL; ?>/category.php">
                            分类管理
                        </a>
                    </li>
                    <li class="nav-item has-submenu <?php echo $active_page == 'link' ? 'open' : ''; ?>">
                        <a class="nav-link" href="javascript:void(0);">
                            链接管理
                        </a>
                        <ul class="nav flex-column submenu <?php echo $active_page == 'link' ? 'show' : ''; ?>">
                            <?php 
                            if ($categories) {
                                while ($category = $categories->fetch_assoc()): 
                            ?>
                            <li class="nav-item">
                                <a class="nav-link" href="<?php echo SITE_URL; ?>/link.php?category=<?php echo $category['id']; ?>">
                                    <?php echo htmlspecialchars($category['name']); ?>
                                </a>
                            </li>
                            <?php 
                                endwhile;
                                $categories->data_seek(0); // 重置结果集指针，以便其他地方还能使用
                            }
                            ?>
                        </ul>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link <?php echo $active_page == 'settings' ? 'active' : ''; ?>" href="<?php echo SITE_URL; ?>/settings.php">
                            网站设置
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link <?php echo $active_page == 'password' ? 'active' : ''; ?>" href="<?php echo SITE_URL; ?>/password.php">
                            修改密码
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?php echo SITE_URL; ?>/logout.php">
                            退出登录
                        </a>
                    </li>
                </ul>
            </div>
            <!-- 主要内容区域 -->
            <div class="col-md-10 p-4">
                <?php if (isset($page_title)): ?>
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h4 class="mb-0"><?php echo $page_title; ?></h4>
                </div>
                <?php endif; ?>
                <!-- 顶部导航栏 -->
                <nav class="navbar navbar-expand-md navbar-light bg-light mb-4 rounded">
                    <div class="container-fluid">
                        <button class="navbar-toggler d-md-none" type="button" data-bs-toggle="collapse" data-bs-target="#sidebarMenu">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="d-flex align-items-center">
                            <button class="btn btn-link me-3" id="theme-toggle">
                            </button>
                        </div>
                    </div>
                </nav>

                <script src="../assets/js/bootstrap.bundle.min.js"></script>
                <script>
                document.addEventListener('DOMContentLoaded', function() {
                    // 处理子菜单的展开/收起
                    document.querySelectorAll('.has-submenu > .nav-link').forEach(link => {
                        link.addEventListener('click', function(e) {
                            e.preventDefault();
                            const item = this.parentElement;
                            const submenu = item.querySelector('.submenu');
                            item.classList.toggle('open');
                            submenu.classList.toggle('show');
                        });
                    });
                });
                </script>

<?php endif; ?>
