<?php
require_once 'auth.php';

// Handle Status Update
if (isset($_POST['update_status'])) {
    $stmt = $db->prepare("UPDATE orders SET status = ? WHERE id = ?");
    $stmt->execute([$_POST['status'], $_POST['order_id']]);
    header("Location: orders.php" . (isset($_GET['id']) ? "?id=".$_POST['order_id'] : ""));
    exit;
}

$view_order = null;
if (isset($_GET['id'])) {
    $stmt = $db->prepare("SELECT * FROM orders WHERE id = ?");
    $stmt->execute([$_GET['id']]);
    $view_order = $stmt->fetch();
    
    if ($view_order) {
        $stmt = $db->prepare("SELECT oi.*, p.name FROM order_items oi JOIN products p ON oi.product_id = p.id WHERE oi.order_id = ?");
        $stmt->execute([$view_order['id']]);
        $view_order['items'] = $stmt->fetchAll();
    }
}

$orders = $db->query("SELECT * FROM orders ORDER BY id DESC")->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; }
    </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"><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" class="active"><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">
            <?php if ($view_order): ?>
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2>订单详情: <?php echo $view_order['order_no']; ?></h2>
                <a href="orders.php" class="btn btn-secondary">返回列表</a>
            </div>
            
            <div class="row g-4">
                <div class="col-md-8">
                    <div class="card p-4 mb-4">
                        <h5 class="mb-4">商品列表</h5>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>商品</th>
                                    <th>单价</th>
                                    <th>数量</th>
                                    <th>小计</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($view_order['items'] as $it): ?>
                                <tr>
                                    <td><?php echo $it['name']; ?></td>
                                    <td><?php echo $it['price_usdt']; ?> USDT</td>
                                    <td><?php echo $it['quantity']; ?></td>
                                    <td class="fw-bold"><?php echo number_format($it['price_usdt'] * $it['quantity'], 2); ?> USDT</td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="3" class="text-end fw-bold">合计金额</td>
                                    <td class="text-primary fs-5 fw-bold"><?php echo $view_order['total_amount']; ?> USDT</td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card p-4 mb-4">
                        <h5 class="mb-4">订单状态</h5>
                        <form method="POST">
                            <input type="hidden" name="order_id" value="<?php echo $view_order['id']; ?>">
                            <div class="mb-3">
                                <label class="form-label">修改状态</label>
                                <select name="status" class="form-select">
                                    <option value="pending" <?php echo $view_order['status'] === 'pending' ? 'selected' : ''; ?>>待付款 (Pending)</option>
                                    <option value="paid" <?php echo $view_order['status'] === 'paid' ? 'selected' : ''; ?>>已付款 (Paid)</option>
                                    <option value="completed" <?php echo $view_order['status'] === 'completed' ? 'selected' : ''; ?>>已完成 (Completed)</option>
                                    <option value="cancelled" <?php echo $view_order['status'] === 'cancelled' ? 'selected' : ''; ?>>已取消 (Cancelled)</option>
                                </select>
                            </div>
                            <button type="submit" name="update_status" class="btn btn-primary w-100">更新状态</button>
                        </form>
                    </div>
                    <div class="card p-4">
                        <h5 class="mb-3">客户信息</h5>
                        <p><strong>联系方式:</strong><br><?php echo $view_order['contact_info']; ?></p>
                        <p><strong>下单时间:</strong><br><?php echo $view_order['created_at']; ?></p>
                        <p><strong>支付方式:</strong><br><?php echo $view_order['payment_method']; ?></p>
                    </div>
                </div>
            </div>
            
            <?php else: ?>
            <h2 class="mb-4">订单管理</h2>
            <div class="card p-4">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr>
                            <th>订单号</th>
                            <th>联系方式</th>
                            <th>金额</th>
                            <th>状态</th>
                            <th>日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($orders as $o): ?>
                        <tr>
                            <td><code><?php echo $o['order_no']; ?></code></td>
                            <td><?php echo $o['contact_info']; ?></td>
                            <td class="fw-bold"><?php echo $o['total_amount']; ?> USDT</td>
                            <td>
                                <span class="badge bg-<?php echo $o['status'] === 'completed' ? 'success' : ($o['status'] === 'pending' ? 'warning' : ($o['status'] === 'paid' ? 'info' : 'danger')); ?>">
                                    <?php echo $o['status']; ?>
                                </span>
                            </td>
                            <td><?php echo $o['created_at']; ?></td>
                            <td><a href="?id=<?php echo $o['id']; ?>" class="btn btn-sm btn-outline-primary">查看详情</a></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>
