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

$db = db();
$total_users = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$pending_kyc = $db->query("SELECT COUNT(*) FROM users WHERE kyc_status = 1")->fetchColumn();
$pending_orders = $db->query("SELECT COUNT(*) FROM fiat_orders WHERE status IN ('matching', 'submitting')")->fetchColumn();
$unread_msgs = $db->query("SELECT COUNT(*) FROM messages WHERE sender = 'user' AND is_read = 0")->fetchColumn();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>管理后台 - NovaEx</title>
    <link rel="icon" type="image/png" href="../assets/pasted-20260211-152742-c4035b1b.png">
    <link rel="stylesheet" href="../assets/css/custom.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .admin-layout { display: flex; min-height: 100vh; }
        .sidebar { width: 250px; background: #1E2329; border-right: 1px solid #2B3139; padding: 1rem; }
        .main-content { flex: 1; padding: 2rem; background: #0B0E11; }
        .menu-item { padding: 12px; color: #848E9C; text-decoration: none; display: flex; align-items: center; gap: 10px; border-radius: 4px; margin-bottom: 5px; }
        .menu-item:hover, .menu-item.active { background: #2B3139; color: white; }
        .badge { background: var(--danger-color); color: white; border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; margin-left: auto; }
        .stat-card { background: #1E2329; padding: 20px; border-radius: 8px; border: 1px solid #2B3139; }
        .admin-logo { width: 200px; height: 80px; object-fit: contain; }
    </style>
</head>
<body>
<div class="admin-layout">
    <div class="sidebar">
        <div style="display: flex; flex-direction: column; align-items: center; margin-bottom: 2rem; gap: 10px;">
            <img src="../assets/pasted-20260211-152742-c4035b1b.png" alt="Logo" class="admin-logo">
            <div style="color: white; font-weight: bold; font-size: 0.9rem;">NovaEx 管理员</div>
        </div>
        <a href="index.php" class="menu-item active"><i class="fas fa-chart-pie"></i> 仪表盘</a>
        <a href="users.php" class="menu-item"><i class="fas fa-users"></i> 用户管理</a>
        <a href="kyc.php" class="menu-item">
            <i class="fas fa-id-card"></i> KYC 审核
            <?php if($pending_kyc > 0): ?><span class="badge"><?php echo $pending_kyc; ?></span><?php endif; ?>
        </a>
        <a href="chat.php" class="menu-item">
            <i class="fas fa-headset"></i> 客服管理
            <?php if($unread_msgs > 0 || $pending_orders > 0): ?><span class="badge"><?php echo ($unread_msgs + $pending_orders); ?></span><?php endif; ?>
        </a>
        <a href="spot_orders.php" class="menu-item"><i class="fas fa-exchange-alt"></i> 现货交易</a>
        <a href="futures_orders.php" class="menu-item"><i class="fas fa-file-contract"></i> 合约交易</a>
        <a href="orders.php" class="menu-item">
            <i class="fas fa-wallet"></i> 充值记录
        </a>
        <a href="settings.php" class="menu-item"><i class="fas fa-cog"></i> 系统设置</a>
        <a href="../index.php" class="menu-item" style="margin-top: 2rem; color: orange;"><i class="fas fa-external-link-alt"></i> 查看前端</a>
    </div>
    <div class="main-content">
        <h2 style="color: white; margin-bottom: 2rem;">系统概览</h2>
        <div style="grid-template-columns: repeat(4, 1fr); display: grid; gap: 1.5rem;">
            <div class="stat-card">
                <div style="color: #848E9C; font-size: 0.9rem;">总注册人数</div>
                <div style="font-size: 2rem; color: white; margin-top: 10px;"><?php echo $total_users; ?></div>
            </div>
            <div class="stat-card">
                <div style="color: #848E9C; font-size: 0.9rem;">待处理 KYC</div>
                <div style="font-size: 2rem; color: #F0B90B; margin-top: 10px;"><?php echo $pending_kyc; ?></div>
            </div>
            <div class="stat-card">
                <div style="color: #848E9C; font-size: 0.9rem;">待匹配/审核充值</div>
                <div style="font-size: 2rem; color: #F0B90B; margin-top: 10px;"><?php echo $pending_orders; ?></div>
            </div>
            <div class="stat-card">
                <div style="color: #848E9C; font-size: 0.9rem;">未读消息</div>
                <div style="font-size: 2rem; color: #00C087; margin-top: 10px;"><?php echo $unread_msgs; ?></div>
            </div>
        </div>

        <div style="margin-top: 3rem; background: #1E2329; padding: 25px; border-radius: 8px;">
            <h3 style="color: white; margin-bottom: 20px;">控制中心</h3>
            <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;">
                <div style="border: 1px solid #2B3139; padding: 20px; border-radius: 6px;">
                    <h4 style="color: #848E9C;">客服与充值管理</h4>
                    <p style="color: #848E9C; font-size: 0.8rem;">与用户对话并处理实时的充值匹配请求。</p>
                    <a href="chat.php" class="btn-primary" style="display: inline-block; margin-top: 15px; font-size: 0.8rem; padding: 8px 15px;">进入工作台</a>
                </div>
                <div style="border: 1px solid #2B3139; padding: 20px; border-radius: 6px;">
                    <h4 style="color: #848E9C;">交易管理</h4>
                    <p style="color: #848E9C; font-size: 0.8rem;">审核并处理现货及合约交易订单。</p>
                    <div style="display:flex; gap: 10px; margin-top: 15px;">
                        <a href="spot_orders.php" class="btn-primary" style="font-size: 0.75rem; padding: 5px 10px; background: #377aff;">现货</a>
                        <a href="futures_orders.php" class="btn-primary" style="font-size: 0.75rem; padding: 5px 10px; background: #f0b90b; color: black;">合约</a>
                    </div>
                </div>
                <div style="border: 1px solid #2B3139; padding: 20px; border-radius: 6px;">
                    <h4 style="color: #848E9C;">价格控制</h4>
                    <p style="color: #848E9C; font-size: 0.8rem;">手动覆盖特定交易对的实时价格及插针控制。</p>
                    <a href="settings.php" class="btn-primary" style="display: inline-block; margin-top: 15px; font-size: 0.8rem; padding: 8px 15px;">立即配置</a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>