<?php
session_start();

// Redirect to login page if not logged in
if (!isset($_SESSION['username'])) {
    header("Location: login.php");
    exit();
}
?>

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Caller</title>
    <style>
        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            color: #e0e0e0;
            font-family: 'Roboto', sans-serif;
            margin: 0;
        }

        .button-container {
            display: grid;
            grid-template-columns: repeat(3, 160px);
            gap: 10px;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
        }

        button {
            width: 150px;
            height: 150px;
            font-size: 32px;
            font-weight: bold;
            background: #61afef;
            color: white;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            transition: transform 0.2s background 0.3s, box-shadow 0.3s;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }

        button:hover {
            background: #4aa3df;
            transform: scale(1.05);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.25);
        }

        button:active {
            transform: scale(0.95);
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
        }

        #output {
            font-size: 40px;
            margin: 20px;
            color: black;
            font-weight: 600;
        }

        .count-display {
            font-size: 24px;
            margin-bottom: 10px;
            color: black;
        }

        #call {
            background: yellow;
            color: black;
        }

        #alert-box {
            position: fixed;
            top: 20px;
            left: 50%;
            transform: translateX(-50%);
            background: rgba(0, 0, 0, 0.8);
            color: #fff;
            padding: 10px 20px;
            border-radius: 5px;
            box-shadow: 0px 6px 10px rgba(0, 0, 0, 0.2);
            display: none;
            z-index: 1000;
            font-size: 16px;
        }

        .call {
            border: 2px solid black;
        }

        @media (max-width: 768px) {
            .button-container {
                grid-template-columns: repeat(3, minmax(60px, 90px));
            }

            button {
                width: 80px;
                height: 80px;
                font-size: 24px;
            }

            #output {
                font-size: 40px;
            }

            .count-display {
                font-size: 20px;
            }
        }

        @media (max-width: 480px) {
            .button-container {
                grid-template-columns: repeat(3, minmax(50px, 70px));
            }

            button {
                width: 60px;
                height: 60px;
                font-size: 18px;
            }

            #output {
                font-size: 30px;
            }

            .count-display {
                font-size: 18px;
            }
        }
    </style>

    <script>
        let currentNumber = "";
        let count = 0;
        let isCalling = false; // Prevents multiple submissions

        function callNumber(number) {
            if (currentNumber.length < 2) {
                currentNumber += number;
            }
            updateDisplay();
        }

        function deleteNumber() {
            currentNumber = currentNumber.slice(0, -1);
            updateDisplay();
        }

        function submitNumber() {
            if (isCalling) return; // Prevent double calling

            if (currentNumber) {
                isCalling = true; // Lock call process

                fetch('update.php?number=' + currentNumber)
                    .then(response => response.text())
                    .then(() => {
                        showAlert("Customer " + currentNumber + " called!");

                        currentNumber = "";
                        updateDisplay();

                        count++;
                        if (count > 99) {
                            count = 1;
                        }
                        document.getElementById('count-display').innerText = "Customer served: " + count;

                        setTimeout(() => {
                            isCalling = false; // Unlock call process after 1 second
                        }, 1000);
                    });
            } else {
                showAlert("No number to submit!");
            }
        }

        function updateDisplay() {
            document.getElementById('output').innerText = currentNumber || "0";
        }

        function showAlert(message) {
            let alertBox = document.getElementById('alert-box');
            alertBox.innerText = message;
            alertBox.style.display = "block";
            setTimeout(() => {
                alertBox.style.display = "none";
            }, 2000);
        }

        function toggleFullScreen() {
            if (!document.fullscreenElement) {
                document.documentElement.requestFullscreen();
            } else {
                document.toggleFullScreen();
            }
        }

        document.addEventListener('keydown', (event) => {
            if (event.key === 'Escape' && document.fullscreenElement) {
                document.toggleFullScreen();
            }
        });

        document.addEventListener('dblclick', toggleFullScreen);
    </script>
</head>

<body>
    <div id="alert-box"></div>
    <h2 id="count-display" class="count-display">Customer served: 0</h2>
    <div id="output">0</div>
    <div class="button-container">
        <?php for ($i = 1; $i <= 9; $i++) : ?>
            <button onclick="callNumber(<?php echo $i; ?>)"><?php echo $i; ?></button>
        <?php endfor; ?>
        <button onclick="deleteNumber()">C</button>
        <button onclick="callNumber(0)">0</button>
        <button class="call" id="call" onclick="submitNumber()">Call</button>
    </div>
</body>

</html>