<?php
require_once __DIR__ . '/auth.php';
require_login();
$admin = $_SESSION['admin_user'];
$pdo = db();

// Fetch Website Settings for Logo & Favicon
$stmt = $pdo->query("SELECT * FROM website_settings");
$settings = [];
while ($row = $stmt->fetch()) {
    $settings[$row['key']] = $row['value'];
}
$site_logo = $settings['site_logo'] ?? 'assets/pasted-20260226-073317-a8105f30.png';
$site_name = $settings['site_name'] ?? '智域科技';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= htmlspecialchars($site_name) ?> - 后台管理系统</title>
    <!-- Favicon -->
    <link rel="icon" href="../<?= htmlspecialchars($site_logo) ?>" type="image/x-icon">
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <!-- Google Fonts: Ma Shan Zheng -->
    <link href="https://fonts.googleapis.com/css2?family=Ma+Shan+Zheng&display=swap" rel="stylesheet">
    <style>
        :root {
            --sidebar-bg: #ffffff;
            --sidebar-text: #4b5563;
            --sidebar-active-bg: #f3f4f6;
            --sidebar-active-text: #0d6efd;
            --sidebar-active-border: #0d6efd;
            --main-bg: #f8f9fa;
            --card-border-radius: 12px;
        }

        body {
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: var(--main-bg);
            margin: 0;
            display: flex;
            min-height: 100vh;
        }

        /* Sidebar Style */
        .sidebar {
            width: 260px;
            background-color: var(--sidebar-bg);
            color: var(--sidebar-text);
            flex-shrink: 0;
            display: flex;
            flex-direction: column;
            position: sticky;
            top: 0;
            height: 100vh;
            z-index: 1000;
            transition: all 0.3s;
            border-right: 1px solid #edf2f9;
        }

        .sidebar-header {
            padding: 24px;
            border-bottom: 1px solid #edf2f9;
            display: flex;
            align-items: center;
        }

        .sidebar-brand {
            font-family: 'Ma Shan Zheng', cursive;
            font-size: 1.5rem;
            color: #333;
            text-decoration: none;
            display: flex;
            align-items: center;
        }

        .sidebar-brand span:nth-child(1) { color: #3b82f6; }
        .sidebar-brand span:nth-child(2) { color: #10b981; }
        .sidebar-brand span:nth-child(3) { color: #f59e0b; }
        .sidebar-brand span:nth-child(4) { color: #ef4444; }

        .sidebar-nav {
            padding: 16px 0;
            flex-grow: 1;
            overflow-y: auto;
        }

        .nav-item {
            list-style: none;
            padding: 4px 16px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            color: var(--sidebar-text);
            text-decoration: none;
            border-radius: 8px;
            transition: all 0.2s;
            font-size: 0.95rem;
        }

        .nav-link i {
            width: 20px;
            margin-right: 12px;
            font-size: 1.1rem;
            text-align: center;
        }

        .nav-link:hover {
            background-color: #f9fafb;
            color: var(--sidebar-active-text);
        }

        .nav-link.active {
            background-color: var(--sidebar-active-bg);
            color: var(--sidebar-active-text);
            font-weight: 600;
        }

        .nav-link.active i {
            color: var(--sidebar-active-border);
        }

        .nav-group-title {
            padding: 16px 32px 8px;
            font-size: 0.75rem;
            text-transform: uppercase;
            letter-spacing: 1px;
            color: #9ca3af;
        }

        /* Main Content */
        .content-wrapper {
            flex-grow: 1;
            display: flex;
            flex-direction: column;
            overflow-x: hidden;
        }

        .top-navbar {
            height: 70px;
            background: #fff;
            padding: 0 24px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #edf2f9;
            position: sticky;
            top: 0;
            z-index: 999;
        }

        .main-content {
            padding: 24px;
        }

        /* UI Components */
        .card {
            border: none;
            border-radius: var(--card-border-radius);
            box-shadow: 0 0.75rem 1.5rem rgba(18, 38, 63, 0.03);
            margin-bottom: 24px;
        }

        .card-header {
            background-color: transparent;
            border-bottom: 1px solid #edf2f9;
            padding: 16px 24px;
            font-weight: 600;
        }

        .btn-primary {
            background-color: #0d6efd;
            border-color: #0d6efd;
            padding: 8px 20px;
            border-radius: 8px;
        }

        .table thead th {
            background-color: #f8f9fa;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.75rem;
            letter-spacing: 0.5px;
            color: #7c8db5;
            padding: 12px 24px;
        }

        .table tbody td {
            padding: 16px 24px;
            vertical-align: middle;
        }

        .badge-soft-success { background-color: #d1e7dd; color: #0f5132; }
        .badge-soft-primary { background-color: #cfe2ff; color: #084298; }

        /* Animation */
        .fade-in {
            animation: fadeIn 0.5s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(10px); }
            to { opacity: 1; transform: translateY(0); }
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <div class="sidebar-header">
            <a href="index.php" class="sidebar-brand">
                <img src="../<?= htmlspecialchars($site_logo) ?>" alt="Logo" height="30" class="me-2">
                <span>智</span><span>域</span><span>科</span><span>技</span>
            </a>
        </div>
        <div class="sidebar-nav">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a href="index.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'index.php' ? 'active' : '' ?>">
                        <i class="fas fa-chart-line"></i> 仪表盘
                    </a>
                </li>
                
                <li class="nav-group-title">内容管理</li>
                <li class="nav-item">
                    <a href="cases.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'cases.php' ? 'active' : '' ?>">
                        <i class="fas fa-briefcase"></i> 声誉作品 (案例)
                    </a>
                </li>
                <li class="nav-item">
                    <a href="services.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'services.php' ? 'active' : '' ?>">
                        <i class="fas fa-cube"></i> 核心能力 (服务)
                    </a>
                </li>
                <li class="nav-item">
                    <a href="tech_stack.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'tech_stack.php' ? 'active' : '' ?>">
                        <i class="fas fa-layer-group"></i> 技术底座
                    </a>
                </li>
                <li class="nav-item">
                    <a href="pricing.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'pricing.php' ? 'active' : '' ?>">
                        <i class="fas fa-tags"></i> 报价方案
                    </a>
                </li>
                <li class="nav-item">
                    <a href="transformations.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'transformations.php' ? 'active' : '' ?>">
                        <i class="fas fa-rocket"></i> 转型实践
                    </a>
                </li>
                
                <li class="nav-group-title">咨询与联系</li>
                <li class="nav-item">
                    <a href="leads.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'leads.php' ? 'active' : '' ?>">
                        <i class="fas fa-headset"></i> 咨询记录 (Leads)
                    </a>
                </li>
                <li class="nav-item">
                    <a href="settings.php?group=contact" class="nav-link <?= ($_GET['group'] ?? '') == 'contact' ? 'active' : '' ?>">
                        <i class="fas fa-address-book"></i> 联系方式
                    </a>
                </li>

                <li class="nav-group-title">系统管理</li>
                <li class="nav-item">
                    <a href="settings.php?group=general" class="nav-link <?= ($_GET['group'] ?? '') == 'general' ? 'active' : '' ?>">
                        <i class="fas fa-cog"></i> 基础设置
                    </a>
                </li>
                <li class="nav-item">
                    <a href="settings.php?group=seo" class="nav-link <?= ($_GET['group'] ?? '') == 'seo' ? 'active' : '' ?>">
                        <i class="fas fa-search"></i> SEO设置
                    </a>
                </li>
                <li class="nav-item">
                    <a href="admins.php" class="nav-link <?= basename($_SERVER['PHP_SELF']) == 'admins.php' ? 'active' : '' ?>">
                        <i class="fas fa-users-cog"></i> 管理员
                    </a>
                </li>
            </ul>
        </div>
        <div class="sidebar-footer p-3 border-top">
            <a href="logout.php" class="btn btn-outline-danger w-100 btn-sm">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>

    <div class="content-wrapper">
        <div class="top-navbar">
            <div class="d-flex align-items-center">
                <h4 class="mb-0 fw-bold"><?php 
                    $titles = [
                        'index.php' => '仪表盘',
                        'cases.php' => '案例管理',
                        'services.php' => '服务管理',
                        'tech_stack.php' => '技术底座',
                        'pricing.php' => '报价方案',
                        'transformations.php' => '转型实践',
                        'leads.php' => '咨询记录',
                        'settings.php' => '网站设置',
                        'admins.php' => '管理员管理',
                        'profile.php' => '个人资料'
                    ];
                    echo $titles[basename($_SERVER['PHP_SELF'])] ?? '后台管理';
                ?></h4>
            </div>
            <div class="d-flex align-items-center">
                <div class="dropdown">
                    <a href="#" class="d-flex align-items-center text-dark text-decoration-none dropdown-toggle" data-bs-toggle="dropdown">
                        <img src="../<?= htmlspecialchars($site_logo) ?>" alt="" width="32" height="32" class="rounded-circle me-2">
                        <strong><?= htmlspecialchars($admin['nickname'] ?? $admin['username']) ?></strong>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                        <li><a class="dropdown-item" href="profile.php"><i class="fas fa-user-edit me-2"></i> 个人资料</a></li>
                        <li><hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item text-danger" href="logout.php"><i class="fas fa-sign-out-alt me-2"></i> 退出登录</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="main-content fade-in">