<!--

    Queue - A Queueing system that can be used to handle labs in higher education
    Copyright (C) 2016-2024  Delft University of Technology

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

-->
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{edition/view}">
    <head>
        <title th:text="|Create session for ${'#' + (ec == null ? edition.id : ec.id)}|"></title>

        <script src="/webjars/momentjs/min/moment.min.js"></script>
    </head>

    <!--@thymesVar id="ec" type="nl.tudelft.labracore.api.dto.EditionCollectionDetailsDTO"-->
    <!--@thymesVar id="edition" type="nl.tudelft.labracore.api.dto.EditionDetailsDTO"-->

    <!--@thymesVar id="dto" type="nl.tudelft.queue.dto.create.labs.LabCreateDTO"-->
    <!--@thymesVar id="lType" type="nl.tudelft.queue.model.enums.QueueSessionType"-->

    <body>
        <section layout:fragment="subcontent">
            <form
                th:with="action = @{/{ou}/{id}/lab/create/{lTy}(id=${ec == null ? edition.id : ec.id}, ou=${ec == null ? 'edition' : 'shared-edition'}, lTy=${lType.name().toLowerCase()})}"
                th:action="${action}"
                th:object="${dto}"
                class="flex vertical"
                method="post"
                id="form">
                <th:block layout:fragment="general-config"></th:block>

                <th:block layout:fragment="slot-config"></th:block>
                <th:block layout:fragment="exam-config"></th:block>
                <th:block layout:fragment="capacity-config"></th:block>
                <th:block layout:fragment="extra-config"></th:block>

                <th:block layout:fragment="advanced-config"></th:block>

                <div>
                    <button type="submit" class="button">Create new session</button>
                </div>
            </form>
            <script th:unless="${lType.equals(T(nl.tudelft.queue.model.enums.QueueSessionType).SLOTTED)}">
                document.addEventListener("DOMContentLoaded", function () {
                    const gpInput = document.getElementById("end-of-lab-gp-input");
                    gpInput.addEventListener("change", function () {
                        updateGracePeriod(gpInput.value);
                        displayDuration(true);
                    });
                });

                let gracePeriod = 15; // default value

                function updateGracePeriod(newPeriod) {
                    gracePeriod = newPeriod;
                }

                /**
                 * Calculates the duration of the session and displays it in the duration label
                 * @param withGracePeriod indicates if the grace period should be added to the display
                 */
                function displayDuration(withGracePeriod) {
                    const startInput = document.getElementById("opens-at").value;
                    const endInput = document.getElementById("closes-at").value;
                    const label = document.getElementById("session-duration");

                    if (startInput && endInput) {
                        const startDate = new Date(startInput);
                        const endDate = new Date(endInput);
                        const difference = endDate - startDate; // difference in milliseconds

                        if (difference < 0) {
                            label.textContent = "End should be later than the start";
                            return;
                        }

                        const minutes = Math.floor(difference / (1000 * 60));
                        const hours = Math.floor(minutes / 60);
                        const remainingMinutes = minutes % 60;
                        let text = `Duration: ${hours} hour(s) ${remainingMinutes} minutes`;

                        if (withGracePeriod) {
                            text += ` (+ ${gracePeriod} minutes grace)`;
                        }
                        label.textContent = text;
                        return;
                    }
                    label.textContent = "";
                }
            </script>
        </section>
    </body>
</html>
