<?php
$current_page = basename($_SERVER['PHP_SELF']);
require_once __DIR__ . '/../db/config.php';
$pdo_sidebar = db();
$settings_sidebar = $pdo_sidebar->query("SELECT setting_key, setting_value FROM settings")->fetchAll(PDO::FETCH_KEY_PAIR);
$site_name = $settings_sidebar['site_name'] ?? '全球接码';
$site_logo = $settings_sidebar['site_logo'] ?? 'assets/pasted-20260210-082628-83f66727.png';
?>
<style>
    .sidebar {
        width: 280px;
        height: 100vh;
        position: fixed;
        left: 0;
        top: 0;
        background: rgba(255, 255, 255, 0.9);
        backdrop-filter: blur(10px);
        border-right: 1px solid rgba(0, 0, 0, 0.05);
        z-index: 1000;
        padding: 2rem 1.2rem;
        display: flex;
        flex-direction: column;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    .sidebar-brand {
        padding: 0.5rem 1rem;
        margin-bottom: 2.5rem;
        display: flex;
        align-items: center;
        gap: 15px;
        text-decoration: none;
    }
    .sidebar-brand img {
        width: 42px;
        height: 42px;
        border-radius: 12px;
        object-fit: cover;
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    .sidebar-brand span {
        font-size: 1.25rem;
        font-weight: 800;
        background: linear-gradient(135deg, #1a1a1a 0%, #4a4a4a 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        letter-spacing: -0.5px;
    }
    .sidebar .nav-link {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 12px 18px;
        color: #64748b;
        text-decoration: none;
        font-weight: 600;
        border-radius: 14px;
        margin-bottom: 6px;
        transition: all 0.2s ease;
        position: relative;
    }
    .sidebar .nav-link i {
        font-size: 1.2rem;
        width: 24px;
        text-align: center;
        transition: transform 0.2s ease;
    }
    .sidebar .nav-link:hover {
        background-color: #f8fafc;
        color: #0f172a;
        transform: translateX(4px);
    }
    .sidebar .nav-link:hover i {
        transform: scale(1.1);
    }
    .sidebar .nav-link.active {
        background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
        color: #ffffff;
        box-shadow: 0 4px 15px rgba(37, 99, 235, 0.25);
    }
    .sidebar .nav-link.active i {
        color: #ffffff;
    }
    
    .user-profile-mini {
        background: #f8fafc;
        border-radius: 16px;
        padding: 16px;
        margin-top: auto;
        margin-bottom: 1.5rem;
        border: 1px solid #f1f5f9;
    }
    .logout-link {
        color: #ef4444 !important;
    }
    .logout-link:hover {
        background-color: #fef2f2 !important;
    }
    .badge-notif {
        position: absolute;
        top: 10px;
        right: 15px;
        background: #ef4444;
        color: white;
        border-radius: 50%;
        width: 18px;
        height: 18px;
        font-size: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 0 0 2px white;
    }

    @media (max-width: 992px) {
        .sidebar { width: 85px; padding: 2rem 0.8rem; }
        .sidebar-brand span, .sidebar .nav-link span, .user-profile-mini { display: none; }
        .sidebar-brand { margin-bottom: 2rem; padding: 0; justify-content: center; }
        .sidebar .nav-link { justify-content: center; padding: 15px; margin-bottom: 10px; }
        .badge-notif { top: 5px; right: 5px; }
    }
</style>

<div class="sidebar">
    <a href="dashboard.php" class="sidebar-brand">
        <img src="<?= htmlspecialchars($site_logo) ?>?v=<?= time() ?>" alt="Logo">
        <span><?= htmlspecialchars($site_name) ?></span>
    </a>
    
    <nav class="flex-grow-1">
        <a href="dashboard.php" class="nav-link <?= $current_page === 'dashboard.php' ? 'active' : '' ?>">
            <i class="fas fa-th-large"></i>
            <span>工作台</span>
        </a>
        <a href="orders.php" class="nav-link <?= $current_page === 'orders.php' ? 'active' : '' ?>">
            <i class="fas fa-receipt"></i>
            <span>接码记录</span>
        </a>
        <a href="recharge.php" class="nav-link <?= $current_page === 'recharge.php' ? 'active' : '' ?>">
            <i class="fas fa-wallet"></i>
            <span>充值中心</span>
        </a>
        <a href="support.php" class="nav-link <?= $current_page === 'support.php' ? 'active' : '' ?>">
            <i class="fas fa-headset"></i>
            <span>联系客服</span>
            <span id="userSupportBadge" class="badge-notif" style="display:none;">0</span>
        </a>
    </nav>
    
    <div class="mt-auto">
        <?php if (isset($user['username'])): ?>
        <div class="user-profile-mini">
            <div class="d-flex align-items-center gap-3">
                <div class="rounded-circle bg-primary d-flex align-items-center justify-content-center text-white fw-bold" style="width: 32px; height: 32px; font-size: 0.8rem;">
                    <?= strtoupper(substr($user['username'], 0, 1)) ?>
                </div>
                <div class="overflow-hidden">
                    <div class="small text-muted fw-bold">Hi, <?= htmlspecialchars($user['username']) ?></div>
                    <div class="small fw-bold text-primary">$<?= number_format($user['balance'] ?? 0, 2) ?></div>
                </div>
            </div>
        </div>
        <?php endif; ?>
        <a href="auth.php?action=logout" class="nav-link logout-link">
            <i class="fas fa-sign-out-alt"></i>
            <span>退出登录</span>
        </a>
    </div>
</div>

<script>
    async function checkUserNotifications() {
        try {
            const res = await fetch('ajax_handler.php?action=check_new_messages');
            const data = await res.json();
            if (data.code === 0) {
                const count = parseInt(data.unread_total);
                const badge = document.getElementById('userSupportBadge');
                if (badge) {
                    if (count > 0) {
                        badge.textContent = count;
                        badge.style.display = 'flex';
                    } else {
                        badge.style.display = 'none';
                    }
                }
            }
        } catch(e) {}
    }
    setInterval(checkUserNotifications, 5000);
    checkUserNotifications();
</script>