<!--
 ~ 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="http://www.w3.org/1999/xhtml"
        xmlns:th="http://www.thymeleaf.org">
<body>
<script th:fragment="scripts">
// ============ set target properties ============
ezyadmin.setTargetProperties = async function(target, formAllData) {
    return new Promise((resolve, reject) => {
        var propertiesLines = formAllData[target + 'Properties'].split('\n');
        var properties = {};
        propertiesLines.forEach(line => {
            if (line.length > 0 && line.includes('=')) {
                var keyValue = line.split('=');
                properties[keyValue[0].trim()] = keyValue[1].trim();
            }
        });
        var formData = {
            properties: properties
        };
        ezyadmin.hideFormErrors(formData);
        $.ajax({
            type: 'PUT',
            url: '/api/v1/settings/set-' + target + '-properties',
            contentType: 'application/json',
            data: JSON.stringify(formData)
        }).done(function() {
            var msg = ezyadmin.messages['set_' + target + '_properties_successfully'];
            ezyadmin.shortToast('bg-success', msg);
            resolve();
        }).fail(function(e) {
            var msg = ezyadmin.messages['set_' + target + '_properties_failed'];
            ezyadmin.shortToast('bg-danger', msg);
            formData[target + 'Properties'] = formData.properties;
            if (e.responseJSON) {
                e.responseJSON[target + 'Properties'] = e.responseJSON.properties;
            }
            ezyadmin.processUpdateApiErrors(formData, e);
            reject(e);
        });
    });
}

// ============ set target vm options ============
ezyadmin.setTargetVmOptions = async function(target, formAllData) {
    return new Promise((resolve, reject) => {
        var formData = {
            vmOptions: formAllData[target + 'VmOptions']
        };
        ezyadmin.hideFormErrors(formData);
        $.ajax({
            type: 'PUT',
            url: '/api/v1/settings/set-' + target + '-vm-options',
            contentType: 'application/json',
            data: JSON.stringify(formData)
        }).done(function() {
            var msg = ezyadmin.messages['set_' + target + '_vm_options_successfully'];
            ezyadmin.shortToast('bg-success', msg);
            resolve();
        }).fail(function(e) {
            var msg = ezyadmin.messages['set_' + target + '_vm_options_failed'];
            ezyadmin.shortToast('bg-danger', msg);
            formData[target + 'VmOptions'] = formData.vmOptions;
            if (e.responseJSON) {
                e.responseJSON[target + 'VmOptions'] = e.responseJSON.vmOptions;
            }
            ezyadmin.processUpdateApiErrors(formData, e);
            reject(e);
        });
    });
}
</script>
</body>
</html>
