<!--

    Queue - A Queueing system that can be used to handle labs in higher education
    Copyright (C) 2016-2020  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.patch.labs.CapacitySessionPatchDTO"-->
    <!--@thymesVar id="lSession" type="nl.tudelft.labracore.api.dto.SessionDetailsDTO"-->

    <!--@thymesVar id="buildings" type="java.util.List<nl.tudelft.labracore.api.dto.BuildingSummaryDTO>"-->
    <!--@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>"-->

    <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
                                name="name"
                                th:value="${lSession.name}"
                                placeholder="Question session" />
                        </div>

                        <span>Slot</span>
                        <div class="flex gap-3">
                            <div class="flex vertical gap-0">
                                <label for="opens-at">From</label>
                                <input
                                    type="datetime-local"
                                    id="opens-at"
                                    name="slot.opensAt"
                                    th:value="${#temporals.format(lSession.start, 'yyyy-MM-dd''T''HH:mm')}"
                                    class="textfield"
                                    required />
                            </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"
                                    name="slot.closesAt"
                                    th:value="${#temporals.format(lSession.endTime, 'yyyy-MM-dd''T''HH:mm')}"
                                    class="textfield"
                                    required />
                            </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
                                class="selectpicker form-control"
                                data-select
                                id="building-select"
                                data-placeholder="Pick at least one building"
                                onchange="updateRooms()">
                                <th:block th:each="b : ${buildings}">
                                    <option
                                        th:value="${b.id}"
                                        th:text="${b.name}"
                                        th:selected="${!#lists.isEmpty(lSession.rooms.?[building.id == __${b.id}__])}"></option>
                                </th:block>
                            </select>
                        </div>

                        <label for="room-select">Rooms</label>
                        <div>
                            <select
                                multiple
                                data-select
                                th:classappend="${#fields.hasErrors('rooms')} ? 'is-invalid'"
                                id="room-select"
                                name="rooms"
                                required>
                                <th:block th:each="r : ${rooms}">
                                    <option
                                        th:value="${r.id}"
                                        th:classappend="${#lists.isEmpty(lSession.rooms.?[building.id == __${r.building.id}__])} ? 'd-none'"
                                        th:selected="${@labService.containsRoom(lSession, r.id)}"
                                        th:text="|${r.building.name} - ${r.name} (${r.capacity})|"
                                        th:data-building-id="${r.building.id}"></option>
                                </th:block>
                            </select>
                        </div>

                        <label for="module-select">Modules</label>
                        <div>
                            <select multiple data-select id="module-select" name="modules" required>
                                <th:block th:each="m : ${modules}">
                                    <option th:value="${m.id}" th:selected="${lab.modules.contains(m.id)}" th:text="${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 => {
                            const visibleElem = document.getElementById(`_room-select__option-${opt.index}`);
                            if (buildings.includes(opt.dataset.buildingId.toString())) {
                                visibleElem.classList.remove("hidden");
                                opt.removeAttribute("disabled");
                            } else {
                                visibleElem.classList.add("hidden");
                                opt.removeAttribute("selected");
                                opt.setAttribute("disabled", "");
                            }
                        });

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