<?php
$page_num = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 15;
$status = isset($_GET['status']) ? $_GET['status'] : '';

$where = "1=1";
if ($status) $where .= " AND o.status = '$status'";

$total = $db->fetchOne("SELECT COUNT(*) as cnt FROM orders o WHERE $where")['cnt'];
$offset = ($page_num - 1) * $perpage;

$orders = $db->fetchAll("
    SELECT o.*, u.username,
           CASE WHEN o.type = 'app' THEN (SELECT name FROM apps WHERE id = o.item_id)
                WHEN o.type = 'group' THEN (SELECT name FROM user_groups WHERE id = o.item_id)
                ELSE NULL END as item_name
    FROM orders o 
    JOIN users u ON o.user_id = u.id 
    WHERE $where 
    ORDER BY o.id DESC 
    LIMIT $offset, $perpage
");

$stats = [
    'total' => $db->fetchOne("SELECT COUNT(*) as cnt FROM orders")['cnt'],
    'pending' => $db->fetchOne("SELECT COUNT(*) as cnt FROM orders WHERE status = 'pending'")['cnt'],
    'paid' => $db->fetchOne("SELECT COUNT(*) as cnt FROM orders WHERE status = 'paid'")['cnt'],
];
?>

<!-- 统计 -->
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;">
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 20px; font-weight: 700;"><?php echo $stats['total']; ?></div>
        <div style="font-size: 12px; color: #999;">总数</div>
    </div>
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 20px; font-weight: 700; color: #f59e0b;"><?php echo $stats['pending']; ?></div>
        <div style="font-size: 12px; color: #999;">待支付</div>
    </div>
    <div class="card" style="text-align: center; padding: 12px;">
        <div style="font-size: 20px; font-weight: 700; color: #10b981;"><?php echo $stats['paid']; ?></div>
        <div style="font-size: 12px; color: #999;">已支付</div>
    </div>
</div>

<!-- 筛选 -->
<div style="display: flex; gap: 8px; margin: 12px 0;">
    <a href="?page=orders" class="btn" style="background:<?php echo $status==''?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status==''?'white':'#666'; ?>;">全部</a>
    <a href="?page=orders&status=pending" class="btn" style="background:<?php echo $status=='pending'?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status=='pending'?'white':'#666'; ?>;">待支付</a>
    <a href="?page=orders&status=paid" class="btn" style="background:<?php echo $status=='paid'?'#667eea':'#f5f5f5'; ?>; color:<?php echo $status=='paid'?'white':'#666'; ?>;">已支付</a>
</div>

<!-- 订单列表 -->
<?php foreach ($orders as $order): ?>
<div class="card">
    <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
        <span style="font-size: 12px; color: #999;"><?php echo $order['order_no']; ?></span>
        <span style="padding: 4px 10px; border-radius: 20px; font-size: 12px;
                     <?php 
                     if ($order['status'] == 'paid') echo 'background:#d1fae5;color:#065f46;';
                     elseif ($order['status'] == 'pending') echo 'background:#fef3c7;color:#92400e;';
                     else echo 'background:#e5e7eb;color:#4b5563;';
                     ?>">
            <?php echo $order['status'] == 'paid' ? '已支付' : ($order['status'] == 'pending' ? '待支付' : '已取消'); ?>
        </span>
    </div>
    <div style="display: flex; align-items: center; gap: 10px;">
        <img src="https://api.iconify.design/mdi:account.svg?color=%23666" width="20" height="20" alt="">
        <span><?php echo htmlspecialchars($order['username']); ?></span>
    </div>
    <div style="margin: 8px 0;">
        <span><?php echo htmlspecialchars($order['item_name'] ?: ($order['type'] == 'recharge' ? '余额充值' : '-')); ?></span>
    </div>
    <div style="display: flex; justify-content: space-between; align-items: center;">
        <span style="font-size: 18px; font-weight: 700; color: #10b981;">¥<?php echo number_format($order['amount'], 2); ?></span>
        <span style="font-size: 12px; color: #999;"><?php echo date('m-d H:i', strtotime($order['created_at'])); ?></span>
    </div>
    <?php if ($order['status'] == 'pending'): ?>
    <div style="margin-top: 10px;">
        <button onclick="deleteOrder(<?php echo $order['id']; ?>)" style="padding: 6px 12px; background: #fef2f2; border: none; border-radius: 6px; color: #ef4444;">
            <img src="https://api.iconify.design/mdi:delete.svg?color=%23ef4444" width="14" height="14" alt=""> 删除
        </button>
    </div>
    <?php endif; ?>
</div>
<?php endforeach; ?>

<script>
function deleteOrder(id) {
    if (!confirm('确定删除？')) return;
    fetch('/api/admin_delete_order.php', {
        method: 'POST',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'order_id=' + id
    }).then(r => r.json()).then(d => {
        if (d.success) location.reload();
        else showToast(d.message || '删除失败');
    });
}
</script>