<?php
require_once "auth.php";
require_once '../db/config.php';

$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="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root { --primary: #f0b90b; --bg: #ffffff; --text: #1e2329; --border: #eaecef; }
        body { background: #f4f6f9; color: var(--text); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; margin: 0; }
        .admin-layout { display: flex; min-height: 100vh; }
        .sidebar { width: 250px; background: #ffffff; border-right: 1px solid var(--border); padding: 1.5rem; }
        .main-content { flex: 1; padding: 2rem; background: #ffffff; }
        .menu-item { padding: 12px 15px; color: #474d57; text-decoration: none; display: flex; align-items: center; gap: 12px; border-radius: 8px; margin-bottom: 8px; transition: 0.2s; }
        .menu-item:hover, .menu-item.active { background: #f5f5f5; color: var(--primary); font-weight: bold; }
        .badge { background: #f6465d; color: white; border-radius: 10px; padding: 2px 8px; font-size: 0.7rem; margin-left: auto; }
        .stat-card { background: white; padding: 25px; border-radius: 12px; border: 1px solid var(--border); box-shadow: 0 2px 10px rgba(0,0,0,0.03); }
        .stat-label { color: #707a8a; font-size: 0.85rem; margin-bottom: 10px; font-weight: 600; text-transform: uppercase; }
        .stat-value { font-size: 2.2rem; font-weight: 800; color: #1e2329; }
    </style>
</head>
<body>
<div class="admin-layout">
    <div class="sidebar">
        <h2 style="color: var(--primary); margin-bottom: 2rem;">NovaEx Admin</h2>
        <a href="index.php" class="menu-item active"><i class="fas fa-home"></i> 仪表盘</a>
        <a href="users.php" class="menu-item"><i class="fas fa-users"></i> 用户管理</a>
        <a href="agents.php" class="menu-item"><i class="fas fa-user-shield"></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-comments"></i> 客服管理
            <?php if($unread_msgs > 0 || $pending_orders > 0): ?><span class="badge"><?php echo ($unread_msgs + $pending_orders); ?></span><?php endif; ?>
        </a>
        <a href="options_orders.php" class="menu-item"><i class="fas fa-clock"></i> 秒合约</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="withdrawals.php" class="menu-item"><i class="fas fa-hand-holding-usd"></i> 提现记录</a>
        <a href="settings.php" class="menu-item"><i class="fas fa-cog"></i> 系统设置</a>
        <a href="logout.php" class="menu-item"><i class="fas fa-sign-out-alt"></i> 退出登录</a>
        <a href="../index.php" class="menu-item" style="margin-top: 2rem; color: #f0b90b;"><i class="fas fa-external-link-alt"></i> 前端首页</a>
    </div>

    <div class="main-content">
        <h1 style="margin-bottom: 2rem;">系统概览</h1>
        <div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 1.5rem;">
            <div class="stat-card">
                <div class="stat-label">总注册用户</div>
                <div class="stat-value"><?php echo $total_users; ?></div>
            </div>
            <div class="stat-card">
                <div class="stat-label">待审核 KYC</div>
                <div class="stat-value" style="color: #f0b90b;"><?php echo $pending_kyc; ?></div>
            </div>
            <div class="stat-card">
                <div class="stat-label">待匹配充值</div>
                <div class="stat-value" style="color: #f6465d;"><?php echo $pending_orders; ?></div>
            </div>
            <div class="stat-card">
                <div class="stat-label">待回复消息</div>
                <div class="stat-value" style="color: #00c087;"><?php echo $unread_msgs; ?></div>
            </div>
        </div>

        <div style="margin-top: 3rem; background: #f9fafb; padding: 30px; border-radius: 16px; border: 1px solid var(--border);">
            <h2 style="margin-bottom: 20px;">控制中心</h2>
            <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px;">
                <div style="background: white; padding: 25px; border-radius: 12px; border: 1px solid var(--border);">
                    <h3>充值与客服</h3>
                    <p style="color: #707a8a; font-size: 0.9rem;">实时处理充值申请并与用户在线沟通。</p>
                    <a href="chat.php" style="background: var(--primary); color: black; padding: 10px 20px; border-radius: 8px; text-decoration: none; display: inline-block; margin-top: 15px; font-weight: bold;">进入客服中心</a>
                </div>
                <div style="background: white; padding: 25px; border-radius: 12px; border: 1px solid var(--border);">
                    <h3>交易订单</h3>
                    <p style="color: #707a8a; font-size: 0.9rem;">监控秒合约、现货及合约交易流水。</p>
                    <div style="margin-top: 15px; display: flex; gap: 10px;">
                        <a href="options_orders.php" style="background: #f4f6f9; color: #474d57; padding: 8px 15px; border-radius: 6px; text-decoration: none; font-size: 0.85rem;">秒合约</a>
                        <a href="futures_orders.php" style="background: #f4f6f9; color: #474d57; padding: 8px 15px; border-radius: 6px; text-decoration: none; font-size: 0.85rem;">合约交易</a>
                    </div>
                </div>
                <div style="background: white; padding: 25px; border-radius: 12px; border: 1px solid var(--border);">
                    <h3>全局设置</h3>
                    <p style="color: #707a8a; font-size: 0.9rem;">修改系统参数、胜率及站点 LOGO。</p>
                    <a href="settings.php" style="color: var(--primary); text-decoration: none; display: block; margin-top: 15px; font-weight: bold;">立即配置 <i class="fas fa-arrow-right"></i></a>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>