<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单查询 - 量迹AI硬件</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        #result {
            margin-top: 30px;
        }
        .order-card {
            border: 1px solid #eee;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 15px;
            background-color: #fff;
        }
        .order-header {
            display: flex;
            justify-content: space-between;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
            margin-bottom: 10px;
        }
        .status {
            font-weight: bold;
        }
        .status-paid { color: green; }
        .status-pending { color: orange; }
        .status-shipped { color: blue; }
        .error { color: red; text-align: center; }
    </style>
</head>
<body>
    <div class="container">
        <h1>订单状态查询</h1>
        <div class="form-group">
            <label for="phone">请输入手机号码查询：</label>
            <input type="tel" id="phone" placeholder="请输入下单时填写的手机号" required>
        </div>
        <button onclick="searchOrders()">查询订单</button>
        
        <div id="result"></div>
    </div>

    <script>
        async function searchOrders() {
            const phone = document.getElementById('phone').value;
            const resultDiv = document.getElementById('result');
            
            if (!phone) {
                alert('请输入手机号码');
                return;
            }

            resultDiv.innerHTML = '<p style="text-align:center">查询中...</p>';

            try {
                const response = await fetch(`/api/orders/lookup/?phone=${phone}`);
                const data = await response.json();

                if (response.ok) {
                    if (data.length === 0) {
                        resultDiv.innerHTML = '<p class="error">未找到相关订单</p>';
                        return;
                    }

                    let html = '';
                    data.forEach(order => {
                        const statusMap = {
                            'pending': '待支付',
                            'paid': '已支付',
                            'shipped': '已发货',
                            'cancelled': '已取消'
                        };
                        const statusText = statusMap[order.status] || order.status;
                        const statusClass = `status-${order.status}`;
                        
                        html += `
                            <div class="order-card">
                                <div class="order-header">
                                    <span>订单号: ${order.id}</span>
                                    <span class="status ${statusClass}">${statusText}</span>
                                </div>
                                <div>
                                    <p><strong>商品:</strong> ${order.config_name || '未命名配置'}</p>
                                    <p><strong>数量:</strong> ${order.quantity}</p>
                                    <p><strong>总价:</strong> ¥${order.total_price}</p>
                                    <p><strong>下单时间:</strong> ${new Date(order.created_at).toLocaleString()}</p>
                                </div>
                            </div>
                        `;
                    });
                    resultDiv.innerHTML = html;
                } else {
                    resultDiv.innerHTML = `<p class="error">${data.error || '查询失败'}</p>`;
                }
            } catch (error) {
                console.error('Error:', error);
                resultDiv.innerHTML = '<p class="error">网络错误，请稍后重试</p>';
            }
        }
    </script>
</body>
</html>