<?php
if (session_status() === PHP_SESSION_NONE) session_start();
require_once __DIR__ . '/../db/config.php';
require_once __DIR__ . '/lang.php';

$user = null;
if (isset($_SESSION['user_id'])) {
    $stmt = db()->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->execute([$_SESSION['user_id']]);
    $user = $stmt->fetch();
    
    if ($user) {
        require_once __DIR__ . '/mining_helper.php';
        settleMining($user['id']);
    }
}

// site settings are fetched via getSetting() in db/config.php
?>
<!DOCTYPE html>
<html lang="<?= $lang ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <?php
    $site_logo = getSetting('site_logo', '/assets/images/logo.png');
    $site_favicon = getSetting('site_favicon', $site_logo);
    $site_name = getSetting('site_name', 'BYRO');
    $logo_v = time(); // Use timestamp for cache busting
    ?>
    <title><?= $site_name ?> | <?= __('site_title') ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link rel="stylesheet" href="/assets/css/style.css?v=<?= $logo_v ?>">
    <link rel="icon" href="<?= $site_favicon ?>?v=<?= $logo_v ?>">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;700;900&family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        window.notify = function(type, title, text) {
            Swal.fire({
                icon: type,
                title: title,
                text: text,
                timer: 3000,
                timerProgressBar: true,
                showConfirmButton: false
            });
        };
        window.APP_ROOT = '<?= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]" ?>';
        <?php
            $depth = substr_count(trim($_SERVER['PHP_SELF'], '/'), '/');
            $relRoot = str_repeat('../', $depth);
        ?>
        window.REL_PATH = '<?= $relRoot ?>';
    </script>
    <style>
        :root {
            --primary: #0062ff;
            --bg: #0b0e11;
            --surface: #1e2329;
            --text: #eaecef;
            --text-muted: #9ba3af;
            --border: #2b3139;
            --success: #26a69a;
            --danger: #ef5350;
        }
        body {
            background-color: var(--bg);
            color: var(--text);
            font-family: 'Noto Sans SC', 'Inter', system-ui, -apple-system, sans-serif;
            margin: 0;
            padding: 0;
        }
        header {
            height: 70px;
            display: flex;
            align-items: center;
            padding: 0 24px;
            border-bottom: 1px solid var(--border);
            position: sticky;
            top: 0;
            background: rgba(0,0,0,0.9);
            backdrop-filter: blur(10px);
            z-index: 1000;
        }
        .logo-container {
            display: flex;
            align-items: center;
            text-decoration: none;
            margin-right: 40px;
        }
        .logo-container img {
            height: 32px;
            width: auto;
            object-fit: contain;
            filter: drop-shadow(0 0 8px rgba(0, 194, 255, 0.5));
            transition: all 0.3s ease;
        }
        .logo-container:hover img {
            filter: drop-shadow(0 0 12px rgba(0, 194, 255, 0.8));
            transform: scale(1.05);
        }
        .logo-text {
            display: inline-block;
            font-size: 15px;
            font-weight: 900;
            color: #fff;
            letter-spacing: 1px;
            margin-left: 10px;
            text-transform: uppercase;
            vertical-align: middle;
            text-shadow: 0 0 10px rgba(0, 194, 255, 0.3);
        }
        nav {
            display: flex;
            gap: 24px;
            flex: 1;
        }
        @media (max-width: 992px) {
            nav {
                display: none;
            }
            .logo-container {
                margin-right: auto;
            }
            .profile-trigger span {
                display: none;
            }
            .profile-trigger {
                padding: 8px;
            }
        }
        nav a {
            color: var(--text);
            text-decoration: none;
            font-size: 15px;
            font-weight: 500;
            transition: color 0.2s;
        }
        nav a:hover {
            color: var(--primary);
        }
        .header-right {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        .lang-switcher {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
            position: relative;
            padding: 8px 12px;
            border-radius: 8px;
            border: 1px solid var(--border);
            background: rgba(255,255,255,0.05);
            transition: all 0.2s;
        }
        .lang-switcher:hover {
            background: rgba(255,255,255,0.1);
            border-color: var(--primary);
        }
        .flag-icon {
            width: 20px;
            height: 15px;
            object-fit: cover;
            border-radius: 2px;
        }
        .lang-dropdown {
            display: none;
            position: absolute;
            top: 100%;
            right: 0;
            background: #121212;
            border: 1px solid var(--border);
            border-radius: 12px;
            padding: 8px;
            min-width: 160px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.8);
            z-index: 1100;
            margin-top: 5px;
        }
        .lang-switcher:hover .lang-dropdown,
        .lang-dropdown:hover {
            display: block;
        }
        .lang-dropdown a {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 12px 16px;
            color: var(--text);
            text-decoration: none;
            font-size: 14px;
            border-radius: 8px;
            transition: background 0.2s;
        }
        .lang-dropdown a:hover {
            background: var(--primary);
            color: #fff;
        }
        .auth-btns a {
            font-size: 14px;
            text-decoration: none;
            color: var(--text);
            padding: 8px 16px;
            border-radius: 8px;
            font-weight: 600;
            transition: all 0.2s;
        }
        .btn-login:hover {
            background: rgba(255,255,255,0.05);
        }
        .btn-register {
            background: var(--primary);
            color: #fff !important;
            box-shadow: 0 4px 12px rgba(0, 98, 255, 0.2);
        }
        .btn-register:hover {
            background: #0056e0;
            transform: translateY(-1px);
            box-shadow: 0 6px 16px rgba(0, 98, 255, 0.3);
        }
        @media (max-width: 576px) {
            .header-right {
                gap: 8px;
            }
            .auth-btns {
                background: rgba(255,255,255,0.08);
                padding: 3px;
                border-radius: 12px;
                display: flex;
                align-items: center;
                gap: 2px;
                border: 1px solid rgba(255,255,255,0.1);
            }
            .auth-btns a {
                padding: 7px 14px;
                font-size: 13px;
                border-radius: 10px;
                font-weight: 700;
                transition: all 0.3s ease;
            }
            .btn-login {
                background: transparent;
                color: rgba(255,255,255,0.8) !important;
            }
            .btn-login:active {
                background: rgba(255,255,255,0.1);
            }
            .btn-register {
                background: var(--primary);
                color: #fff !important;
                box-shadow: 0 4px 12px rgba(0, 98, 255, 0.3);
            }
            .lang-switcher span {
                display: none;
            }
        }
        
        .user-center {
            position: relative;
        }
        .profile-trigger {
            display: flex;
            align-items: center;
            gap: 8px;
            color: var(--text);
            text-decoration: none;
            padding: 8px 12px;
            border-radius: 8px;
            transition: background 0.2s;
            border: 1px solid transparent;
        }
        .profile-trigger:hover {
            background: rgba(255,255,255,0.05);
            border-color: var(--border);
            color: var(--primary);
        }
        .user-dropdown {
            display: none;
            position: absolute;
            top: 100%;
            right: 0;
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            width: 260px;
            padding: 16px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.8);
            z-index: 1200;
            margin-top: 5px;
        }
        .user-center:hover .user-dropdown,
        .user-dropdown:hover {
            display: block;
        }
        .user-info-header {
            padding-bottom: 12px;
            border-bottom: 1px solid var(--border);
            margin-bottom: 12px;
        }
        .uid-tag {
            font-size: 12px;
            color: var(--text-muted);
            margin-bottom: 4px;
        }
        .user-stats {
            display: flex;
            flex-direction: column;
            gap: 8px;
            font-size: 13px;
        }
        .stat-item {
            display: flex;
            justify-content: space-between;
        }
        .stat-label { color: var(--text-muted); }
        .stat-value { color: var(--text); font-weight: 500; }
        .stat-value.success { color: var(--success); }
        
        .dropdown-links {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 8px;
            margin-top: 12px;
        }
        .dropdown-links a {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
            padding: 10px;
            background: rgba(255,255,255,0.05);
            border-radius: 8px;
            font-size: 13px;
            color: var(--text);
            text-decoration: none;
            transition: all 0.2s;
        }
        .dropdown-links a:hover {
            background: var(--primary);
            color: #fff;
        }
        .logout-link {
            display: block;
            text-align: center;
            margin-top: 16px;
            padding-top: 12px;
            border-top: 1px solid var(--border);
            color: var(--danger);
            text-decoration: none;
            font-size: 13px;
            font-weight: 600;
        }
    </style>
</head>
<body>
<header>
    <a href="/" class="logo-container">
        <img src="<?= $site_logo ?>?v=<?= $logo_v ?>" alt="<?= $site_name ?>">
        <span class="logo-text"><?= $site_name ?></span>
    </a>
    <nav>
        <a href="/"><?= __('home') ?></a>
        <a href="/market.php"><?= __('market') ?></a>
        <a href="/binary.php"><?= __('second_contract') ?></a>
        <a href="/trade.php"><?= __('spot') ?></a>
        <a href="/contract.php"><?= __('contract') ?></a>
        <a href="/mining.php"><?= __('mining') ?></a>
        <a href="/swap.php"><?= __('swap') ?></a>
        <a href="/app.php"><?= __('app_download') ?></a>
    </nav>
    <div class="header-right">
        <div class="lang-switcher">
            <?php if ($lang === 'zh'): ?>
                <img src="https://flagcdn.com/w40/cn.png" class="flag-icon" alt="CN">
                <span><?= __('chinese') ?></span>
            <?php else: ?>
                <img src="https://flagcdn.com/w40/us.png" class="flag-icon" alt="US">
                <span><?= __('english') ?></span>
            <?php endif; ?>
            <div class="lang-dropdown">
                <a href="?lang=zh">
                    <img src="https://flagcdn.com/w40/cn.png" class="flag-icon" alt="CN">
                    <?= __('chinese') ?>
                </a>
                <a href="?lang=en">
                    <img src="https://flagcdn.com/w40/us.png" class="flag-icon" alt="US">
                    <?= __('english') ?>
                </a>
            </div>
        </div>
        <?php if ($user): ?>
            <div class="user-center">
                <a href="/profile.php" class="profile-trigger">
                    <i class="bi bi-person-circle fs-5"></i>
                    <span><?= htmlspecialchars($user['username']) ?></span>
                </a>
                <div class="user-dropdown">
                    <div class="user-info-header">
                        <div class="uid-tag"><?= __('uid') ?>: <?= $user['uid'] ?? 'N/A' ?></div>
                        <div class="fw-bold"><?= htmlspecialchars($user['username']) ?></div>
                    </div>
                    <div class="user-stats">
                        <div class="stat-item">
                            <span class="stat-label"><?= __('real_name') ?></span>
                            <span class="stat-value <?= ($user['kyc_status'] ?? 0) == 2 ? 'success' : 'text-warning' ?>">
                                <?php 
                                    $statusMap = [0 => __('unverified'), 1 => __(0), 2 => __('verified'), 3 => __(4)];
                                    echo $statusMap[$user['kyc_status'] ?? 0] ?? __('unverified');
                                ?>
                            </span>
                        </div>
                        <div class="stat-item">
                            <span class="stat-label"><?= __('credit_score') ?></span>
                            <span class="stat-value text-primary"><?= $user['credit_score'] ?? 80 ?></span>
                        </div>
                        <?php 
                        $stmt = db()->prepare("SELECT available FROM user_balances WHERE user_id = ? AND symbol = 'USDT'");
                        $stmt->execute([$user['id']]);
                        $bal = $stmt->fetch();
                        ?>
                        <div class="stat-item">
                            <span class="stat-label"><?= __('assets') ?></span>
                            <span class="stat-value"><?= number_format($bal['available'] ?? 0, 2) ?> <?= __('USDT') ?></span>
                        </div>
                    </div>
                    <div class="dropdown-links">
                        <a href="/profile.php"><i class="bi bi-person"></i> <?= __('personal') ?></a>
                        <a href="/recharge.php"><i class="bi bi-wallet2"></i> <?= __('recharge') ?></a>
                        <a href="/withdraw.php"><i class="bi bi-cash-stack"></i> <?= __('withdraw') ?></a>
                        <a href="/orders.php"><i class="bi bi-list-check"></i> <?= __('orders') ?></a>
                    </div>
                    <a href="/auth/logout.php" class="logout-link"><?= __('logout') ?></a>
                </div>
            </div>
        <?php else: ?>
            <div class="auth-btns">
                <a href="/auth/login.php" class="btn-login"><?= __('login') ?></a>
                <a href="/auth/register.php" class="btn-register"><?= __('register') ?></a>
            </div>
        <?php endif; ?>
    </div>
</header>
</body>
</html>