<!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">
    <title>OpenKDS - <%- station.name  %></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>


    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background-color: #000;
            color: #fff;
            font-size: 2em;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            overflow: hidden;
        }
        .orderContainer {
            display: grid;
            grid-template-rows: repeat(2, 1fr);
            grid-template-columns: repeat(2, 1fr);
            height: 80vh;
            width: 85vw;
            grid-gap: 10px;
            margin: 0 auto;
            margin-top: 50px;
        }

        .order {
            background-color: #d5e8fd;
            border-radius: 10px;
            color: #000;
            position: relative;
        }
        .order-header{
            display: flex;
            justify-content: space-between;
            align-items: center;
            width: 100%;
            border-top-left-radius: 10px;
            border-top-right-radius: 10px;
            top: 0;
            left: 0;
            background-color: #385497;
            border-bottom: 1px solid #fff;
            padding: 10px;
            color: white;
        }
        .order-footer{
            position: absolute;
            bottom: 0;
            left: 0;
            width: 100%;
            background-color: #385497;
            border-bottom-left-radius: 10px;
            border-bottom-right-radius: 10px;
            padding: 10px;
            color: white;
        }
        .footer{
            position: fixed;
            bottom: 0;
            width: 100%;
            height: 10vh;
            color: #fff;
            border-radius: 10px;
        }

        .stationInfo{
            position: absolute;
            bottom: 10px;
            color: white;
            left: 50%;
            transform: translateX(-50%);

        }
        .time{
            float: right;
        }
        .pendingInfo{
            position: absolute;
            bottom:0;
            right:0;
            background-color: #fff;
            color: #000;
            padding: 10px;
        }
        .order [i="1"]{
            border: 1px solid #fff;
        }

        .loading__content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        .items{
            line-height: 0.7;
            margin-left: 10px;
            margin-top: 10px;
        }

        .item{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0px;
            margin: 0px;

        }
    </style>

</head>
<body>


    <div class="loading__content">
        <h1 class="loading__title">OpenKDS</h1>
        <p class="loading__text">Loading Data...</p>
    </div>
    

</body>

<script>
    let hourAvg = <%- station.avgtime %>;
    let stationType = <%- station.type.id %>;

    let orderDivs = document.querySelectorAll('.order');
    if(orderDivs.length > 4){
        for(let i = 4; i < orderDivs.length; i++){
            orderDivs[i].style.display = 'none';
        }
    }

    let timeCounters = document.querySelectorAll('.time');
    timeCounters.forEach((t) => {
        let time = t.innerHTML;
        let interval = setInterval(() => {
            time++;
            t.innerHTML = time;
        }, 1000);
    })

    setInterval(() => {
        let orders = document.querySelectorAll('.order');
        orders.forEach(order => {
            let time = order.querySelector('.time').innerHTML;
            let footer = order.querySelector('.order-footer');
            if(time > hourAvg){
                footer.style.backgroundColor = '#ff0033';
            } else {
                footer.style.backgroundColor = '#07cc00';
            }
        })
    }, 1000);
    // onclick event for order, to serve off the screen, orders are dynamically loaded so we need to use event delegation

    $("body").on("click", ".order", (e) => {
        let id = e.currentTarget.getAttribute('order-id');
        $.ajax({
            url: '/api/serve/' + id,
            type: 'GET',
            success: (data) => {
                console.log("OpenKDS: Served order " + id)
                setTimeout(() => {
                    updateOrders();
                }, 500);
            },
            error: (err) => {
                console.log("OpenKDS: Error serving order " + id)
                console.log("OpenKDS: " + err)
                $(".offlineMsg").show();
            }
        })
    })


    function updateOrders(){
        $.ajax({
            url: '/orders/html',
            type: 'GET',
            success: (data) => {
                console.log("OpenKDS: Updated orders")
                $("body").html(data);
            },
            error: (err) => {
                console.log("OpenKDS: Error updating orders")
                console.log("OpenKDS: " + err)
                $(".offlineMsg").show();
            }
        })
    }

    setInterval(() => {
        updateOrders();
    }, 1000);
</script>
</html>