{#
 # Copyright (c) 2026 cynfo.com
 # RMM Agent Settings Page
 #}

<script>
    $(document).ready(function () {
        var data_get_map = {'frm_GeneralSettings': '/api/rmmagent/settings/get'};
        mapDataToFormUI(data_get_map).done(function () {
            formatTokenizersUI();
            $('.selectpicker').selectpicker('refresh');
        });

        // Service status abrufen
        ajaxCall('/api/rmmagent/service/status', {}, function (data, status) {
            if (data && data['status']) {
                var badge = data['status'] === 'running'
                    ? '<span class="label label-success">Running</span>'
                    : '<span class="label label-danger">Stopped</span>';
                $('#service_status_container').html(badge);
            }
        });

        $('#saveAct').unbind('click').click(function () {
            saveFormToEndpoint('/api/rmmagent/settings/set', 'frm_GeneralSettings', function () {
                $('#saveAct_progress').addClass('fa fa-spinner fa-pulse');
                ajaxCall('/api/rmmagent/service/reconfigure', {}, function (data, status) {
                    $('#saveAct_progress').removeClass('fa fa-spinner fa-pulse');
                    // Status aktualisieren
                    ajaxCall('/api/rmmagent/service/status', {}, function (data, status) {
                        if (data && data['status']) {
                            var badge = data['status'] === 'running'
                                ? '<span class="label label-success">Running</span>'
                                : '<span class="label label-danger">Stopped</span>';
                            $('#service_status_container').html(badge);
                        }
                    });
                });
            });
        });

        $('#restartAct').unbind('click').click(function () {
            $('#restartAct_progress').addClass('fa fa-spinner fa-pulse');
            ajaxCall('/api/rmmagent/service/restart', {}, function (data, status) {
                $('#restartAct_progress').removeClass('fa fa-spinner fa-pulse');
                ajaxCall('/api/rmmagent/service/status', {}, function (data, status) {
                    if (data && data['status']) {
                        var badge = data['status'] === 'running'
                            ? '<span class="label label-success">Running</span>'
                            : '<span class="label label-danger">Stopped</span>';
                        $('#service_status_container').html(badge);
                    }
                });
            });
        });
    });
</script>

<div class="content-box" style="padding: 10px;">
    <div class="content-box-header">
        <h3>{{ lang._('RMM Agent') }} <small id="service_status_container"><span class="label label-default">...</span></small></h3>
    </div>
    <div class="content-box-main">
        <div class="table-responsive">
            {{ partial("layout_partials/base_form", ['fields': generalForm, 'id': 'frm_GeneralSettings']) }}
        </div>
    </div>
    <div class="col-md-12">
        <hr/>
        <button class="btn btn-primary" id="saveAct" type="button">
            <b>{{ lang._('Speichern & Anwenden') }}</b> <i id="saveAct_progress"></i>
        </button>
        <button class="btn btn-default" id="restartAct" type="button">
            <b>{{ lang._('Neustart') }}</b> <i id="restartAct_progress"></i>
        </button>
    </div>
</div>
