---
import Layout from "../../layouts/Layout.astro";
---
<Layout title="">
    <main>
        <div>
            <section class="bg-white bg-center bg-no-repeat bg-cover h-screen">
                <div class="container mx-auto px-4">
                    <div class="flex flex-row place-content-between pt-6">
                        <p class="text-5xl text-[#5ac6c8] font-bold select-none">Tick</p>
                        <img src="/assets/top_logo.png" alt="" draggable="false" class="select-none">
                    </div>
                    <!-- <div class="flex flex-col">
                        <p id="LearningArea"></p>
                        <p id="LearningSubArea_copy"></p>
                    </div> -->
                    <p class="text-4xl text-center font-[600] text-[#7c4c23] mb-4 select-none" id="gameDescription"></p>
                    <form id="contactForm" class="flex flex-col justify-center">
                        <div id="" class="flex flex-row place-content-between gap-4">
                            <div class="flex flex-col gap-x-6 gap-y-2 place-items-center">
                                <label for="a1" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image1" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label1"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image1');" type="checkbox" id="a1" class="round-checkbox-input myCheckbox largerCheckbox" value="a1"/>
    
                                <label for="a2" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image2" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label2"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image2');" type="checkbox" id="a2" class="round-checkbox-input myCheckbox largerCheckbox" value="a2"/>
    
                                <label for="a3" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image3" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label3"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image3');" type="checkbox" id="a3" class="round-checkbox-input myCheckbox largerCheckbox" value="a3"/>
                            </div>
                            <div class="flex flex-col gap-x-6 gap-y-2 place-items-center">
                                <label for="a4" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image4" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label4"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image4');" type="checkbox" id="a4" class="round-checkbox-input myCheckbox largerCheckbox" value="a4"/>
    
                                <label for="a5" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image5" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label5"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image5');" type="checkbox" id="a5" class="round-checkbox-input myCheckbox largerCheckbox" value="a5"/>
    
                                <label for="a6" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image6" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label6"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image6');" type="checkbox" id="a6" class="round-checkbox-input myCheckbox largerCheckbox" value="a6"/>
                            </div>
                            <div class="flex flex-col gap-x-6 gap-y-2 place-items-center">
                                <label for="a7" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image7" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label7"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image7');" type="checkbox" id="a7" class="round-checkbox-input myCheckbox largerCheckbox" value="a7"/>
    
                                <label for="a8" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image8" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label8"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image8');" type="checkbox" id="a8" class="round-checkbox-input myCheckbox largerCheckbox" value="a8"/>
    
                                <label for="a9" class="round-checkbox-label">
                                    <div class="flex flex-col place-items-center">
                                        <img id="image9" src="" alt="" draggable="false" class="select-none" />
                                        <p id="label9"></p>
                                    </div>
                                </label>
                                <input onclick="checkResult2('image9');" type="checkbox" id="a9" class="round-checkbox-input myCheckbox largerCheckbox" value="a9"/>
                            </div>
                        </div>
                        <div class="flex flex-col justify-center place-items-center pt-8">
                            <p id="savedMessage"></p>
                            <input onclick="saveUserData();" class="bg-blue-700 px-8 py-2 rounded-lg shadow-lg font-bold text-white cursor-pointer" type="submit" value="Submit">
                        </div>
                    </form>
                </div>
            </section>
        </div>
    </main>
</Layout>
<script is:inline>
    const params = new URLSearchParams(window.location.search);
    const paramsID = params.get('id');
    
    fetch(`https://game-du.teachertrainingkolkata.in/items/tick_phonics_option_9/${encodeURIComponent(paramsID)}?filter[status][_eq]=published`)
        .then(res => res.json())
        .then(data => {
            gameData = data.data;
            // console.log(gameData)
            document.getElementById("gameDescription").innerHTML = gameData.description;
            if(gameData.label1){
                document.getElementById("label1").innerHTML = gameData.label1;
            }
            if(gameData.label2){
                document.getElementById("label2").innerHTML = gameData.label2;
            }
            if(gameData.label3){
                document.getElementById("label3").innerHTML = gameData.label3;
            }
            if(gameData.label4){
                document.getElementById("label4").innerHTML = gameData.label4;
            }
            if(gameData.label5){
                document.getElementById("label5").innerHTML = gameData.label5;
            }
            if(gameData.label6){
                document.getElementById("label6").innerHTML = gameData.label6;
            }
            if(gameData.label7){
                document.getElementById("label7").innerHTML = gameData.label7;
            }
            if(gameData.label8){
                document.getElementById("label8").innerHTML = gameData.label8;
            }
            if(gameData.label9){
                document.getElementById("label9").innerHTML = gameData.label9;
            }
            // document.getElementById('LearningArea').innerHTML = gameData.LearningArea;
            // document.getElementById('LearningSubArea_copy').innerHTML = gameData.LearningSubArea_copy;
            const assetsURL = 'https://game-du.teachertrainingkolkata.in/assets/';
            for (let i = 1; i <= 9; i++) {
                const imageId = `image${i}`;
                document.getElementById(imageId).src = assetsURL + gameData[imageId];
            }
        });

    function checkResult2(id) {
        const checkbox = document.getElementById('a' + id.slice(-1));
        const element = document.getElementById(id);

        if (checkbox.checked) {
            if (gameData[id.replace('image', 'a')] === true) {
                element.classList.add('greenBorder');
            } else {
                element.classList.add('redBorder');
            }
        } else {
            element.classList.remove('greenBorder', 'redBorder');
        }
    }

    let url = window.location.href;
    let urlSplit = url.split('/');
    let gameName = urlSplit[3] + '-' + urlSplit[4];
    let formatedDateTime;

    window.onload = function () {
        let currentTime = new Date();
        formatedDateTime = currentTime.toLocaleString();
    };

    function saveUserData() {
        // const checkboxValues = {
        //     a1: document.getElementById('a1').checked,
        //     a2: document.getElementById('a2').checked,
        //     a3: document.getElementById('a3').checked,
        //     a4: document.getElementById('a4').checked,
        //     a5: document.getElementById('a5').checked,
        //     a6: document.getElementById('a6').checked,
        //     a7: document.getElementById('a7').checked,
        //     a8: document.getElementById('a8').checked,
        //     a9: document.getElementById('a9').checked,
        // };
        const checkboxes = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9'];
        const checkboxValues = checkboxes.map(id => {
            const checkbox = document.getElementById(id);
            const element = document.getElementById('image' + id.slice(-1));
            return {
                id: id,
                checked: checkbox.checked,
                element: element
            };
        });

        // Count points based on checkbox values and "greenBorder" class
        let totalPoints = 0;
        checkboxValues.forEach(checkbox => {
            if (checkbox.checked && checkbox.element.classList.contains('greenBorder')) {
                totalPoints += 1;
            }
        });
        let userData = {
            'user': 'tick-v2@beanstalkedu.com',
            'game_name': gameName,
            'starts': formatedDateTime,
            // 'game_start' : gameStartTime,
            'score' : totalPoints,
        };

        fetch(`https://2016.dev2-cs.siliconpin.com/save/`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(userData)
        })
            .then(response => {
                if (response.ok) {
                    document.getElementById('savedMessage').innerHTML = 'Saved Successfully';
                } else {
                    console.log('Something Wrong', response);
                }
            })
            .catch(error => {
                console.error('An error occurred', error);
            });
    }

    document.addEventListener('DOMContentLoaded', function () {
        const contactForm = document.getElementById('contactForm');
        contactForm.addEventListener('submit', async function (event) {
            event.preventDefault();
        });
    });
</script>
<!-- <script is:inline>
    const params = new URLSearchParams(window.location.search);
    const paramsID = params.get('id');
    fetch(`https://management.beanstalkedu.com/items/game_tick_variant2/${encodeURIComponent(paramsID)}?filter[status][_eq]=published`)
    .then(res => res.json())
    .then(data => {
        gameData = data.data;
        // console.log(gameData);  
        document.getElementById("gameDescription").innerHTML = gameData.description;
        const assetsURL = 'https://management.beanstalkedu.com/assets/';
        document.getElementById("image1").src = assetsURL + gameData.image1;
        document.getElementById("image2").src = assetsURL + gameData.image2;
        document.getElementById("image3").src = assetsURL + gameData.image3;
        document.getElementById("image4").src = assetsURL + gameData.image4;
        document.getElementById("image5").src = assetsURL + gameData.image5;
        document.getElementById("image6").src = assetsURL + gameData.image6;
        document.getElementById("image7").src = assetsURL + gameData.image7;
        document.getElementById("image8").src = assetsURL + gameData.image8;
        document.getElementById("image9").src = assetsURL + gameData.image9;
    });
    function checkResult2(id){
        // alert("Matched")
        if(id == 'image1'){
            // console.log(gameData.a1)
            if(gameData.a1 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image2'){
            if(gameData.a2 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image3'){
            if(gameData.a3 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image4'){
            if(gameData.a4 == true){
                var element = document.getElementById(id);
                element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image5'){
            if(gameData.a5 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image6'){
            if(gameData.a6 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image7'){
            if(gameData.a7 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image8'){
            if(gameData.a8 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
        if(id == 'image9'){
            if(gameData.a9 == true){
            var element = document.getElementById(id);
            element.classList.add("greenBorder");
            } else{
                var element = document.getElementById(id);
                element.classList.add("redBorder");
            }
        };
       
    };
    let url = window.location.href;
    let urlSplit = url.split('/')
    let gameName = urlSplit[3] +`-`+ urlSplit[4];
    let formatedDateTime;

    window.onload = function () {
        let currentTime = new Date();
        formatedDateTime = currentTime.toLocaleString();
        // console.log(formatedDateTime);

        // Create userData object inside the onload event
        let userData = {
            'f2': formatedDateTime,
        };
        // console.log(userData); // Now, f1 and f2 should have values
    };
    function generateShortUniqueID(length) {
        const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        let result = '';
        for (let i = 0; i < length; i++) {
            const randomIndex = Math.floor(Math.random() * characters.length);
            result += characters.charAt(randomIndex);
        }
        return result;
    }
    const shortUniqueID = generateShortUniqueID(10); // Change 10 to the desired length
    // console.log(shortUniqueID);
    function saveUserData() {
        let userData = {
            'status' : 'published',
            'user_id': 'tick-v2@beanstalkedu.com',
            'game_name': gameName,
            'game_open': formatedDateTime,
        };

        fetch(`https://2016.dev2-cs.siliconpin.com/save/`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(userData)
        })
        .then(response => {
            if (response.ok) {
                document.getElementById('savedMessage').innerHTML = 'Saved Successfully';
                // console.log('Data Saved', response);
            } else {
                // console.log('Something Wrong', response);
            }
        })
        .catch(error => {
            console.error('An error occurred', error);
        });
    }
    document.addEventListener('DOMContentLoaded', function () {
      const contactForm = document.getElementById('contactForm');
      contactForm.addEventListener('submit', async function (event) {
        event.preventDefault();
      });
    });
</script> -->
<style>
    @import url('https://fonts.googleapis.com/css2?family=Lobster&family=Quicksand:wght@500;700&display=swap');
    body{
        font-family: Quicksand;
    }
    input.largerCheckbox {
        width: 40px;
        height: 40px;
        cursor: pointer;
    }
    .greenBorder{
        border: 4px solid #008000;
        border-radius: 10%;
        transition: border 0.5s, border-color 0.3s, transform 6s;
    }
    .redBorder{
        border: 4px solid red;
        border-radius: 10%;
    }
    #image1, #image2, #image3, #image4, #image5, #image6, #image7, #image8, #image9{
        width: 150px;
    }
    #label1, #label2, #label3, #label4, #label5, #label6, #label7, #label8, #label9{
        text-align: center;
        font-weight: bold;
        color: #7C4C23;
        font-size: 20px;
        padding-top: 10px;
    }
    #LearningArea, #LearningSubArea_copy{
        font-size: 20px;
        color: #7C4C23;
        font-weight: bold;
    }
    .round-checkbox-label {
        display: inline-block;
        cursor: pointer;
        position: relative;
        transition: background-color 0.3s, border-color 0.3s, transform 0.3s;
    }
    .round-checkbox-input:checked + .round-checkbox-label {
        border-radius: 30%;
    }
    .round-checkbox-input:checked + .round-checkbox-label::after {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-size: 40px;
        display: block;
    }
</style>
