<?php
include 'includes/header.php';

$search_order = null;
$error = '';

if (isset($_GET['order_no'])) {
    $stmt = db()->prepare("SELECT * FROM orders WHERE order_no = ?");
    $stmt->execute([$_GET['order_no']]);
    $search_order = $stmt->fetch();
    
    if (!$search_order) {
        $error = '未找到该订单，请检查订单号是否正确';
    } else {
        $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([$search_order['id']]);
        $search_order['items'] = $stmt->fetchAll();
    }
}
?>

<div class="row justify-content-center">
    <div class="col-lg-10">
        <div class="text-center mb-5">
            <h1 class="display-4 fw-bold">订单查询</h1>
            <p class="text-muted fs-5">输入您的订单号实时追踪订单状态与交付进度</p>
        </div>

        <div class="glass-card p-4 mb-5">
            <form action="orders.php" method="GET" class="row g-3">
                <div class="col-md-9">
                    <input type="text" name="order_no" class="form-control bg-dark border-secondary text-white p-3" placeholder="输入订单号 (例如: HR...)" value="<?php echo htmlspecialchars($_GET['order_no'] ?? ''); ?>" required>
                </div>
                <div class="col-md-3">
                    <button type="submit" class="btn btn-primary btn-lg w-100 py-3">立即查询</button>
                </div>
            </form>
        </div>

        <?php if ($error): ?>
        <div class="alert glass-card text-danger border-danger p-4 text-center">
            <i class="bi bi-exclamation-triangle-fill fs-3 d-block mb-2"></i>
            <?php echo $error; ?>
        </div>
        <?php endif; ?>

        <?php if ($search_order): ?>
        <div class="glass-card overflow-hidden">
            <div class="p-4 border-bottom border-secondary d-flex justify-content-between align-items-center" style="background: rgba(0, 242, 255, 0.05);">
                <div>
                    <span class="text-muted small d-block">订单编号</span>
                    <h5 class="text-white fw-bold mb-0"><?php echo $search_order['order_no']; ?></h5>
                </div>
                <div class="text-end">
                    <span class="text-muted small d-block">订单状态</span>
                    <span class="badge rounded-pill bg-<?php echo $search_order['status'] === 'completed' ? 'success' : ($search_order['status'] === 'pending' ? 'warning' : 'info'); ?> px-4 py-2">
                        <?php echo strtoupper($search_order['status']); ?>
                    </span>
                </div>
            </div>
            
            <div class="p-4 p-lg-5">
                <div class="table-responsive mb-5">
                    <table class="table table-dark table-borderless align-middle">
                        <thead class="border-bottom border-secondary">
                            <tr>
                                <th class="ps-0 py-3 text-muted">商品名称</th>
                                <th class="py-3 text-muted">数量</th>
                                <th class="py-3 text-muted text-end">小计</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($search_order['items'] as $it): ?>
                            <tr>
                                <td class="ps-0 py-4 fw-bold text-white"><?php echo $it['name']; ?></td>
                                <td class="py-4 text-white"><?php echo $it['quantity']; ?></td>
                                <td class="py-4 text-end fw-bold text-primary"><?php echo $it['price_usdt'] * $it['quantity']; ?> USDT</td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                        <tfoot>
                            <tr class="border-top border-secondary">
                                <td colspan="2" class="ps-0 py-4 text-white fs-4 fw-bold">订单总额</td>
                                <td class="py-4 text-end text-primary display-6 fw-bold"><?php echo $search_order['total_amount']; ?> USDT</td>
                            </tr>
                        </tfoot>
                    </table>
                </div>

                <div class="row g-4 mb-5">
                    <div class="col-md-6">
                        <div class="glass-card p-4 h-100">
                            <span class="text-muted small d-block mb-2">联系信息</span>
                            <div class="text-white fs-5"><?php echo htmlspecialchars($search_order['contact_info']); ?></div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="glass-card p-4 h-100">
                            <span class="text-muted small d-block mb-2">下单时间</span>
                            <div class="text-white fs-5"><?php echo $search_order['created_at']; ?></div>
                        </div>
                    </div>
                </div>
                
                <?php if ($search_order['status'] === 'pending'): ?>
                <div class="d-grid mb-4">
                    <a href="payment.php?order_no=<?php echo $search_order['order_no']; ?>" class="btn btn-primary btn-lg py-3">
                        <i class="bi bi-credit-card me-2"></i> 立即去支付
                    </a>
                </div>
                <?php endif; ?>
                
                <div class="text-center">
                    <p class="text-muted mb-4">如果您对订单有任何疑问，请随时联系我们的客服。</p>
                    <a href="<?php echo $settings['tg_support'] ?? '#'; ?>?text=订单咨询: <?php echo $search_order['order_no']; ?>" target="_blank" class="btn btn-outline-info rounded-pill px-5">
                        <i class="bi bi-telegram me-2"></i> 联系客服
                    </a>
                </div>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php include 'includes/footer.php'; ?>
