<?php
require_once __DIR__ . '/../db/config.php';
session_start();

// Fetch site settings
try {
    $stmt = db()->query("SELECT key_name, key_value FROM settings");
    $settings = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);
} catch (Exception $e) {
    $settings = [];
}

$site_name = $settings['site_name'] ?? '豪软世界';
$tg_link = $settings['tg_link'] ?? ($settings['tg_support'] ?? 'https://t.me/zhangshihao818');
$site_logo = $settings['site_logo'] ?? 'assets/pasted-20260208-082111-302e08e2.png';
$site_description = $settings['site_description'] ?? '豪软世界，为您提供最优质的软件授权与数字化服务。';

// Theme colors
$primary_color = $settings['primary_color'] ?? '#ff3399';
$accent_color = $settings['accent_color'] ?? '#0088cc';
$mobile_primary = $settings['mobile_primary_color'] ?? '#e1251b';
$mobile_accent = $settings['mobile_accent_color'] ?? '#ff9900';
?>
<!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 $site_name; ?> - 全球专业软件服务商</title>
    <meta name="description" content="<?php echo htmlspecialchars($site_description); ?>">
    
    <!-- Favicon -->
    <link rel="icon" type="image/png" href="<?php echo $site_logo; ?>">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;800&display=swap" rel="stylesheet">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
    
    <style>
        :root {
            --primary-color: <?php echo $primary_color; ?>;
            --accent-color: <?php echo $accent_color; ?>;
            --mobile-primary: <?php echo $mobile_primary; ?>;
            --mobile-accent: <?php echo $mobile_accent; ?>;
        }
        .site-logo-img {
            height: 40px;
            width: auto;
            margin-right: 12px;
            border-radius: 8px;
        }
        @media (max-width: 768px) {
            .site-logo-img { height: 30px; }
            .navbar-brand { font-size: 1.1rem !important; }
        }
    </style>
</head>
<body>

<!-- PC Navbar -->
<nav class="navbar navbar-expand-lg sticky-top navbar-light d-none d-lg-block">
    <div class="container">
        <a class="navbar-brand d-flex align-items-center" href="index.php">
            <img src="<?php echo $site_logo; ?>" alt="Logo" class="site-logo-img">
            <?php echo $site_name; ?>
        </a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                    <a class="nav-link" href="index.php">网站首页</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="orders.php">订单查询</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="help.php">帮助中心</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="faq.php">常见问题</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="<?php echo $tg_link; ?>" target="_blank">联系客服</a>
                </li>
            </ul>
            <form class="d-flex search-container me-3" action="search.php" method="GET">
                <input class="form-control me-2" type="search" name="q" placeholder="搜索软件、账号..." aria-label="Search">
                <button class="btn btn-outline-primary" type="submit"><i class="bi bi-search"></i></button>
            </form>
            <div class="d-flex align-items-center">
                <a href="cart.php" class="btn btn-outline-primary position-relative me-3">
                    <i class="bi bi-cart3"></i>
                    <span id="cart-badge" class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger d-none">
                        0
                    </span>
                </a>
                <?php if (isset($_SESSION['admin_logged_in'])): ?>
                    <a href="admin/index.php" class="btn btn-sm btn-outline-dark">后台管理</a>
                <?php else: ?>
                    <a href="login.php" class="btn btn-sm btn-outline-dark">登录</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</nav>

<!-- Mobile Header -->
<div class="d-lg-none sticky-top" style="background: var(--mobile-primary); padding: 10px 0; z-index: 1040;">
    <div class="container">
        <div class="d-flex justify-content-between align-items-center mb-2">
            <a class="navbar-brand d-flex align-items-center text-white" href="index.php">
                <img src="<?php echo $site_logo; ?>" alt="Logo" class="site-logo-img" style="filter: brightness(0) invert(1);">
                <span class="text-white"><?php echo $site_name; ?></span>
            </a>
            <div class="d-flex gap-3">
                <a href="cart.php" class="text-white position-relative">
                    <i class="bi bi-cart3 fs-4"></i>
                    <span id="cart-badge-mobile" class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-warning text-dark d-none" style="font-size: 0.6rem;">
                        0
                    </span>
                </a>
            </div>
        </div>
        <form action="search.php" method="GET">
            <div class="mobile-search-bar">
                <i class="bi bi-search text-muted"></i>
                <input type="search" name="q" placeholder="搜索您需要的商品...">
            </div>
        </form>
    </div>
</div>

<div class="container mt-4">
    <?php if (isset($settings['notice']) && !empty($settings['notice'])): ?>
    <div class="alert glass-card mb-4 border-0 py-2 bg-white shadow-sm overflow-hidden" role="alert">
        <div class="d-flex align-items-center">
            <i class="bi bi-megaphone-fill me-3 fs-5" style="color: var(--primary-color);"></i>
            <div class="announcement-container flex-grow-1">
                <div class="announcement-content text-dark fw-bold">
                    <?php echo $settings['notice']; ?> 
                    <?php if (!empty($settings['tg_channel'])): ?>
                        &nbsp;&nbsp;&nbsp;&nbsp;官方频道：<?php echo $settings['tg_channel']; ?>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    <?php endif; ?>