<!--

    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/>.

-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <!--@thymesVar id="dto" type="nl.tudelft.queue.dto.create.QueueSessionCreateDTO<?>"-->

    <!--@thymesVar id="modules" type="java.util.List<nl.tudelft.labracore.api.dto.ModuleDetailsDTO>"-->
    <!--@thymesVar id="rooms" type="java.util.List<nl.tudelft.labracore.api.dto.RoomDetailsDTO>"-->
    <!--@thymesVar id="buildings" type="java.util.List<nl.tudelft.labracore.api.dto.BuildingSummaryDTO>"-->

    <body>
        <th:block th:object="${dto}">
            <section class="flex vertical" th:fragment="session-general">
                <div>
                    <h3 class="font-500 mb-3">General</h3>

                    <div class="grid col-2 align-center gap-3" style="--col-1: minmax(0, 8rem)">
                        <label for="title-input">Title</label>
                        <div>
                            <input
                                id="title-input"
                                type="text"
                                class="textfield"
                                required="required"
                                th:field="*{name}"
                                placeholder="Question session" />
                        </div>

                        <span>Slot</span>
                        <div class="flex gap-3">
                            <div class="flex vertical gap-0">
                                <label for="opens-at" class="input-group-text">From</label>
                                <input type="datetime-local" id="opens-at" th:field="*{slot.opensAt}" class="textfield" required />
                                <div class="invalid-feedback" th:if="${#fields.hasErrors('slot.opensAt')}" th:errors="*{slot.opensAt}">
                                    Slot opensAt error
                                </div>
                            </div>

                            <div class="flex vertical gap-0" data-target-input="nearest">
                                <label for="closes-at" class="input-group-text">To</label>
                                <input type="datetime-local" id="closes-at" th:field="*{slot.closesAt}" class="textfield" required />
                                <div class="invalid-feedback" th:if="${#fields.hasErrors('slot.closesAt')}" th:errors="*{slot.closesAt}">
                                    Slot error
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="underlined"></div>

                <div>
                    <h3 class="font-500 mb-3">Location</h3>

                    <div class="grid col-2 gap-3 align-center" style="--col-1: minmax(0, 8rem)">
                        <label for="building-select">Buildings</label>
                        <div>
                            <select
                                multiple
                                data-select
                                id="building-select"
                                data-placeholder="Pick at least one building"
                                required
                                onchange="updateRooms()">
                                <th:block th:each="b : ${buildings}">
                                    <option
                                        th:value="${b.id}"
                                        th:text="${b.name}"
                                        th:selected="${!#lists.isEmpty(@sessionService.roomDetails(dto).?[building.id == __${b.id}__])}"></option>
                                </th:block>
                            </select>
                        </div>

                        <label for="room-select">Rooms</label>
                        <div>
                            <select multiple id="room-select" th:field="*{rooms}" data-select disabled required data-placeholder="Pick a room">
                                <optgroup th:each="building : ${buildings}" th:label="${building.name}">
                                    <option
                                        th:each="room : ${rooms.?[#this.building.id == #root.building.id]}"
                                        th:value="${room.id}"
                                        th:id="|room-${room.id}|"
                                        th:selected="${dto.rooms.contains(room.id)}"
                                        th:text="|${room.name} (${room.capacity})|"
                                        th:data-building-id="${room.building.id}"></option>
                                </optgroup>
                            </select>
                        </div>

                        <label for="module-select">Modules</label>
                        <div>
                            <select
                                multiple
                                id="module-select"
                                th:field="*{modules}"
                                required
                                data-select
                                data-placeholder="Pick at least one module">
                                <th:block th:each="m : ${modules}">
                                    <option
                                        th:value="${m.id}"
                                        th:selected="${dto.modules.contains(m.id)}"
                                        th:text="${ec == null} ? ${m.name} : |${editionMap[m.edition.id].course.name} - ${m.name}|"></option>
                                </th:block>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="underlined"></div>

                <script th:inline="javascript" type="text/javascript">
                    document.addEventListener("ComponentsLoaded", function () {
                        const opensAtInput = document.getElementById("opens-at");
                        const closesAtInput = document.getElementById("closes-at");

                        opensAtInput.addEventListener("change", function () {
                            closesAtInput.min = opensAtInput.value;
                            closesAtInput.value = moment(opensAtInput.value).add(225, "minutes").format("YYYY-MM-DD[T]HH:mm");
                        });
                    });

                    function updateRooms() {
                        const roomSelect = document.getElementById("room-select");
                        const buildings = [...document.getElementById("building-select").selectedOptions].map(opt => opt.value);

                        if (buildings.length === 0) {
                            roomSelect.setAttribute("disabled", "");
                        } else {
                            roomSelect.removeAttribute("disabled");
                        }

                        [...roomSelect.options].forEach(opt => {
                            if (buildings.includes(opt.dataset.buildingId.toString())) {
                                opt.removeAttribute("disabled");
                                opt.removeAttribute("hidden");
                            } else {
                                opt.removeAttribute("selected");
                                opt.setAttribute("hidden", "");
                                opt.setAttribute("disabled", "");
                            }
                        });

                        roomSelect.update();
                    }
                </script>
            </section>
        </th:block>
    </body>
</html>
