<!--
 ~ Copyright 2022 youngmonkeys.org
 ~ 
 ~ Licensed under the ezyplatform, Version 1.0.0 (the "License");
 ~ you may not use this file except in compliance with the License.
 ~ You may obtain a copy of the License at
 ~ 
 ~     https://youngmonkeys.org/licenses/ezyplatform-1.0.0.txt
 ~ 
 ~ Unless required by applicable law or agreed to in writing, software
 ~ distributed under the License is distributed on an "AS IS" BASIS,
 ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ~ See the License for the specific language governing permissions and
 ~ limitations under the License.
-->

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<body>
<th:block th:fragment="content">
    <div class="col-12 mb-3">
        <h3>[[#{local_settings}]]</h3>
    </div>
    <div class="col-12">
        <form id="localSettingsForm" onsubmit="return false;">
            <div class="form-group">
                <div>
                    <label class="col-form-label text-danger d-none"
                           id="localSettingsErrorLabel" for="localSettings">
                        <i class="far fa-times-circle"></i> <span id="localSettingsError"></span>
                    </label>
                    <textarea name="localSettings" id="localSettings" class="form-control"
                              rows="8" th:placeholder="#{local_settings}"
                              th:text="${localSettingsText}"></textarea>
                </div>
            </div>
            <button type="button" class="btn btn-primary px-5" onclick="ezyadmin.saveLocalSettings();">
                [[#{save}]]
            </button>
        </form>
    </div>
</th:block>
<script th:fragment="scripts">
ezyadmin.saveLocalSettings = function() {
    var formData = {
        localSettings: ezyadmin.getLocalSettingsFormData()
    };
    ezyadmin.hideFormErrors({
        localSettings: $('#localSettings').val()
    });
    $.ajax({
        type: 'PUT',
        url: '/api/v1/settings/local',
        contentType: 'application/json',
        data: JSON.stringify(formData)
    }).done(function() {
        ezyadmin.shortToast(
            'bg-success',
            ezyadmin.messages.save_successfully
        );
    }).fail(function(e) {
        ezyadmin.shortToast(
            'bg-danger',
            ezyadmin.messages.save_failed
        );
        ezyadmin.processUpdateApiErrors(formData, e);
    });
}

ezyadmin.getLocalSettingsFormData = function() {
    var localSettings = {};
    var lines = $('#localSettings').val().split('\n');
    lines.forEach(line => {
        var trimmedLine = line.trim();
        if (!trimmedLine || trimmedLine.startsWith('#')) {
            return;
        }
        var separatorIndex = trimmedLine.indexOf('=');
        if (separatorIndex > 0) {
            var key = trimmedLine.substring(0, separatorIndex).trim();
            var value = trimmedLine.substring(separatorIndex + 1).trim();
            if (key) {
                localSettings[key] = value;
            }
        }
    });
    return localSettings;
}
</script>
</body>
</html>
