<!--
 ~ 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 = function(target, formAllData) {
    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);
        ezyadmin.setTargetVmOptions(target, formAllData);
    }).fail(function(data) {
        var msg = ezyadmin.messages['set_' + target + '_properties_failed'];
        ezyadmin.shortToast('bg-danger', msg);
        formData[target + 'Properties'] = formData.properties;
        if (data.responseJSON) {
            data.responseJSON[target + 'Properties'] = data.responseJSON.properties;
        }
        ezyadmin.processUpdateApiErrors(formData, data);
    });
}

// ============ set target vm options ============
ezyadmin.setTargetVmOptions = function(target, formAllData) {
    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);
        if (target == 'web') {
            location.reload();
        }
    }).fail(function(data) {
        var msg = ezyadmin.messages['set_' + target + '_vm_options_failed'];
        ezyadmin.shortToast('bg-danger', msg);
        formData[target + 'VmOptions'] = formData.vmOptions;
        if (data.responseJSON) {
            data.responseJSON[target + 'VmOptions'] = data.responseJSON.vmOptions;
        }
        ezyadmin.processUpdateApiErrors(formData, data);
    });
}
</script>
</body>
</html>
