{{header}}
    <div class="content">
        <h1>Scale Configuration Page</h1>
        
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Sacle Calibration</h5>
                <button id="calibrateBtn" class="btn btn-primary">Calibrate Scale</button>
                <button id="tareBtn" class="btn btn-secondary">Tare Scale</button>
                <div id="statusMessage" class="mt-3"></div>
            </div>
        </div>

        <!-- Neue Kalibrierungskarte -->
        <div id="calibrationCard" class="card mt-3" style="display: none;">
            <div class="card-body">
                <h5 class="card-title">Calibration done</h5>
                <p>Please follow these steps:</p>
                <ol>
                    <li>Make sure the scale is empty</li>
                    <li>Have a 500g calibration weight ready</li>
                    <li>Click on "Start Calibration"</li>
                    <li>Follow the further instructions</li>
                </ol>
                <ol>
                    <li>Step 1: Empty the scale</li>
                    <li>Step 2: Place the 500g weight on the scale</li>
                    <li>Step 3: Remove weight from Scale</li>
                </ol>
                <button id="startCalibrationBtn" class="btn btn-danger">Start Calibration</button>
            </div>
        </div>
    </div>

    <script>
        let ws = null;
        const statusMessage = document.getElementById('statusMessage');

        function connectWebSocket() {
            ws = new WebSocket(`ws://${window.location.hostname}/ws`);
            
            ws.onopen = () => {
                console.log('WebSocket verbunden');
                statusMessage.innerHTML = 'Scale connected';
                enableButtons(true);
            };

            ws.onclose = () => {
                console.log('WebSocket getrennt');
                statusMessage.innerHTML = 'Scale connection lost';
                enableButtons(false);
                setTimeout(connectWebSocket, 2000);
            };

            ws.onmessage = (event) => {
                const data = JSON.parse(event.data);
                if (data.type === 'scale') {
                    if (data.payload === 'success') {
                        statusMessage.innerHTML = 'Well done';
                        statusMessage.className = 'alert alert-success';
                    } else if (data.payload === 'error') {
                        statusMessage.innerHTML = 'Error while action';
                        statusMessage.className = 'alert alert-danger';
                    }
                }
            };
        }

        function enableButtons(enabled) {
            document.getElementById('calibrateBtn').disabled = !enabled;
            document.getElementById('tareBtn').disabled = !enabled;
        }

        document.getElementById('calibrateBtn').addEventListener('click', () => {
            // Kalibrierungskarte anzeigen
            document.getElementById('calibrationCard').style.display = 'block';
        });

        document.getElementById('startCalibrationBtn').addEventListener('click', () => {
            ws.send(JSON.stringify({
                type: 'scale',
                payload: 'calibrate'
            }));
            // Optional: Kalibrierungskarte nach dem Start ausblenden
            document.getElementById('calibrationCard').style.display = 'none';
        });

        document.getElementById('tareBtn').addEventListener('click', () => {
            ws.send(JSON.stringify({
                type: 'scale',
                payload: 'tare'
            }));
        });

        // WebSocket-Verbindung beim Laden der Seite initiieren
        connectWebSocket();
    </script>
</body>
</html>