<?php
require_once 'auth.php';

$stats = [];
$stats['orders_count'] = $db->query("SELECT COUNT(*) FROM orders")->fetchColumn();
$stats['total_sales'] = $db->query("SELECT SUM(total_amount) FROM orders WHERE status = 'completed'")->fetchColumn() ?: 0;
$stats['products_count'] = $db->query("SELECT COUNT(*) FROM products")->fetchColumn();
$stats['users_count'] = $db->query("SELECT COUNT(*) FROM users")->fetchColumn();

$recent_orders = $db->query("SELECT * FROM orders ORDER BY id DESC LIMIT 5")->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>管理后台 - 豪软世界</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body { background: #f8f9fa; }
        .sidebar { min-height: 100vh; background: #212529; color: white; }
        .sidebar a { color: rgba(255,255,255,0.7); text-decoration: none; padding: 12px 20px; display: block; }
        .sidebar a:hover, .sidebar a.active { background: #343a40; color: white; }
        .card { border: none; box-shadow: 0 0.125rem 0.25rem rgba(0,0,0,0.075); border-radius: 10px; }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-2 p-0 sidebar">
            <div class="p-4 fs-4 fw-bold border-bottom border-secondary">豪软管理</div>
            <a href="index.php" class="active"><i class="bi bi-speedometer2 me-2"></i> 仪表盘</a>
            <a href="products.php"><i class="bi bi-box-seam me-2"></i> 商品管理</a>
            <a href="categories.php"><i class="bi bi-grid me-2"></i> 分类管理</a>
            <a href="orders.php"><i class="bi bi-cart me-2"></i> 订单管理</a>
            <a href="settings.php"><i class="bi bi-gear me-2"></i> 站点设置</a>
            <div class="mt-auto p-3">
                <a href="../index.php" target="_blank" class="small"><i class="bi bi-house me-1"></i> 返回前台</a>
                <a href="../logout.php" class="small text-danger"><i class="bi bi-box-arrow-right me-1"></i> 退出</a>
            </div>
        </div>
        <div class="col-md-10 p-5">
            <h2 class="mb-4">数据总览</h2>
            <div class="row g-4 mb-5">
                <div class="col-md-3">
                    <div class="card p-4 bg-primary text-white">
                        <div class="small">累计订单</div>
                        <div class="fs-2 fw-bold"><?php echo $stats['orders_count']; ?></div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card p-4 bg-success text-white">
                        <div class="small">完成交易 (USDT)</div>
                        <div class="fs-2 fw-bold"><?php echo number_format($stats['total_sales'], 2); ?></div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card p-4 bg-info text-white">
                        <div class="small">在售商品</div>
                        <div class="fs-2 fw-bold"><?php echo $stats['products_count']; ?></div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="card p-4 bg-warning text-dark">
                        <div class="small">注册用户</div>
                        <div class="fs-2 fw-bold"><?php echo $stats['users_count']; ?></div>
                    </div>
                </div>
            </div>

            <div class="card p-4">
                <h5 class="mb-4">最新订单</h5>
                <table class="table align-middle">
                    <thead>
                        <tr>
                            <th>订单号</th>
                            <th>联系方式</th>
                            <th>金额</th>
                            <th>状态</th>
                            <th>日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent_orders as $o): ?>
                        <tr>
                            <td><code><?php echo $o['order_no']; ?></code></td>
                            <td><?php echo $o['contact_info']; ?></td>
                            <td><?php echo $o['total_amount']; ?> USDT</td>
                            <td>
                                <span class="badge bg-<?php echo $o['status'] === 'completed' ? 'success' : ($o['status'] === 'pending' ? 'warning' : 'info'); ?>">
                                    <?php echo $o['status']; ?>
                                </span>
                            </td>
                            <td><?php echo date('m-d H:i', strtotime($o['created_at'])); ?></td>
                            <td><a href="orders.php?id=<?php echo $o['id']; ?>" class="btn btn-sm btn-outline-primary">查看</a></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
