<?php
require_once __DIR__ . '/layout.php';
$db = db();

// Real stats
$total_users = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();
$total_recharge = $db->query("SELECT SUM(amount) FROM finance_requests WHERE type='recharge' AND status IN ('3', 'completed')")->fetchColumn() ?: 0;
$total_withdrawal = $db->query("SELECT SUM(amount) FROM finance_requests WHERE type='withdrawal' AND status IN ('3', 'completed')")->fetchColumn() ?: 0;
$pending_finance = $db->query("SELECT COUNT(*) FROM finance_requests WHERE status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished')")->fetchColumn();
$pending_kyc = $db->query("SELECT COUNT(*) FROM users WHERE kyc_status=1 AND kyc_name IS NOT NULL")->fetchColumn();

ob_start();
?>

<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="card p-3 border-start border-primary border-4">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <div class="text-muted small">总用户数</div>
                    <div class="fs-3 fw-bold" id="stat-total-users"><?= number_format($total_users) ?></div>
                </div>
                <div class="text-primary"><i class="bi bi-people fs-1 opacity-25"></i></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 border-start border-success border-4">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <div class="text-muted small">总充值 (USDT)</div>
                    <div class="fs-3 fw-bold" id="stat-total-recharge"><?= number_format($total_recharge, 2) ?></div>
                </div>
                <div class="text-success"><i class="bi bi-cash-stack fs-1 opacity-25"></i></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 border-start border-danger border-4">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <div class="text-muted small">总提现 (USDT)</div>
                    <div class="fs-3 fw-bold" id="stat-total-withdrawal"><?= number_format($total_withdrawal, 2) ?></div>
                </div>
                <div class="text-danger"><i class="bi bi-bank fs-1 opacity-25"></i></div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card p-3 border-start border-warning border-4">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <div class="text-muted small">待办事项</div>
                    <div class="fs-3 fw-bold text-warning" id="stat-pending-tasks"><?= $pending_finance + $pending_kyc ?></div>
                </div>
                <div class="text-warning"><i class="bi bi-list-check fs-1 opacity-25"></i></div>
            </div>
        </div>
    </div>
</div>

<div class="row g-4">
    <div class="col-md-8">
        <div class="table-container mb-4">
            <h6 class="mb-4 fw-bold">最近待审核充值</h6>
            <table class="table table-sm align-middle">
                <thead>
                    <tr class="text-muted small">
                        <th>用户ID</th>
                        <th>金额</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $pending_list = $db->query("SELECT r.*, u.uid FROM finance_requests r JOIN users u ON r.user_id=u.id WHERE r.status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished') AND r.type='recharge' LIMIT 5")->fetchAll();
                    foreach ($pending_list as $p):
                    ?>
                    <tr>
                        <td><code><?= $p['uid'] ?></code></td>
                        <td class="text-success fw-bold">+<?= number_format($p['amount'], 2) ?></td>
                        <td><small><?= date('H:i', strtotime($p['created_at'])) ?></small></td>
                        <td><a href="finance.php?type=recharge" class="btn btn-sm btn-link">处理</a></td>
                    </tr>
                    <?php endforeach; ?>
                    <?php if (empty($pending_list)): ?><tr><td colspan="4" class="text-center py-3 text-muted">暂无待处理充值</td></tr><?php endif; ?>
                </tbody>
            </table>
        </div>

        <div class="table-container">
            <h6 class="mb-4 fw-bold">最新下单记录</h6>
            <table class="table table-sm align-middle">
                <thead>
                    <tr class="text-muted small">
                        <th>用户</th>
                        <th>类型</th>
                        <th>金额</th>
                        <th>方向</th>
                        <th>控制</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $orders = $db->query("SELECT o.*, u.username FROM binary_orders o JOIN users u ON o.user_id=u.id ORDER BY o.created_at DESC LIMIT 5")->fetchAll();
                    foreach ($orders as $o):
                    ?>
                    <tr>
                        <td><?= htmlspecialchars($o['username']) ?></td>
                        <td>秒合约</td>
                        <td><?= number_format($o['amount'], 2) ?></td>
                        <td><span class="text-<?= $o['direction']=='buy'?'success':'danger' ?>"><?= $o['direction']=='buy'?'涨':'跌' ?></span></td>
                        <td>
                            <?php if ($o['control_status']==1): ?><span class="badge bg-success">控赢</span>
                            <?php elseif ($o['control_status']==2): ?><span class="badge bg-danger">控输</span>
                            <?php else: ?>-<?php endif; ?>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="table-container h-100">
            <h6 class="mb-4 fw-bold">快速导航</h6>
            <div class="list-group list-group-flush">
                <a href="users.php" class="list-group-item list-group-item-action border-0 px-0 d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-people me-2"></i> 玩家管理</span>
                    <span class="badge bg-light text-dark rounded-pill"><?= $total_users ?></span>
                </a>
                <a href="finance.php?type=recharge" class="list-group-item list-group-item-action border-0 px-0 d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-wallet2 me-2"></i> 充值审核</span>
                    <span class="badge bg-danger rounded-pill"><?= $db->query("SELECT COUNT(*) FROM finance_requests WHERE type='recharge' AND status IN ('0', '1', '2', 'pending', 'matched', 'account_sent', 'finished')")->fetchColumn() ?></span>
                </a>
                <a href="kyc.php" class="list-group-item list-group-item-action border-0 px-0 d-flex justify-content-between align-items-center">
                    <span><i class="bi bi-card-text me-2"></i> 实名审核</span>
                    <span class="badge bg-warning text-dark rounded-pill"><?= $pending_kyc ?></span>
                </a>
                <a href="ai_control.php" class="list-group-item list-group-item-action border-0 px-0">
                    <i class="bi bi-robot me-2"></i> AI 控盘设置
                </a>
                <a href="backend_settings.php" class="list-group-item list-group-item-action border-0 px-0">
                    <i class="bi bi-gear me-2"></i> 系统参数设置
                </a>
            </div>
            
            <div class="mt-5 p-3 bg-light rounded">
                <p class="small text-muted mb-0">系统版本: v2.5.0</p>
                <p class="small text-muted mb-0">数据库状态: <span class="text-success">在线</span></p>
            </div>
        </div>
    </div>
</div>

<?php
$content = ob_get_clean();
renderAdminPage($content, '后台仪表盘');
?>
