<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单流实时数据监控</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/dataTables.bootstrap5.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 8px;
            text-align: center;
        }
        button {
            margin: 10px;
            padding: 10px;
            cursor: pointer;
        }
        .active-symbol {
            background-color: #e0e0e0;
        }
    </style>
</head>
<body>
    <h1>Real-Time CSV Data Viewer</h1>

    <div id="symbol-buttons">
        <!-- 动态生成按钮 -->
    </div>

    <h3>Data for <span id="current-symbol">Loading...</span></h3>
    <table id="data-table">
        <thead>
            <tr>
                <th>Symbol</th>
                <th>Datetime</th>
                <th>Delta</th>
                <th>Close</th>
                <th>Open</th>
                <th>High</th>
                <th>Low</th>
                <th>Volume</th>
                <th>DJ</th>
                <th>Delta累计</th>
                <th>POC</th>
                <th>终极平滑值</th>
                <th>趋势方向</th>
                <th>最终趋势</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>

    <script>
        let currentSymbol = null;
        const socket = io();
        const symbolButtons = document.getElementById('symbol-buttons');
        const currentSymbolDisplay = document.getElementById('current-symbol');
        const tableBody = document.querySelector("#data-table tbody");

        // 初始化数据
        fetch('/api/data')
            .then(response => response.json())
            .then(data => {
                updateSymbolButtons(data);
                if (Object.keys(data).length > 0) {
                    currentSymbol = Object.keys(data)[0];
                    updateTable(data[currentSymbol]);
                }
            });

        // WebSocket事件处理
        socket.on('connect', () => {
            console.log('Connected to server');
        });

        socket.on('data_update', (data) => {
            updateSymbolButtons(data);
            if (currentSymbol && data[currentSymbol]) {
                updateTable(data[currentSymbol]);
            }
        });

        function updateSymbolButtons(data) {
            symbolButtons.innerHTML = '';
            Object.keys(data).forEach(symbol => {
                const button = document.createElement('button');
                button.textContent = symbol;
                button.onclick = () => {
                    currentSymbol = symbol;
                    updateTable(data[symbol]);
                };
                if (symbol === currentSymbol) {
                    button.classList.add('active-symbol');
                }
                symbolButtons.appendChild(button);
            });
        }

        function updateTable(data) {
            currentSymbolDisplay.textContent = currentSymbol;
            tableBody.innerHTML = '';
            data.forEach(row => {
                const rowElement = document.createElement('tr');
                rowElement.innerHTML = `
                    <td>${row.symbol || ''}</td>
                    <td>${row.datetime || ''}</td>
                    <td>${row.delta || ''}</td>
                    <td>${row.close || ''}</td>
                    <td>${row.open || ''}</td>
                    <td>${row.high || ''}</td>
                    <td>${row.low || ''}</td>
                    <td>${row.volume || ''}</td>
                    <td>${row.dj !== undefined ? row.dj : ''}</td>
                    <td>${row.delta累计 || ''}</td>
                    <td>${row.POC || ''}</td>
                    <td>${row.终极平滑值 || ''}</td>
                    <td>${row.趋势方向 || ''}</td>
                    <td>${row.最终趋势 || ''}</td>
                `;
                tableBody.appendChild(rowElement);
            });
        }
    </script>
</body>
</html>
