<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.6.1/font/bootstrap-icons.css">
    <link href="/css/style.css" rel="stylesheet">
    <title>Interactive presentation - Result</title>
</head>
<body>
    <div class="container-fluid mt-5" id="result">
        
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.3.2/socket.io.min.js" integrity="sha512-N5JbZMETkl+0SZh5l8OA2VxSfF120owfQyy54iAXTJIKuqAn3A9TrNz1ypB55o8aSU5MxaodEgv74wGUvgUynQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script>
        $(function() {
            
            function render(question, results) {
                let results_html = '';

                for(const[key, value] of Object.entries(results)) {
                    results_html += `
                    <div class="card shadow mt-2">
                        <div class="card-body">
                            <div class="row">
                                <div class="col-6">${key}</div>
                                <div class="col-6 text-right sepa-l">${value}%</div>
                            </div>
                        </div>
                    </div>
                    `
                }
                

                if(results_html == '') {
                    results_html = `<h5 class="text-center">No answers yet...</h5>`;
                }

                $('#result').html(`
                    <div class="row current-question">
                        <div class="col-md-3"></div>
                        <div class="col-md-6">
                            <h1>${question}</h1>
                            <hr>
                            ${results_html}
                        </div>
                        <div class="col-md-3"></div>
                    </div>
                `);
            }

            const socket = io("http://localhost:3000");
            socket.emit("resultLoaded");
            socket.on('updateResults', (payload) => {
                render(payload.question, payload.results);
            });
            socket.on("reload", (payload) => {
                if(payload.reload == true) {
                    window.location.reload();
                }
            });
        });
    </script>
</body>
</html>