<!--
 ~ 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>
<th:block th:fragment="actions">
    <button type="button" class="btn btn-primary"
            th:if="${dataAppenderManager.isDataAppenderStarted(dataAppenderName) != true}"
            th:attr="onclick=|ezyadmin.onDataAppenderActionClick('${dataAppenderName}', 'start')|">
        [[#{start}]]
    </button>
    <button type="button" class="btn btn-danger"
            th:if="${dataAppenderManager.isDataAppenderStarted(dataAppenderName) == true}"
            th:attr="onclick=|ezyadmin.onDataAppenderActionClick('${dataAppenderName}', 'stop')|">
        [[#{stop}]]
    </button>
    <button type="button" class="btn btn-info"
            th:if="${dataAppenderManager.isDataAppenderStarted(dataAppenderName) == true}"
            th:attr="onclick=|ezyadmin.onDataAppenderActionClick('${dataAppenderName}', 'restart')|">
        [[#{restart}]]
    </button>
    <button th:if="${allowReloadDataAppender}" type="button" class="btn btn-warning"
            th:attr="onclick=|ezyadmin.onDataAppenderActionClick('${dataAppenderName}', 'reload')|">
        [[#{restart}]]
    </button>
</th:block>
<th:block th:fragment="modals">
    <th:block th:replace="~{fragments/prompt :: content(id='start-data-appender-modal', title=confirm, confirmButton=start, closeButton=close)}" />
    <th:block th:replace="~{fragments/prompt :: content(id='stop-data-appender-modal', title=confirm, confirmButton=stop, closeButton=close, type=delete)}" />
    <th:block th:replace="~{fragments/prompt :: content(id='restart-data-appender-modal', title=confirm, confirmButton=restart, closeButton=close)}" />
    <th:block th:replace="~{fragments/prompt :: content(id='reload-data-appender-modal', title=confirm, confirmButton=reload, closeButton=close)}" />
</th:block>
<script th:fragment="scripts" th:inline="javascript">
/*<![CDATA[*/
ezyadmin.messages.do_you_want_to_act_data_appender = /*[[#{do_you_want_to_act_data_appender}]]*/;
ezyadmin.messages.reload = /*[[#{reload}]]*/;
ezyadmin.messages.restart = /*[[#{restart}]]*/;
ezyadmin.messages.start = /*[[#{start}]]*/;
ezyadmin.messages.stop = /*[[#{stop}]]*/;
ezyadmin.messages.successfully = /*[[#{successfully}]]*/;
/*]]>*/

ezyadmin.onDataAppenderActionClick = function(dataAppenderName, action) {
    ezyadmin.selectedDataAppenderName = dataAppenderName;
    ezyadmin.selectedDataAppenderAction = action;
    var confirmModal = $(`#${action}-data-appender-modal`);
    if (confirmModal && confirmModal.length) {
        $(`#${action}-data-appender-modal-body-content`).text(
            ezyadmin
                .messages
                .do_you_want_to_act_data_appender
                .replace('{0}', ezyadmin.messages[action].toLowerCase())
                .replace('{1}', dataAppenderName)
        );
        $(`#${action}-data-appender-modal`).modal('show');
    } else {
        ezyadmin.doDataAppenderAction();
    }
}

ezyadmin.doDataAppenderAction = function() {
    $.ajax({
        type: 'PUT',
        url: `/api/v1/settings/data-appenders/${ezyadmin.selectedDataAppenderName}/${ezyadmin.selectedDataAppenderAction}`,
        success: function () {
            var confirmModal = $(`#${ezyadmin.selectedDataAppenderAction}-data-appender-modal`);
            if (confirmModal) {
                confirmModal.modal('hide');
            }
            ezyadmin.shortToast(
                'bg-success',
                ezyadmin.messages[ezyadmin.selectedDataAppenderAction],
                ezyadmin.messages.successfully
            );
            if (ezyadmin.fetchDataAppenders) {
                ezyadmin.fetchDataAppenders();
            } else {
                location.reload();
            }
        },
        error: function (data, e) {
            ezyadmin.processUpdateApiErrors({}, data);
        }
    });
}

ezyadmin.onDataAppenderPromptConfirmed = function(modalId) {
    if (modalId == 'start-data-appender-modal') {
        ezyadmin.doDataAppenderAction();
    } else if (modalId == 'stop-data-appender-modal') {
        ezyadmin.doDataAppenderAction();
    } else if (modalId == 'restart-data-appender-modal') {
        ezyadmin.doDataAppenderAction();
    } else if (modalId == 'reload-data-appender-modal') {
        ezyadmin.doDataAppenderAction();
    }
}

ezyadmin.onPromptConfirmed = ezyadmin.onDataAppenderPromptConfirmed;
</script>
</body>
</html>
