{% extends 'wgcomm/base.html' %}

{% block title %}
    Add Floor
{% endblock %}
{% block tail %}
    <script src="{{ url_for('static',filename='wgcomm/js/controller-add.js') }}"></script>
{% endblock %}
{% block tail_min %}
    <script src="{{ url_for('static',filename='wgcomm/js/controller-add.min.js') }}"></script>
{% endblock %}
{% block main_content %}
    <!--app-content open-->
    <div class="app-content main-content mt-0">
        <div class="side-app">
            <!-- CONTAINER -->
            <div class="main-container container-fluid">

                <!-- PAGE-HEADER -->
                <div class="page-header">
                    <div>
                        <h1 class="page-title">Add Floor</h1>
                    </div>
                    <div class="ms-auto pageheader-btn">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item">Configuration</li>
                            <li class="breadcrumb-item"><a href="javascript:void(0);">Floor</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Add</li>
                        </ol>
                    </div>
                </div>
                <!-- PAGE-HEADER END -->
                {% with messages = get_flashed_messages() %}
                    {% if messages %}
                        <div class="form-group">
                            <ul>
                                {% for message in messages %}
                                    <li class="text-danger">{{ message }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                    {% endif %}
                {% endwith %}
                <!--ROW OPENED-->
                <div class="row">
                    <div class="col-lg-12 col-md-12">
                        <div class="card">
                            <form action="{{ url_for('wgcomm_extern_elevator.web_extern_elevator_add') }}"
                                  method="post">
                                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                                {{ wgform._csrf }}
                                <div class="card-header border-bottom">
                                    <h4 class="mb-0">Floor Information</h4>
                                </div>
                                <div class="card-body">
                                    <div class="form-group">
                                        {{ wgform.f_FloorName.label }}
                                        {{ wgform.f_FloorName( class="form-control") }}
                                    </div>
                                    <div class="form-group">
                                        {{ wgform.f_DoorID.label }}
                                        <select class="form-control  form-select  text-dark" id="f_DoorID"
                                                name="f_DoorID">
                                            {% for dept in elevators %}
                                                <option value="{{ dept.f_DoorID }}"
                                                        {% if wgform.f_DoorID.data== dept.f_DoorID %}
                                                        selected
                                                        {% endif %}
                                                >{{ dept.f_DoorName }}</option>
                                            {% endfor %}
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        {{ wgform.f_FloorNO.label }}
                                        <select class="form-control  form-select  text-dark" id="f_FloorNO"
                                                name="f_FloorNO">
                                            {% for key, value_list in arr_allow_floorno.items() %}
                                                {% if key == wgform.f_DoorID.data %}
                                                    {% for id in value_list %}
                                                        <option value="{{ id }}"
                                                                {% if wgform.f_FloorNO.data == id %}
                                                                selected
                                                                {% endif %}
                                                        >{{ id }}</option>
                                                    {% endfor %}
                                                {% endif %}
                                            {% endfor %}
                                        </select>
                                    </div>
                                </div>
                                <script>
                                    // 获取页面上的元素
                                    const selectElement = document.getElementById('f_DoorID');
                                    const displayArea = document.getElementById('f_FloorNO');
                                    const arrfloorno = {{ arr_allow_floorno }}
                                        // 为 select 元素Add change 事件监听器
                                        selectElement.addEventListener('change', function (event) {
                                            // 获取新选中的值。event.target 就是Trigger Event的这Unit select 元素本身
                                            const selectedValue = event.target.value;
                                            // 使用Object.entries()遍历
                                            displayArea.textContent = "";
                                            //console.log(selectedValue);
                                            Object.entries(arrfloorno).forEach(([key, valueArray]) => {
                                                if (selectedValue == key) {
                                                    valueArray.forEach(floorno => {
                                                        displayArea.innerHTML += `<option value="${floorno}">${floorno}</option>`;
                                                    });
                                                }
                                            });
                                        });
                                </script>
                                <div class="row p-5">
                                    <div class="btn-list text-end">
                                        <button id="submitcancel" name="submitcancel" class="btn btn-outline-danger"
                                                type="submit" value="Cancel">
                                            <i class="fe fe-x-circle"></i>
                                            Cancel
                                        </button>
                                        <button id="submitsave" name="submitsave" class="btn btn-primary" type="submit"
                                                value="Save">
                                            <i class="fe fe-check-circle"></i>
                                            Save
                                        </button>
                                        <button id="submitsave" name="submitsaveandadd" class="btn btn-primary"
                                                type="submit" value="Save And Continue">
                                            <i class="fe fe-check-square"></i>
                                            Save And Continue
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <!--ROW CLOSED-->
            </div>
        </div>
    </div>
    <!-- CONTAINER CLOSED -->
{% endblock %}
